-
Notifications
You must be signed in to change notification settings - Fork 4
/
office_hours_post_request.py
192 lines (153 loc) · 4.85 KB
/
office_hours_post_request.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import csv
import json
import requests
import sys
def convert_to_dict(data_in: list):
"""
Takes a list of string lists which contains each row from
a CSV and generates a dictionary of dictionaries which is
each professor and their properties
Args:
data_in (list of str lists)
Returns:
return_dict (dict of dicts)
Ex:
>> data_in = [[
"Smith, John",
"01-234",
"4567",
"jsmith@",
"10:00 - 12:00 PM",
"",
"10:00 - 12:00 PM",
"",
"10:00 - 12:00 PM"]
]
>> oh_hours = convert_to_dict(data_in)
>> oh_hours
{
"Smith, John":
{
"Name": "Smith, John"
"Office": "01-234"
"Phone": "4567"
"Email": "jsmith@"
"Monday": "10:00 - 12:00 PM"
"Tuesday": "",
"Wednesday":"10:00 - 12:00 PM"
"Thursday: "",
"Friday": "10:00 - 12:00 PM" }
}
"""
# Dictionary of professors to be returned
return_dict = {}
try:
# Parse each list of the input which is a row from the CSV
for data in data_in:
# Generate the office hours template dictionary
office_hours_dict = {
"Name": "",
"Office": "",
"Phone": "",
"Email": "",
"Monday": "",
"Tuesday": "",
"Wednesday": "",
"Thursday": "",
"Friday": "",
}
# Populate each property of the office hours dictionary
office_hours_dict["Name"] = data[0]
office_hours_dict["Office"] = data[1]
office_hours_dict["Phone"] = data[2]
office_hours_dict["Email"] = data[3]
office_hours_dict["Monday"] = data[4]
office_hours_dict["Tuesday"] = data[5]
office_hours_dict["Wednesday"] = data[6]
office_hours_dict["Thursday"] = data[7]
office_hours_dict["Friday"] = data[8]
# The key of the current professor of the professors dictionary
# is the professor's name
return_dict[data[0]] = office_hours_dict
return return_dict
except Exception as e:
raise e
def process_csv(curr_file: str):
"""
Reads in the CSV and outputs a list of string lists
which is each row of the CSV
Args:
curr_file (str)
Return:
curr_data (list of str lists)
Ex:
>> out_list = process_csv("/path/to/office_hours.csv")
>> out_list
[[
"Smith, John",
"01-234",
"4567",
"jsmith@",
"10:00 - 12:00 PM",
"",
"10:00 - 12:00 PM",
"",
"10:00 - 12:00 PM"]
]
"""
# List to be returned
curr_data = []
# Open the CSV and read the fields of the CSV
with open(curr_file, "r") as csv_file:
csvreader = csv.reader(csv_file)
fields = next(csvreader)
# Iterate through each row and append the row
# to curr_data
for row in csvreader:
curr_data.append(row)
return curr_data
def post_request(oh_dict: dict):
"""
Takes in the dictionary of professors and
sends the post request.
Args:
oh_dict (dict)
Return:
None
Ex:
>> oh_dict =
{
"Smith, John" :
{
"Name": "Smith, John"
"Office": "01-234"
"Phone": "4567"
"Email": "jsmith@"
"Monday": "10:00 - 12:00 PM"
"Tuesday": "",
"Wednesday":"10:00 - 12:00 PM"
"Thursday: "",
"Friday": "10:00 - 12:00 PM"}
}
>> post_request(oh_dict)
"""
# URL for making the post request
url = sys.argv[2]
# Header contents for the post request
headers = {"Content-Type": "application/json"}
# Passes the professor dictionary through the post request
x = requests.post(url, headers=headers, data=json.dumps(oh_dict))
if __name__ == "__main__":
if (len(sys.argv) != 3) or (sys.argv[1][-3:] != "csv"):
print(
"Usage: python office_hours_post_request.py "
'"/path/to/office_hours.csv" '
'"http://post_request_url.com/new_data/office_hours"'
)
else:
try:
csv_data = process_csv(sys.argv[1])
oh_dict = convert_to_dict(csv_data)
post_request(oh_dict)
except Exception as e:
raise e