forked from realpython/materials
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
105 lines (81 loc) · 3.16 KB
/
Copy pathmain.py
File metadata and controls
105 lines (81 loc) · 3.16 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
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
"""
This program gathers information from the temp_data.csv file about temperature
"""
import csv
from pkg_resources import resource_filename
from datetime import datetime
from datetime import timedelta
from typing import List, Dict
from collections import defaultdict
def get_temperature_data(filepath: str) -> Dict:
"""
This function gets the temperature data from the csv file
"""
with open(filepath) as csvfile:
csv_reader = csv.DictReader(csvfile)
data = {row["name"]: row for row in csv_reader}
for value in data.values():
value.pop("name")
return data
def get_average_temp_by_date(
date_string: str, temperature_data: Dict
) -> float:
"""
This function gets the average temperature for all the samples
taken by the students by date
:param date_string: date to find average temperature for
:param connection: database connection
:return: average temp for date, or None if not found
"""
# Target date
target_date = datetime.strptime(date_string, "%Y-%m-%d").date()
# Iterate through the data and get the data
data = []
for samples in temperature_data.values():
# Iterate through the samples
for sample_date, sample in samples.items():
# Generate a date range for the sample
min_date = datetime.strptime(
sample_date, "%Y-%m-%d"
).date() + timedelta(days=-3)
max_date = datetime.strptime(
sample_date, "%Y-%m-%d"
).date() + timedelta(days=3)
if min_date <= target_date <= max_date:
data.append(float(sample))
# Get the average temp
return sum(data) / len(data)
def get_average_temp_sorted(direction: str, temperature_data: Dict) -> List:
dir = direction.lower()
if dir not in ["asc", "desc"]:
raise Exception(f"Unknown direction: {direction}")
results = defaultdict(int)
for data in temperature_data.values():
for date, value in data.items():
results[date] += float(value)
for date, total in results.items():
results[date] = float(total) / float(len(temperature_data.keys()))
# Convert dictionary to list
results = results.items()
# Sort the list in the appropriate order
return sorted(
results, key=lambda v: v[1], reverse=False if dir == "asc" else True
)
def main():
print("starting")
# Get the temperature data into a dictionary structure
filepath = resource_filename("project.data", "temp_data.csv")
temperature_data = get_temperature_data(filepath)
# Get the average temperature by date
date_string = "2019-02-10"
average_temp = get_average_temp_by_date(date_string, temperature_data)
print(f"Average temp {date_string}: {average_temp:.2f}")
print()
# Get the average temps for the year sorted ascending or descending
average_temps = get_average_temp_sorted("asc", temperature_data)
for date, average_temp in average_temps:
print(f"Date: {date}, average temp: {average_temp:.2f}")
print()
print("finished")
if __name__ == "__main__":
main()