Skip to content

Commit ac6aa5f

Browse files
committed
apply black formatting (120 line-length)
1 parent f9cf1aa commit ac6aa5f

38 files changed

+382
-334
lines changed

app/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
# See PEP396.
2-
__version__ = '2.0'
2+
__version__ = "2.0"
33

44
from .core import create_app

app/config/settings.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

33
# Load enviroment variables from .env file.
44
from dotenv import load_dotenv
5+
56
load_dotenv()
67

78
"""
89
The port to serve the app application on.
910
"""
10-
PORT = int(os.getenv('PORT', 5000))
11+
PORT = int(os.getenv("PORT", 5000))

app/coordinates.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,7 @@ def serialize(self):
1414
:returns: The serialized coordinates.
1515
:rtype: dict
1616
"""
17-
return {
18-
'latitude' : self.latitude,
19-
'longitude': self.longitude
20-
}
17+
return {"latitude": self.latitude, "longitude": self.longitude}
2118

2219
def __str__(self):
23-
return 'lat: %s, long: %s' % (self.latitude, self.longitude)
20+
return "lat: %s, long: %s" % (self.latitude, self.longitude)

app/core.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from flask import Flask
22
from flask_cors import CORS
33

4+
45
def create_app():
56
"""
67
Construct the core application.
@@ -10,7 +11,7 @@ def create_app():
1011
CORS(app)
1112

1213
# Set app config from settings.
13-
app.config.from_pyfile('config/settings.py');
14+
app.config.from_pyfile("config/settings.py")
1415

1516
with app.app_context():
1617
# Import routes.

app/data/__init__.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@
22
from ..services.location.csbs import CSBSLocationService
33

44
# Mapping of services to data-sources.
5-
data_sources = {
6-
'jhu': JhuLocationService(),
7-
'csbs': CSBSLocationService()
8-
}
5+
data_sources = {"jhu": JhuLocationService(), "csbs": CSBSLocationService()}
6+
97

108
def data_source(source):
119
"""
@@ -14,4 +12,4 @@ def data_source(source):
1412
:returns: The service.
1513
:rtype: LocationService
1614
"""
17-
return data_sources.get(source.lower())
15+
return data_sources.get(source.lower())

app/enums/sources.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
from enum import Enum
22

3+
34
class Sources(str, Enum):
45
"""
56
A source available for retrieving data.
67
"""
7-
jhu = 'jhu'
8-
csbs = 'csbs'
8+
9+
jhu = "jhu"
10+
csbs = "csbs"

app/location/__init__.py

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from ..coordinates import Coordinates
22
from ..utils import countrycodes
33

4+
45
class Location:
56
"""
67
A location in the world affected by the coronavirus.
@@ -20,7 +21,7 @@ def __init__(self, id, country, province, coordinates, last_updated, confirmed,
2021
self.confirmed = confirmed
2122
self.deaths = deaths
2223
self.recovered = recovered
23-
24+
2425
@property
2526
def country_code(self):
2627
"""
@@ -37,25 +38,19 @@ def serialize(self):
3738
"""
3839
return {
3940
# General info.
40-
'id' : self.id,
41-
'country' : self.country,
42-
'country_code': self.country_code,
43-
'province' : self.province,
44-
41+
"id": self.id,
42+
"country": self.country,
43+
"country_code": self.country_code,
44+
"province": self.province,
4545
# Coordinates.
46-
'coordinates': self.coordinates.serialize(),
47-
46+
"coordinates": self.coordinates.serialize(),
4847
# Last updated.
49-
'last_updated': self.last_updated,
50-
48+
"last_updated": self.last_updated,
5149
# Latest data (statistics).
52-
'latest': {
53-
'confirmed': self.confirmed,
54-
'deaths' : self.deaths,
55-
'recovered': self.recovered
56-
},
50+
"latest": {"confirmed": self.confirmed, "deaths": self.deaths, "recovered": self.recovered},
5751
}
5852

53+
5954
class TimelinedLocation(Location):
6055
"""
6156
A location with timelines.
@@ -64,18 +59,21 @@ class TimelinedLocation(Location):
6459
def __init__(self, id, country, province, coordinates, last_updated, timelines):
6560
super().__init__(
6661
# General info.
67-
id, country, province, coordinates, last_updated,
68-
62+
id,
63+
country,
64+
province,
65+
coordinates,
66+
last_updated,
6967
# Statistics (retrieve latest from timelines).
70-
confirmed=timelines.get('confirmed').latest or 0,
71-
deaths=timelines.get('deaths').latest or 0,
72-
recovered=timelines.get('recovered').latest or 0,
68+
confirmed=timelines.get("confirmed").latest or 0,
69+
deaths=timelines.get("deaths").latest or 0,
70+
recovered=timelines.get("recovered").latest or 0,
7371
)
7472

7573
# Set timelines.
7674
self.timelines = timelines
7775

