summaryrefslogtreecommitdiff
path: root/elo.py
blob: ed0a2f8d369f17bd0e45b12ac7a030d1a506e601 (plain)
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
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/env python
import argparse
import sys

import dateutil.parser
from f1elo.interface import Interface

parser = argparse.ArgumentParser(
    description='Ranks Formula One drivers using Elo rating',
    formatter_class=argparse.RawTextHelpFormatter)
parser.add_argument('command', metavar='COMMAND', nargs='?',
                    help="Action to execute against the database:\n"
                    "print - prints the rankings for all drivers ranked "
                    "in 12 months,\n"
                    "reset - resets the rankings,\n"
                    "rate - calculates the rankings\n"
                    "init - init clean database for the application\n"
                    "Default value is 'print'.",
                    default='print',
                    choices=['print', 'rate', 'reset', 'init'])
parser.add_argument('--date',
                    help='Date for which the action should be executed.\n'
                    'Print ratings for DATE,\n'
                    'reset ratings all the way down to DATE\n'
                    'or rank the races all the way up to DATE.')
parser.add_argument(
    '--limit',
    help='Ranking list (display) cut-off point.',
    type=int)
parser.add_argument(
    '-v',
    help='Display verbose info on rating progress to STDERR.',
    action='store_true')
parser.add_argument(
    '--force',
    '-f',
    help='Force database initialization (for "init" command).',
    action='store_true')

arguments = parser.parse_args()

command = arguments.command.lower()

date = arguments.date
if date:
    date = dateutil.parser.parse(date).date()

interface = Interface(date)

if command == 'reset':
    interface.reset(_debug=arguments.v)
elif command == 'rate':
    interface.rate(_debug=arguments.v)
elif command == 'init':
    interface.init_db(force=arguments.force)
    sys.exit(0)

rankings = interface.fetch()

if len(rankings):
    print 'Rankings for %s' % interface.date
    if arguments.limit:
        rankings = rankings[0:arguments.limit]
    for rank in rankings:
        print rank
else:
    print 'No rankings for %s' % interface.date