Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MAINT: Apply black, isort, pre-commit #9

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .isort.cfg
Original file line number Diff line number Diff line change
@@ -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
40 changes: 40 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -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]
1 change: 1 addition & 0 deletions COVID19Py/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
# Local modules
from .covid19 import COVID19
50 changes: 30 additions & 20 deletions COVID19Py/covid19.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# Core Library modules
from typing import Dict, List

# Third party modules
import requests


Expand All @@ -9,22 +12,23 @@ 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):
latest = self.getLatest()
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")
Expand All @@ -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()

Expand All @@ -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]]:
Expand All @@ -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
Expand All @@ -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"]
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
```

Expand Down Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions examples/latest.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# First party modules
import COVID19Py

covid19 = COVID19Py.COVID19()
Expand Down
31 changes: 31 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
[metadata]
name = COVID19Py

author = Konstantinos Kamaropoulos
author_email = [email protected]

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:
29 changes: 3 additions & 26 deletions setup.py
Original file line number Diff line number Diff line change
@@ -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="[email protected]",
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"],
)