-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
135 lines (95 loc) · 3.92 KB
/
main.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
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import csv
import requests
URL = "https://raw.githubusercontent.com/nytimes/covid-19-data/master/us-states.csv"
def main():
# Download NYTimes Covid Database
data = fetch_data(URL)
# Process CSV data
reader = process_data(data)
# Construct 14 day lists of new cases for each states
new_cases = calculate_new_cases(reader)
# Create a list to store selected states
states = process_input(new_cases)
# Print out 7-day averages for this week vs last week
comparative_averages(new_cases, states)
def calculate_new_cases(reader):
"""
Calculate the new COVID cases for each state over the last 14 days.
Args:
reader: A CSV DictReader object containing the COVID data.
Returns:
A dictionary with states as keys and a list of new cases as values.
"""
previous_cases = {}
new_cases = {}
for row in reader:
current_state = row["state"]
current_cases = int(row["cases"])
if current_state not in new_cases:
new_cases[current_state] = []
previous_cases[current_state] = current_cases
else:
new_cases[current_state].append(current_cases - previous_cases[current_state])
previous_cases[current_state] = current_cases
if len(new_cases[current_state]) > 14:
new_cases[current_state].pop(0)
return new_cases
def comparative_averages(new_cases, states):
"""
Prints the seven-day average of new COVID-19 cases for selected states, comparing the last week to the previous week.
Args:
new_cases (dict): Dictionary with state names as keys and lists of daily new cases as values.
states (list): List of states selected by the user.
Returns:
None
"""
for state in states:
last_week_average = seven_day_average(new_cases[state][-7:])
penultimate_week_average = seven_day_average(new_cases[state][:7])
if last_week_average > penultimate_week_average:
increase = calculate_percentage_change(last_week_average, penultimate_week_average)
print(f"{state} had a 7-day average of {round(last_week_average)} and an increase of {round(increase)}%.")
elif last_week_average < penultimate_week_average:
decrease = calculate_percentage_change(penultimate_week_average, last_week_average)
print(f"{state} had a 7-day average of {round(last_week_average)} and a decrease of {round(decrease)}%.")
else:
print(f"{state} had a 7-day average of {round(last_week_average)}, same as last week.")
def seven_day_average(l):
return sum(l) / 7
def calculate_percentage_change(new, old):
try:
return ((new - old) / old) * 100
except ZeroDivisionError:
print("An error occured calculating the percentage")
return None
def fetch_data(URL):
try:
download = requests.get(URL)
download.raise_for_status()
decoded_content = download.content.decode("utf-8")
return decoded_content
except requests.exceptions.RequestException as e:
print(f"Error fetching data: {e}")
return
def process_data(data):
try:
file = data.splitlines()
reader = csv.DictReader(file)
return reader
except Exception as e:
print(f"Error processing csv data: {e}")
def process_input(new_cases):
states = []
print("Choose one or more states to view average COVID cases.")
print("Press enter when done.\n")
while True:
state = input("State: ")
if state == "":
break
elif state in new_cases:
states.append(state)
else:
print(f"Invalid state: {state}. Please enter a valid state name")
print(f"\nSeven-Day Averages") if len(states) > 0 else print("No state selected")
return states
main()