Skip to content

Instantly share code, notes, and snippets.

@ilusi
Created November 18, 2012 11:53
Show Gist options
  • Select an option

  • Save ilusi/4104768 to your computer and use it in GitHub Desktop.

Select an option

Save ilusi/4104768 to your computer and use it in GitHub Desktop.
m101 hw3-1 - Remove lowest homework score from students
import sys
sys.path.append("/usr/local/bin/ENV/lib/python2.7/site-packages")
import pymongo
#import sys
connection = pymongo.Connection("mongodb://localhost", safe=True)
db=connection.school
students = db.students
def delete_lowest_score():
sort = [('_id',pymongo.ASCENDING)]
try:
cursor = students.find().sort(sort)
except:
print "Unexpected error:", sys.exc_info()[0]
for doc in cursor:
savedScore = 0
scores = doc['scores']
for pairs in scores:
if ('homework' == pairs['type']):
id = doc['_id']
print id
homeworkScore = pairs['score']
print "Homework score is "
print homeworkScore
if (savedScore == 0):
savedScore = homeworkScore
prevPairs = pairs
print "Saved is "
print savedScore
else:
if (savedScore > homeworkScore):
scores.remove(pairs)
print "Lower than "
print homeworkScore
else:
scores.remove(prevPairs)
print "Greater than "
print homeworkScore
print scores
students.update({ "_id": id }, { "$set": {"scores": scores }})
delete_lowest_score()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment