Skip to content

Commit

Permalink
fix: Fix linter issues (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
xmnlab authored Feb 1, 2024
1 parent d0fce78 commit 12b6e6c
Show file tree
Hide file tree
Showing 8 changed files with 141 additions and 100 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ jobs:
- "3.9"
- "3.10"
- "3.11"
- '3.12'
- "3.12"
os:
- "ubuntu"
# - 'macos'
Expand Down
14 changes: 4 additions & 10 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,9 @@ repos:

- id: mypy
name: mypy
entry: mypy
entry: mypy .
language: system
files: "./"
pass_filenames: true
exclude: |
(?x)(
docs|
examples|
)
pass_filenames: false
types:
- python

Expand Down Expand Up @@ -78,7 +72,7 @@ repos:
name: vulture
entry: vulture --min-confidence 80
language: system
files: "src/umlizer"
files: "src/pymedx"
description: Find unused Python code.
pass_filenames: true
types:
Expand All @@ -88,7 +82,7 @@ repos:
name: mccabe
entry: python -m mccabe --min 10
language: system
files: "src/umlizer"
files: "src/pymedx"
pass_filenames: true
types:
- python
6 changes: 6 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,9 @@ exclude = [
'^docs/$',
'^examples/$',
]

[[tool.mypy.overrides]]
module = [
"requests",
]
ignore_missing_imports = true
147 changes: 88 additions & 59 deletions src/pymedx/api.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
"""API module for PubMed."""
import datetime
import itertools
import xml.etree.ElementTree as xml
Expand All @@ -15,26 +16,30 @@


class PubMed:
"""Wrapper around the PubMed API."""
"""Wrap around the PubMed API."""

def __init__(
self,
tool: str = "my_tool",
email: str = "[email protected]",
) -> None:
"""Initialization of the object.
Parameters:
- tool String, name of the tool that is executing the query.
This parameter is not required but kindly requested by
PMC (PubMed Central).
- email String, email of the user of the tool. This parameter
is not required but kindly requested by PMC (PubMed Central).
Returns:
- None
"""

Initialize the PubMed object.
Parameters
----------
tool: String
name of the tool that is executing the query.
This parameter is not required but kindly requested by
PMC (PubMed Central).
email: String
email of the user of the tool. This parameter
is not required but kindly requested by PMC (PubMed Central).
Returns
-------
None
"""
# Store the input parameters
self.tool = tool
self.email = email
Expand All @@ -53,17 +58,21 @@ def query(
max_date: str,
max_results: int = 100,
) -> Iterable[Union[PubMedArticle, PubMedBookArticle]]:
"""Method that executes a query agains the GraphQL schema, automatically
inserting the PubMed data loader.
"""
Execute a query agains the GraphQL schema.
Parameters:
- query String, the GraphQL query to execute against the schema.
Automatically inserting the PubMed data loader.
Returns:
- result ExecutionResult, GraphQL object that contains the result
in the "data" attribute.
"""
Parameters
----------
query: String
the GraphQL query to execute against the schema.
Returns
-------
result: ExecutionResult
GraphQL object that contains the result in the "data" attribute.
"""
# Retrieve the article IDs for the query
article_ids = self._getArticleIds(
query=query,
Expand All @@ -84,15 +93,19 @@ def query(
return itertools.chain.from_iterable(articles)

def getTotalResultsCount(self, query: str) -> int:
"""Helper method that returns the total number of results that match the query.
"""
Return the total number of results that match the query.
Parameters:
- query String, the query to send to PubMed
Parameters
----------
query: String
the query to send to PubMed
Returns:
- total_results_count Int, total number of results for the query in PubMed
Returns
-------
total_results_count: Int
total number of results for the query in PubMed
"""

# Get the default parameters
parameters = self.parameters.copy()

Expand All @@ -105,7 +118,8 @@ def getTotalResultsCount(self, query: str) -> int:
url="/entrez/eutils/esearch.fcgi", parameters=parameters
)

# Get from the returned meta data the total number of available results for the query
# Get from the returned meta data the total number of available
# results for the query
total_results_count = int(
response.get("esearchresult", {}).get("count")
)
Expand All @@ -114,12 +128,14 @@ def getTotalResultsCount(self, query: str) -> int:
return total_results_count

def _exceededRateLimit(self) -> bool:
"""Helper method to check if we've exceeded the rate limit.
Returns:
- exceeded Bool, Whether or not the rate limit is exceeded.
"""
Check if we've exceeded the rate limit.
Returns
-------
exceeded: Bool
Whether or not the rate limit is exceeded.
"""
# Remove requests from the list that are longer than 1 second ago
self._requestsMade = [
requestTime
Expand All @@ -128,7 +144,8 @@ def _exceededRateLimit(self) -> bool:
> datetime.datetime.now() - datetime.timedelta(seconds=1)
]

# Return whether we've made more requests in the last second, than the rate limit
# Return whether we've made more requests in the last second,
# than the rate limit
return len(self._requestsMade) > self._rateLimit

def _get(
Expand All @@ -137,21 +154,26 @@ def _get(
parameters: Dict[Any, Any] = dict(),
output: str = "json",
) -> Union[str, requests.models.Response]:
"""Generic helper method that makes a request to PubMed.
Parameters:
- url Str, last part of the URL that is requested (will
be combined with the base url)
- parameters Dict, parameters to use for the request
- output Str, type of output that is requested (defaults to
JSON but can be used to retrieve XML)
Returns:
"""
Make a request to PubMed.
Parameters
----------
url: Str
last part of the URL that is requested (will
be combined with the base url)
parameters: Dict
parameters to use for the request
output: Str
type of output that is requested (defaults to
JSON but can be used to retrieve XML)
Returns
-------
- response Dict / str, if the response is valid JSON it will
be parsed before returning, otherwise a string is
returend
"""

