2020LOGGER = logging .getLogger ("services.location.jhu" )
2121PID = os .getpid ()
2222
23-
2423class JhuLocationService (LocationService ):
2524 """
2625 Service for retrieving locations from Johns Hopkins CSSE (https://github.com/CSSEGISandData/COVID-19).
@@ -34,6 +33,7 @@ async def get_all(self):
3433 async def get (self , loc_id ): # pylint: disable=arguments-differ
3534 # Get location at the index equal to provided id.
3635 locations = await self .get_all ()
36+
3737 return locations [loc_id ]
3838
3939
@@ -52,6 +52,7 @@ async def get_category(category):
5252 :returns: The data for category.
5353 :rtype: dict
5454 """
55+
5556 # Adhere to category naming standard.
5657 category = category .lower ()
5758 data_id = f"jhu.{ category } "
@@ -87,27 +88,24 @@ async def get_category(category):
8788 # Make location history from dates.
8889 history = {date : int (float (amount or 0 )) for date , amount in dates .items ()}
8990
90- # Country for this location.
91- country = item ["Country/Region" ]
92-
9391 # Latest data insert value.
9492 latest = list (history .values ())[- 1 ]
9593
94+ # Country for this location.
95+ country = item ["Country/Region" ]
96+
9697 # Normalize the item and append to locations.
97- locations .append (
98- {
99- # General info.
100- "country" : country ,
101- "country_code" : countries .country_code (country ),
102- "province" : item ["Province/State" ],
103- # Coordinates.
104- "coordinates" : {"lat" : item ["Lat" ], "long" : item ["Long" ],},
105- # History.
106- "history" : history ,
107- # Latest statistic.
108- "latest" : int (latest or 0 ),
109- }
110- )
98+ locations .append ({
99+ "country" : country ,
100+ "country_code" : countries .country_code (country ),
101+ "province" : item ["Province/State" ],
102+ "coordinates" : {
103+ "lat" : item ["Lat" ],
104+ "long" : item ["Long" ],
105+ },
106+ "history" : history ,
107+ "latest" : int (latest or 0 ),
108+ })
111109 LOGGER .debug (f"{ data_id } Data normalized" )
112110
113111 # Latest total.
@@ -135,8 +133,10 @@ async def get_locations():
135133 :returns: The locations.
136134 :rtype: List[Location]
137135 """
136+
138137 data_id = "jhu.locations"
139138 LOGGER .info (f"pid:{ PID } : { data_id } Requesting data..." )
139+
140140 # Get all of the data categories locations.
141141 confirmed = await get_category ("confirmed" )
142142 deaths = await get_category ("deaths" )
@@ -163,8 +163,8 @@ async def get_locations():
163163
164164 timelines = {
165165 "confirmed" : location ["history" ],
166- "deaths" : parse_history (key , locations_deaths , index ),
167- "recovered" : parse_history (key , locations_recovered , index ),
166+ "deaths" : parse_history (key , locations_deaths ),
167+ "recovered" : parse_history (key , locations_recovered ),
168168 }
169169
170170 # Grab coordinates.
@@ -173,16 +173,12 @@ async def get_locations():
173173 # Create location (supporting timelines) and append.
174174 locations .append (
175175 TimelinedLocation (
176- # General info.
177- index ,
178- location ["country" ],
179- location ["province" ],
180- # Coordinates.
181- Coordinates (latitude = coordinates ["lat" ], longitude = coordinates ["long" ]),
182- # Last update.
183- datetime .utcnow ().isoformat () + "Z" ,
184- # Timelines (parse dates as ISO).
185- {
176+ id = index ,
177+ country = location ["country" ],
178+ province = location ["province" ],
179+ coordinates = Coordinates (latitude = coordinates ["lat" ], longitude = coordinates ["long" ]),
180+ last_updated = datetime .utcnow ().isoformat () + "Z" ,
181+ timelines = {
186182 "confirmed" : Timeline (
187183 timeline = {
188184 datetime .strptime (date , "%m/%d/%y" ).isoformat () + "Z" : amount
@@ -204,25 +200,23 @@ async def get_locations():
204200 },
205201 )
206202 )
203+
207204 LOGGER .info (f"{ data_id } Data normalized" )
208205
209- # Finally, return the locations.
210206 return locations
211207
212208
213- def parse_history (key : tuple , locations : list , index : int ):
209+ def parse_history (key : tuple , locations : list ):
214210 """
215211 Helper for validating and extracting history content from
216- locations data based on index . Validates with the current country/province
212+ locations data based on key . Validates with the current country/province
217213 key to make sure no index/column issue.
218-
219- TEMP: solution because implement a more efficient and better approach in the refactor.
220214 """
221- location_history = {}
222- try :
223- if key == ( locations [ index ][ "country" ], locations [ index ][ "province" ]):
224- location_history = locations [ index ] ["history" ]
225- except ( IndexError , KeyError ):
226- LOGGER .debug (f"iteration data merge error: { index } { key } " )
227-
228- return location_history
215+
216+ for i , location in enumerate ( locations ) :
217+ if ( location [ "country" ], location [ "province" ]) == key :
218+ return location ["history" ]
219+
220+ LOGGER .debug (f"iteration data merge error: { key } " )
221+
222+ return {}
0 commit comments