-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpythonBeyondAdvanced_Review2.txt
More file actions
47 lines (40 loc) · 3.53 KB
/
Copy pathpythonBeyondAdvanced_Review2.txt
File metadata and controls
47 lines (40 loc) · 3.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
Python Beyond Advanced Review Exercise 2 see https://github.com/onionmccabbage/beyondAdvancedPythonApril2021/blob/main/pythonBeyondAdvanced_Review2.txt
======================================== until about 3:00 ish
(You may need to sign up for free for an API key)
For this project, use modules and imports to separate your code structures
In a new Python module, declare a 'WeatherGetter' class which inherits from the Thread class.
Initialize it to take a 'city' parameter. Declare a 'run' method for this class.
You will need something like this at the top of your module:
from threading import Thread
import json
import requests
import time
In the 'run' method, populate a response object by calling 'requests.get' for this end-point:
http://api.openweathermap.org/data/2.5/weather?q=athlone&units=metric&APPID=48f2d5e18b0d2bc50519b58cce6409f1
(replace 'athlone' with the city parameter)
Populate a 'data' object from the response as JSON, then set self.temperature to the returned temperature, e.g.
data = json.loads(response.text) # convert the returned reponse to JSON
self.temperature = data['main']['temp'] # access the temperature property from the JSON
(You could also read the weather description, wind speed and direction)
Declare a list of cities, e.g.
CITIES = ['Athlone', 'Dublin', 'Galway', 'Belfast', 'London', 'Cork', 'Lund', 'Kista']
Iterate over these cities, starting a new instance of your 'WeatherGetter' thread for each city
Print out the temperature for each of the cities
Include code to time how long it takes to retrieve all the weather reports
Remember to code against the main types of error that might occur here
If Time
-------
Declare a data structure to record each city and temperature, along with a time-stamp
Each thread should add to this structure, using locks as needed (enter nulls on error)
When all threads complete, persist the contents of this structure to a text file (overwrite each time)
Subsequent runs of your code should first read this file, then calculate any temperature difference between the current and previous values
(in short time scales, this temperature difference will often be zero)
Optional
--------
Also show other weather data, such as wind speed and direction, the weather description etc.
Show the temperature as Celcius (the default), Kelvin (-273) and Fahrenheit (°F=°C*1.8+32.0)
Use the 'lon' and 'lat' values to retrieve a map from https://www.google.co.uk/maps/place/lon,lat
Store each retrieved map as a text file in a reliable place on your hard drive
Try retrieving a lot of cities (but be careful of the 60-a-minute limit):
Use semaphores, RLocks and/or barriers to limit the number of concurrent requests to 5 at a time
["Dublin","Cork","Limerick","Galway","Waterford","Drogheda","Kilkenny","Wexford","Sligo","Clonmel","Dundalk","Bray","Ennis","Tralee","Carlow","Naas","Athlone","Letterkenny","Tullamore","Killarney","Arklow","Cobh","Castlebar","Midleton","Mallow","Ballina","Enniscorthy","Wicklow","Cavan","Athenry","Longford","Dungarvan","Nenagh","Trim","Thurles","Youghal","Monaghan","Buncrana","Ballinasloe","Fermoy","Westport","Carrick-on-Suir","Birr","Tipperary","Carrickmacross","Kinsale","Listowel","Clonakilty","Cashel","Macroom","Castleblayney","Kilrush","Skibbereen","Bundoran","Templemore","Clones","Newbridge","Mullingar","Balbriggan","Greystones","Leixlip","Tramore","Shannon","Gorey","Tuam","Edenderry","Bandon","Loughrea","Ardee","Mountmellick","Bantry","Boyle","Ballyshannon","Cootehill","Ballybay","Belturbet","Lismore","Kilkee","Granard"]