diff --git a/.isort.cfg b/.isort.cfg new file mode 100644 index 0000000..ed6782c --- /dev/null +++ b/.isort.cfg @@ -0,0 +1,11 @@ +[settings] +line_length=79 +indent=' ' +multi_line_output=3 +length_sort=0 +import_heading_stdlib=Core Library modules +import_heading_thirdparty=Third party modules +import_heading_firstparty=First party modules +import_heading_localfolder=Local modules +known_third_party = requests,setuptools +include_trailing_comma=True diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..6af3c37 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,40 @@ +# pre-commit run --all-files +repos: +- repo: https://github.com/pre-commit/pre-commit-hooks + rev: v2.3.0 + hooks: + - id: check-ast + - id: check-byte-order-marker + - id: check-case-conflict + - id: check-docstring-first + - id: check-executables-have-shebangs + - id: check-json + - id: check-yaml + - id: debug-statements + - id: detect-aws-credentials + - id: detect-private-key + - id: end-of-file-fixer + - id: trailing-whitespace + - id: mixed-line-ending + +- repo: https://github.com/asottile/seed-isort-config + rev: v1.9.3 + hooks: + - id: seed-isort-config +- repo: https://github.com/pre-commit/mirrors-isort + rev: v4.3.21 + hooks: + - id: isort +- repo: https://github.com/psf/black + rev: 19.3b0 + hooks: + - id: black +- repo: https://github.com/asottile/pyupgrade + rev: v2.1.0 + hooks: + - id: pyupgrade +- repo: https://github.com/asottile/blacken-docs + rev: v1.6.0 + hooks: + - id: blacken-docs + additional_dependencies: [black==19.3b0] diff --git a/COVID19Py/__init__.py b/COVID19Py/__init__.py index 33c6cf4..d230ca1 100644 --- a/COVID19Py/__init__.py +++ b/COVID19Py/__init__.py @@ -1 +1,2 @@ +# Local modules from .covid19 import COVID19 diff --git a/COVID19Py/covid19.py b/COVID19Py/covid19.py index 050a6b3..8a39303 100644 --- a/COVID19Py/covid19.py +++ b/COVID19Py/covid19.py @@ -1,4 +1,7 @@ +# Core Library modules from typing import Dict, List + +# Third party modules import requests @@ -9,11 +12,15 @@ class COVID19(object): latestData = None _valid_data_sources = [] - def __init__(self, url="https://coronavirus-tracker-api.herokuapp.com", data_source='jhu'): + def __init__( + self, url="https://coronavirus-tracker-api.herokuapp.com", data_source="jhu" + ): self.url = url self._valid_data_sources = self._getSources() if data_source not in self._valid_data_sources: - raise ValueError("Invalid data source. Expected one of: %s" % self._valid_data_sources) + raise ValueError( + "Invalid data source. Expected one of: %s" % self._valid_data_sources + ) self.data_source = data_source def _update(self, timelines): @@ -21,10 +28,7 @@ def _update(self, timelines): locations = self.getLocations(timelines) if self.latestData: self.previousData = self.latestData - self.latestData = { - "latest": latest, - "locations": locations - } + self.latestData = {"latest": latest, "locations": locations} def _getSources(self): response = requests.get(self.url + "/v2/sources") @@ -34,7 +38,9 @@ def _getSources(self): def _request(self, endpoint, params=None): if params is None: params = {} - response = requests.get(self.url + endpoint, {**params, "source":self.data_source}) + response = requests.get( + self.url + endpoint, {**params, "source": self.data_source} + ) response.raise_for_status() return response.json() @@ -46,16 +52,15 @@ def getLatestChanges(self): changes = None if self.previousData: changes = { - "confirmed": self.latestData["latest"]["confirmed"] - self.latestData["latest"]["confirmed"], - "deaths": self.latestData["latest"]["deaths"] - self.latestData["latest"]["deaths"], - "recovered": self.latestData["latest"]["recovered"] - self.latestData["latest"]["recovered"], + "confirmed": self.latestData["latest"]["confirmed"] + - self.latestData["latest"]["confirmed"], + "deaths": self.latestData["latest"]["deaths"] + - self.latestData["latest"]["deaths"], + "recovered": self.latestData["latest"]["recovered"] + - self.latestData["latest"]["recovered"], } else: - changes = { - "confirmed": 0, - "deaths": 0, - "recovered": 0, - } + changes = {"confirmed": 0, "deaths": 0, "recovered": 0} return changes def getLatest(self) -> List[Dict[str, int]]: @@ -79,13 +84,15 @@ def getLocations(self, timelines=False, rank_by: str = None) -> List[Dict]: data = self._request("/v2/locations") data = data["locations"] - - ranking_criteria = ['confirmed', 'deaths', 'recovered'] + + ranking_criteria = ["confirmed", "deaths", "recovered"] if rank_by is not None: if rank_by not in ranking_criteria: - raise ValueError("Invalid ranking criteria. Expected one of: %s" % ranking_criteria) + raise ValueError( + "Invalid ranking criteria. Expected one of: %s" % ranking_criteria + ) - ranked = sorted(data, key=lambda i: i['latest'][rank_by], reverse=True) + ranked = sorted(data, key=lambda i: i["latest"][rank_by], reverse=True) data = ranked return data @@ -98,7 +105,10 @@ def getLocationByCountryCode(self, country_code, timelines=False) -> List[Dict]: """ data = None if timelines: - data = self._request("/v2/locations", {"country_code": country_code, "timelines": str(timelines).lower()}) + data = self._request( + "/v2/locations", + {"country_code": country_code, "timelines": str(timelines).lower()}, + ) else: data = self._request("/v2/locations", {"country_code": country_code}) return data["locations"] diff --git a/README.md b/README.md index a602433..a6f45be 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,7 @@ It retrieves data directly from [@ExpDev07](https://github.com/ExpDev07)'s backe To achieve this, just pass the URL of the backend as a parameter to the library's constructor: ```python import COVID19Py + covid19 = COVID19Py.COVID19("https://my-awesome-covid19-backend") ``` ## Installation @@ -36,6 +37,7 @@ To use COVID19Py, you first need to import the package and then create a new ins ```python import COVID19Py + covid19 = COVID19Py.COVID19() ``` @@ -70,7 +72,7 @@ to also get timelines. You can also rank the results by `confirmed`, `deaths` or `recovered`. ```python -locations = covid19.getLocations(rank_by='recovered') +locations = covid19.getLocations(rank_by="recovered") ``` ### Getting location by country code: diff --git a/examples/latest.py b/examples/latest.py index 5d95cdb..bb45107 100644 --- a/examples/latest.py +++ b/examples/latest.py @@ -1,3 +1,4 @@ +# First party modules import COVID19Py covid19 = COVID19Py.COVID19() diff --git a/setup.cfg b/setup.cfg new file mode 100644 index 0000000..140790e --- /dev/null +++ b/setup.cfg @@ -0,0 +1,31 @@ +[metadata] +name = COVID19Py + +author = Konstantinos Kamaropoulos +author_email = k@kamaropoulos.com + +version = 0.3.0 + +description = A tiny Python package for easy access to up-to-date Coronavirus (COVID-19, SARS-CoV-2) cases data. +long_description = file: README.md +long_description_content_type = text/markdown +keywords = covid19, coronavirus, api, covid-19, wrapper + +url = https://github.com/Kamaropoulos/covid19py + +license = GPLv3 + +# https://pypi.org/pypi?%3Aaction=list_classifiers +classifiers = + Programming Language :: Python :: 2 + Programming Language :: Python :: 2.7 + Programming Language :: Python :: 3 + Programming Language :: Python :: 3.5 + Programming Language :: Python :: 3.6 + Programming Language :: Python :: 3.7 + Programming Language :: Python :: 3.8 + License :: OSI Approved :: GNU Affero General Public License v3 + Operating System :: OS Independent + +[options] +packages = find: diff --git a/setup.py b/setup.py index 18efd5a..ffe716c 100644 --- a/setup.py +++ b/setup.py @@ -1,30 +1,7 @@ +# Third party modules import setuptools -with open("README.md", "r") as fh: - long_description = fh.read() - setuptools.setup( - name="COVID19Py", # Replace with your own username - version="0.3.0", - author="Konstantinos Kamaropoulos", - author_email="k@kamaropoulos.com", - description="A tiny Python package for easy access to up-to-date Coronavirus (COVID-19, SARS-CoV-2) cases data.", - license = "GPLv3", - long_description=long_description, - long_description_content_type="text/markdown", - url="https://github.com/Kamaropoulos/covid19py", - packages=["COVID19Py"], - keywords = ['covid19', 'coronavirus', 'api', 'covid-19', 'wrapper'], - classifiers=[ - "Programming Language :: Python :: 2", - "Programming Language :: Python :: 2.7", - "Programming Language :: Python :: 3", - 'Programming Language :: Python :: 3.5', - 'Programming Language :: Python :: 3.6', - 'Programming Language :: Python :: 3.7', - 'Programming Language :: Python :: 3.8', - 'License :: OSI Approved :: GNU Affero General Public License v3', - "Operating System :: OS Independent", - ], - python_requires='>=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*', + python_requires=">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*", + install_requires=["requests"], )