summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xelo.py11
-rw-r--r--f1elo/db.py1
-rw-r--r--f1elo/interface.py8
-rw-r--r--f1elo/model.py16
4 files changed, 25 insertions, 11 deletions
diff --git a/elo.py b/elo.py
index 43e11a5..4fac540 100755
--- a/elo.py
+++ b/elo.py
@@ -1,6 +1,7 @@
#!/usr/bin/env python
import argparse
import dateutil.parser
+import sys
parser = argparse.ArgumentParser(description='Ranks Formula One drivers using Elo rating',
formatter_class=argparse.RawTextHelpFormatter)
@@ -9,8 +10,9 @@ parser.add_argument('command', metavar='COMMAND', nargs='?',
"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'])
+ 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'
@@ -18,21 +20,26 @@ parser.add_argument('--date',
'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.', 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()
from f1elo.interface import Interface
-
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()
diff --git a/f1elo/db.py b/f1elo/db.py
index bf106a6..cd7c4ac 100644
--- a/f1elo/db.py
+++ b/f1elo/db.py
@@ -10,7 +10,6 @@ config = json.load(open(path.join(path.dirname(__main__.__file__), 'config', 'db
engine = create_engine("mysql://{0[user]}:{0[pass]}@{0[host]}/{0[db]}?charset=utf8".format(config))
Session = sessionmaker(bind=engine)
-
def find_driver(name, country, session):
driver = session.query(Driver).filter(Driver.driver==name).first()
if driver:
diff --git a/f1elo/interface.py b/f1elo/interface.py
index 2065160..4b938ae 100644
--- a/f1elo/interface.py
+++ b/f1elo/interface.py
@@ -1,6 +1,8 @@
import datetime
import dateutil.relativedelta
+from sqlalchemy import MetaData
+
from f1elo.db import Session
from f1elo.elo import Elo
from f1elo.model import *
@@ -10,6 +12,12 @@ class Interface:
self.session = Session()
self.date = date
+ def init_db(self, force=False):
+ from f1elo.model import Base
+ if force:
+ Base.metadata.drop_all(self.session.get_bind())
+ Base.metadata.create_all(self.session.get_bind())
+
def reset(self, date=None, _debug=False):
if date is None:
date = self.date
diff --git a/f1elo/model.py b/f1elo/model.py
index 7b340fa..b60ff85 100644
--- a/f1elo/model.py
+++ b/f1elo/model.py
@@ -9,8 +9,8 @@ class Driver(Base):
__tablename__ = 'drivers'
id = Column(Integer, primary_key=True)
- driver = Column(String)
- country = Column(String)
+ driver = Column(String(1024))
+ country = Column(String(255))
rankings = relationship('Ranking', order_by='Ranking.rank_date', back_populates='driver')
@@ -34,8 +34,8 @@ class Entry(Base):
__tablename__ = 'entries'
id = Column(Integer, primary_key=True)
- result = Column(String)
- car_no = Column(String)
+ result = Column(String(255))
+ car_no = Column(String(255))
result_group = Column(Integer)
_race = Column(Integer, ForeignKey('races.id'))
@@ -49,7 +49,7 @@ class Race(Base):
__tablename__ = 'races'
id = Column(Integer, primary_key=True)
- race = Column(String)
+ race = Column(String(1024))
date = Column(Date)
ranked = Column(Boolean)
@@ -62,10 +62,10 @@ class Race(Base):
class RaceType(Base):
__tablename__ = 'race_types'
-
+
id = Column(Integer, primary_key=True)
- code = Column(String)
- description = Column(String)
+ code = Column(String(255))
+ description = Column(String(1024))
races = relationship('Race', back_populates='type')