Пример #1
0
if __name__ == "__main__":
    # start up the default logging
    logging.basicConfig(level=logging.DEBUG)

    # connect to mongodb
    try:
        connect('tweet-store')
    except:
        logging.critical('couldnt connect to mongodb!')
        sys.exit(-1)
    else:
        logging.info('connected to mongodb')

    while True:
        battles = Battle.objects(active = True)

        for battle in battles:
            if (battle.end - datetime.now()) <= timedelta(seconds=0):
                logging.info("battle '%s' ended!" % battle)

                battle.active = False
                battle.save()

                # find the winner
                max_votes = 0
                max_choice = None
                total_votes = 0
                for choice, voters in battle.choices.items():
                    num_votes = len(voters)
                    total_votes += num_votes
Пример #2
0
from mongoengine import *
from db.document import Battle
from datetime import datetime
import sys

connect('tweet-store')
print 'enter tag: '
tag = sys.stdin.readline().strip()
print 'enter choices: '
c = sys.stdin.readline().split(',')
d = {c[0].strip():[], c[1].strip():[]}
b = Battle(tag = tag, start = datetime.now(), suggester = "someone", choices = d, active = True)
b.save()
b = Battle.objects.get(tag = tag)
print b.tag, b.choices