-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCreateDictionary.py
More file actions
56 lines (46 loc) · 2.04 KB
/
Copy pathCreateDictionary.py
File metadata and controls
56 lines (46 loc) · 2.04 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
import pymongo
import os
import json
from dotenv import load_dotenv
#loads environment variables and connects to database
load_dotenv()
database_url = os.environ.get("CS125MONGO")
client = pymongo.MongoClient(database_url)
db = client.Fall2019Clean
submission_collection = db.plSubmissions
#creates a nested dictionary where each person contains a question id and number of timestamps corresponding to how many times they submitted that question
def create_dictionary():
dictionary_of_people = {}
#loop through all the documents in the collection
for document in submission_collection.find():
#loops through the fields in the document
timestamp = None
email = None
question_id = None
score = None
#loops through the document to find relevant fields
for key in document:
if key == "feedback" and not document[key] == None:
#loops through feedback object to find score and timestamp
for feedback_key in document[key]:
if feedback_key == "end_time":
timestamp = document[key][feedback_key]
if feedback_key == "results":
for result_key in document[key][feedback_key]:
if result_key == "score":
score = document[key][feedback_key][result_key]
#sets question_id
if key == "question_name":
question_id = document[key]
#sets email
if key == "email":
email = document[key]
#adds into the dictionary
if email in dictionary_of_people:
if question_id in dictionary_of_people[email]:
dictionary_of_people[email][question_id].update({timestamp: score})
else:
dictionary_of_people[email][question_id] = {timestamp: score}
else:
dictionary_of_people.update({email : {question_id : {timestamp: score}}})
return dictionary_of_people