-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_location.py
76 lines (56 loc) · 2.07 KB
/
main_location.py
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import csv
from dataclasses import dataclass
@dataclass
class LocalWeather:
location: str
max_temperature: int
min_temperature: int
avg_temperature: float
avg_humidity: float
@classmethod
def weather_by_location(cls, location: str, temp: list, humid: list) -> 'LocalWeather':
max_temp = max(temp)
min_temp = min(temp)
avg_temp = average(temp)
avg_humid = average(humid)
return cls(
location = location,
max_temperature = max_temp,
min_temperature = min_temp,
avg_temperature = avg_temp,
avg_humidity = avg_humid
)
def print_metrics(self):
print(f"Location:{self.location}")
print(f"Maximum temperature: {self.max_temperature}")
print(f"Minimum temperature: {self.min_temperature}")
print(f"Average temperature: {self.avg_temperature}")
print(f"Avearge humidity: {self.avg_humidity}")
def average(lst: list) -> float:
return round((sum(lst) / len(lst)),2)
def file_reader(filename: str) -> dict:
with open(filename, "r") as csvfile:
reader = csv.reader(csvfile)
next(reader)
weather_data = {}
for row in reader:
location = row[3]
if location not in weather_data:
weather_data[location] = {'temp': [], 'humid': []}
weather_data[location]['temp'].append(float(row[1]))
weather_data[location]['humid'].append(float(row[2]))
return weather_data
def analyze_weather_data(weather_data: dict) -> list[LocalWeather]:
weather_metrics = []
for location, data in weather_data.items():
temp = data['temp']
humid = data['humid']
weather = LocalWeather.weather_by_location(location, temp,humid)
weather_metrics.append(weather)
return weather_metrics
if __name__ == "__main__":
weather_data = file_reader("weather_data_with_location.csv")
weather_metrics = analyze_weather_data(weather_data)
for weather in weather_metrics:
weather.print_metrics()
print()