78-
def serialize(self, timelines = False):
76+
def serialize(self, timelines=False):
7977
"""
8078
Serializes the location into a dict.
8179
@@ -87,10 +85,15 @@ def serialize(self, timelines = False):
8785

8886
# Whether to include the timelines or not.
8987
if timelines:
90-
serialized.update({ 'timelines': {
91-
# Serialize all the timelines.
92-
key: value.serialize() for (key, value) in self.timelines.items()
93-
}})
88+
serialized.update(
89+
{
90+
"timelines": {
91+
# Serialize all the timelines.
92+
key: value.serialize()
93+
for (key, value) in self.timelines.items()
94+
}
95+
}
96+
)
9497

9598
# Return the serialized location.
96-
return serialized
99+
return serialized

app/location/csbs.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,28 @@
11
from . import Location
22

3+
34
class CSBSLocation(Location):
45
"""
56
A CSBS (county) location.
67
"""
8+
79
def __init__(self, id, state, county, coordinates, last_updated, confirmed, deaths):
810
super().__init__(
911
# General info.
10-
id, 'US', state, coordinates, last_updated,
11-
12+
id,
13+
"US",
14+
state,
15+
coordinates,
16+
last_updated,
1217
# Statistics.
1318
confirmed=confirmed,
14-
deaths=deaths,
15-
recovered=0
19+
deaths=deaths,
20+
recovered=0,
1621
)
1722

1823
self.state = state
1924
self.county = county
20-
25+
2126
def serialize(self, timelines=False):
2227
"""
2328
Serializes the location into a dict.
@@ -28,10 +33,9 @@ def serialize(self, timelines=False):
2833
serialized = super().serialize()
2934

3035
# Update with new fields.
31-
serialized.update({
32-
'state': self.state,
33-
'county': self.county,
34-
})
36+
serialized.update(
37+
{"state": self.state, "county": self.county,}
38+
)
3539

3640
# Return the serialized location.
37-
return serialized
41+
return serialized

app/main.py

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@
2626
# ############
2727
# FastAPI App
2828
# ############
29-
LOGGER = logging.getLogger('api')
29+
LOGGER = logging.getLogger("api")
3030

3131
APP = FastAPI(
32-
title='Coronavirus Tracker',
33-
description='API for tracking the global coronavirus (COVID-19, SARS-CoV-2) outbreak. Project page: https://github.com/ExpDev07/coronavirus-tracker-api.',
34-
version='2.0.1',
35-
docs_url='/',
36-
redoc_url='/docs',
32+
title="Coronavirus Tracker",
33+
description="API for tracking the global coronavirus (COVID-19, SARS-CoV-2) outbreak. Project page: https://github.com/ExpDev07/coronavirus-tracker-api.",
34+
version="2.0.1",
35+
docs_url="/",
36+
redoc_url="/docs",
3737
)
3838

3939
# #####################
@@ -42,31 +42,27 @@
4242

4343
# Enable CORS.
4444
APP.add_middleware(
45-
CORSMiddleware,
46-
allow_credentials=True,
47-
allow_origins=['*'],
48-
allow_methods=['*'],
49-
allow_headers=['*'],
45+
CORSMiddleware, allow_credentials=True, allow_origins=["*"], allow_methods=["*"], allow_headers=["*"],
5046
)
5147

5248
# TODO this could probably just be a FastAPI dependency.
53-
@APP.middleware('http')
49+
@APP.middleware("http")
5450
async def add_datasource(request: Request, call_next):
5551
"""
5652
Attach the data source to the request.state.
5753
"""
5854
# Retrieve the datas ource from query param.
59-
source = data_source(request.query_params.get('source', default='jhu'))
60-
55+
source = data_source(request.query_params.get("source", default="jhu"))
56+
6157
# Abort with 404 if source cannot be found.
6258
if not source:
63-
return Response('The provided data-source was not found.', status_code=404)
64-
59+
return Response("The provided data-source was not found.", status_code=404)
60+
6561
# Attach source to request.
6662
request.state.source = source
67-
63+
6864
# Move on...
69-
LOGGER.info(f'source provided: {source.__class__.__name__}')
65+
LOGGER.info(f"source provided: {source.__class__.__name__}")
7066
response = await call_next(request)
7167
return response
7268

@@ -77,13 +73,11 @@ async def add_datasource(request: Request, call_next):
7773

7874

7975
@APP.exception_handler(pydantic.error_wrappers.ValidationError)
80-
async def handle_validation_error(
81-
request: Request, exc: pydantic.error_wrappers.ValidationError
82-
):
76+
async def handle_validation_error(request: Request, exc: pydantic.error_wrappers.ValidationError):
8377
"""
8478
Handles validation errors.
8579
"""
86-
return JSONResponse({'message': exc.errors()}, status_code=422)
80+
return JSONResponse({"message": exc.errors()}, status_code=422)
8781

8882

8983
# ################
@@ -93,17 +87,14 @@ async def handle_validation_error(
9387
from .router import router
9488

9589
# Include routers.
96-
APP.include_router(router, prefix='/v2', tags=['v2'])
90+
APP.include_router(router, prefix="/v2", tags=["v2"])
9791

9892
# mount the existing Flask app
9993
# v1 @ /
100-
APP.mount('/', WSGIMiddleware(create_app()))
94+
APP.mount("/", WSGIMiddleware(create_app()))
10195

10296
# Running of app.
103-
if __name__ == '__main__':
97+
if __name__ == "__main__":
10498
uvicorn.run(
105-
'app.main:APP',
106-
host='127.0.0.1',
107-
port=int(os.getenv('PORT', 5000)),
108-
log_level='info',
99+
"app.main:APP", host="127.0.0.1", port=int(os.getenv("PORT", 5000)), log_level="info",
109100
)

app/models/latest.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
from pydantic import BaseModel
22

3+
34
class Latest(BaseModel):
45
"""
56
Latest model.
67
"""
8+
79
confirmed: int
810
deaths: int
911
recovered: int
1012

13+
1114
class LatestResponse(BaseModel):
1215
"""
1316
Response for latest.
1417
"""
15-
latest: Latest
18+
19+
latest: Latest

0 commit comments

Comments
 (0)