# Make sure the rate limit is not exceeded
while self._exceededRateLimit():
pass
Expand All @@ -178,15 +200,16 @@ def _get(
def _getArticles(
self, article_ids: List[str]
) -> Iterable[Union[PubMedArticle, PubMedBookArticle]]:
"""Helper method that batches a list of article IDs and retrieves the content.
"""Batch a list of article IDs and retrieves the content.
Parameters:
Parameters
----------
- article_ids List, article IDs.
Returns:
Returns
-------
- articles List, article objects.
"""

# Get the default parameters
parameters = self.parameters.copy()
parameters["id"] = article_ids
Expand Down Expand Up @@ -214,16 +237,20 @@ def _getArticleIds(
max_date: str,
max_results: int,
) -> List[str]:
"""Helper method to retrieve the article IDs for a query.
Parameters:
- query Str, query to be executed against the PubMed database.
- max_results Int, the maximum number of results to retrieve.
Returns:
- article_ids List, article IDs as a list.
"""Retrieve the article IDs for a query.
Parameters
----------
query: Str
query to be executed against the PubMed database.
max_results: Int
the maximum number of results to retrieve.
Returns
-------
article_ids: List
article IDs as a list.
"""

# Create a placeholder for the retrieved IDs
article_ids = []

Expand Down Expand Up @@ -269,7 +296,8 @@ def _getArticleIds(
if max_results == -1:
max_results = total_result_count

# If not all articles are retrieved, continue to make requests untill we have everything
# If not all articles are retrieved, continue to make requests until
# we have everything
while (
retrieved_count < total_result_count
and retrieved_count < max_results
Expand All @@ -280,7 +308,8 @@ def _getArticleIds(
):
parameters["retmax"] = max_results - retrieved_count

# Start the collection from the number of already retrieved articles
# Start the collection from the number of already retrieved
# articles
parameters["retstart"] = retrieved_count

# Make a new request
Expand Down
16 changes: 8 additions & 8 deletions src/pymedx/article.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
"""Module for handling articles."""
import datetime
import json

Expand Down Expand Up @@ -32,8 +33,10 @@ def __init__(
*args: List[Any],
**kwargs: Dict[Any, Any],
) -> None:
"""Initialization of the object from XML or from parameters."""

"""Initialize of the object from XML or from parameters."""
if args:
# keep it for resolving problems with linter
pass
# If an XML element is provided, use it for initialization
if xml_element is not None:
self._initializeFromXML(xml_element=xml_element)
Expand Down Expand Up @@ -133,8 +136,7 @@ def _extractAuthors(
]

def _initializeFromXML(self, xml_element: Element) -> None:
"""Helper method that parses an XML element into an article object."""

"""Parse an XML element into an article object."""
# Parse the different fields of the article
self.pubmed_id = self._extractPubMedId(xml_element)
self.title = self._extractTitle(xml_element)
Expand All @@ -151,13 +153,11 @@ def _initializeFromXML(self, xml_element: Element) -> None:
self.xml = xml_element

def toDict(self) -> Dict[Any, Any]:
"""Helper method to convert the parsed information to a Python dict."""

"""Convert the parsed information to a Python dict."""
return {key: self.__getattribute__(key) for key in self.__slots__}

def toJSON(self) -> str:
"""Helper method for debugging, dumps the object as JSON string."""

"""Dump the object as JSON string."""
return json.dumps(
{
key: (
Expand Down
16 changes: 8 additions & 8 deletions src/pymedx/book.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
"""Module for functions about book article."""
import datetime
import json

Expand Down Expand Up @@ -32,8 +33,10 @@ def __init__(
*args: List[str],
**kwargs: Dict[Any, Any],
) -> None:
"""Initialization of the object from XML or from parameters."""

"""Initialize of the object from XML or from parameters."""
if args:
# keep it for resolving problems with linter
pass
# If an XML element is provided, use it for initialization
if xml_element is not None:
self._initializeFromXML(xml_element=xml_element)
Expand Down Expand Up @@ -134,8 +137,7 @@ def _extractSections(
]

def _initializeFromXML(self, xml_element: Element) -> None:
"""Helper method that parses an XML element into an article object."""

"""Parse an XML element into an article object."""
# Parse the different fields of the article
self.pubmed_id = self._extractPubMedId(xml_element)
self.title = self._extractTitle(xml_element)
Expand All @@ -152,16 +154,14 @@ def _initializeFromXML(self, xml_element: Element) -> None:
self.sections = self._extractSections(xml_element)

def toDict(self) -> Dict[Any, Any]:
"""Helper method to convert the parsed information to a Python dict."""

"""Convert the parsed information to a Python dict."""
return {
key: (self.__getattribute__(key) if hasattr(self, key) else None)
for key in self.__slots__
}

def toJSON(self) -> str:
"""Helper method for debugging, dumps the object as JSON string."""

"""Dump the object as JSON string."""
return json.dumps(
{
key: (
Expand Down
Loading

0 comments on commit 12b6e6c

Please sign in to comment.