-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathhw3-1-remove.py
48 lines (40 loc) · 1.57 KB
/
hw3-1-remove.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
#!/usr/bin/python
# -*- coding: UTF8 -*-
import pymongo
import sys
# Homework 3.1 · Course M101P
#
# Write a program in the language of your choice that will remove
# the lowest homework score for each student. Since there is a single
# document for each student containing an array of scores, you will
# need to update the scores array and remove the homework.
#
# Note: when run twice, all homework scores will have been removed.
connection = pymongo.MongoClient("mongodb://localhost")
db = connection.school
students = db.students
def remove_lowest_homework_score( scores):
homework_scores = [score for score in scores if score[u"type"] == u"homework"]
lowest = min( [score[u"score"] for score in homework_scores])
return [score for score in scores \
if ( score[u"type"] != u"homework") \
or( score[u"type"] == u"homework" and score[u"score"] != lowest)]
def update_scores( coll, doc_id, new_scores):
try:
coll.update( { "_id": doc_id}, { "$set": { "scores": new_scores }})
except:
print "Unexpected error while updating:", sys.exc_info()[ 0]
def main( argv):
try:
# All students having a score of type "homework" in their scores array
cursor = students.find( { 'scores.type': 'homework' })
except:
print "Unexpected error while finding:", sys.exc_info()[ 0]
for doc in cursor:
id = doc[ "_id"]
scores = doc[ "scores"]
updated_scores = remove_lowest_homework_score( scores)
update_scores( students, id, updated_scores)
print "updated doc %s:\n scores: %s\nnew scores: %s" % ( id, scores, updated_scores)
if __name__ == "__main__":
main(sys.argv[1:])