Skip to content

Instantly share code, notes, and snippets.

@okalachev
Created December 8, 2013 22:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save okalachev/7864878 to your computer and use it in GitHub Desktop.
Save okalachev/7864878 to your computer and use it in GitHub Desktop.
from mongo import get_db
def is_next_try(session, result):
res = len(session['tries']) == result['tryNumber'] and \
(session['addr'] is None or session['addr'] == result['addr']) and \
not session['tries'][-1].get('last') and \
session['tries'][-1]['spent'] < result['spent']
if not res: return False
for id in session['tries'][-1]['answers']:
answer = session['tries'][-1]['answers'][id]
try:
if answer.get('correct'):
if not result['answers'][id]['correct']:
return False
except TypeError:
return False
except KeyError:
return False
return True
def result_to_try(result):
return {
'points': result['points'],
'percent': result['percent'],
'spent': result['spent'],
'answers': result['answers'],
'last': result.get('Last'),
}
if __name__ == '__main__':
db = get_db()
results = db.results.find().sort('datetime', 1)
sessions = []
for result in results:
if result['tryNumber'] == 0:
sessions.append({
'datetime': result['datetime'],
'addr': result.get('addr'),
'tries': [result_to_try(result)],
'maxPoints': result['maxPoints'],
})
else:
added = False
for j in range(len(sessions) - 1, -1, -1):
session = sessions[j]
if is_next_try(session, result):
session['tries'].append(result_to_try(result))
added = True
break
if not added: print 'Warning: {} result was not added ({}, {})'.format(result['_id'], result['datetime'], result['tryNumber'])
print 'Found total of {} sessions:'.format(len(sessions))
db.sessions.insert(sessions)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment