forked from realpython/materials
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild_temp_data_csv.py
More file actions
114 lines (99 loc) · 1.79 KB
/
Copy pathbuild_temp_data_csv.py
File metadata and controls
114 lines (99 loc) · 1.79 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
106
107
108
109
110
111
112
113
114
import csv
import datetime
from random import randint
from pkg_resources import resource_filename
start_date = datetime.datetime.strptime("2019-01-02", "%Y-%m-%d")
students = [
"John",
"Mary",
"Susan",
"Doug",
"Andrew",
"George",
"Martha",
"Paul",
"Helen",
]
temperature_data = [
10,
12,
16,
23,
13,
12,
14,
22,
25,
28,
32,
33,
37,
36,
35,
40,
44,
45,
50,
52,
58,
60,
66,
70,
70,
72,
78,
80,
81,
82,
85,
88,
90,
87,
90,
85,
82,
81,
78,
75,
72,
72,
70,
63,
65,
62,
60,
45,
40,
37,
30,
28,
]
def offset_temp(temperature):
"""
This function modifies the temperature +/- a random
amount up to 10
:param temperature: temperature to modify
:return: modified temperature
"""
return temperature + randint(-10, 10)
def main():
# create the CSV file
csv_filepath = resource_filename("project.data", "temp_data.csv")
with open(csv_filepath, "w") as data_fh:
# create the writer
csv_writer = csv.writer(data_fh)
# write the header
header = ["name"]
for week in range(0, 52):
current_date = start_date + datetime.timedelta(days=week * 7)
header.append(current_date.strftime("%Y-%m-%d"))
csv_writer.writerow(header)
# iterate through the students and write their data
for student in students:
data = [student]
# iterate through the weeks
for week in range(0, 52):
data.append(offset_temp(temperature_data[week]))
csv_writer.writerow(data)
if __name__ == "__main__":
main()