diff options
-rw-r--r-- | f1elo/model.py | 21 | ||||
-rw-r--r-- | import-csv.py | 5 |
2 files changed, 12 insertions, 14 deletions
diff --git a/f1elo/model.py b/f1elo/model.py index 7865198..96a9ef4 100644 --- a/f1elo/model.py +++ b/f1elo/model.py @@ -25,6 +25,16 @@ class Driver(Base): return ranks[-1] return None + @staticmethod + def fetch(name, country, session): + driver = session.query(Driver).filter(Driver.driver==name).first() + if not driver: + driver = Driver() + driver.driver = name + driver.country = country + session.add(driver) + return driver + driver_entry = Table('driver_entries', Base.metadata, Column('_driver', Integer, ForeignKey('drivers.id')), Column('_entry', Integer, ForeignKey('entries.id')), @@ -85,16 +95,5 @@ class Ranking(Base): def __repr__(self): return ("%s: %0.2f (%s)" % (self.driver.__repr__().decode('utf8'), self.ranking, self. rank_date)).encode('utf8') -def find_driver(name, country, session): - driver = session.query(Driver).filter(Driver.driver==name).first() - if driver: - return driver - else: - driver = Driver() - driver.driver = name - driver.country = country - session.add(driver) - return driver - __all__ = ['Driver', 'Entry', 'Ranking', 'Race', 'RaceType'] diff --git a/import-csv.py b/import-csv.py index 49b699e..199fa5e 100644 --- a/import-csv.py +++ b/import-csv.py @@ -2,7 +2,6 @@ import csv import sys from f1elo.db import Session -from f1elo.model import find_driver from f1elo.model import * session = Session() @@ -18,10 +17,10 @@ with open(sys.argv[1]) as f: entry.car_no = row[2] entry.result_group = row[5] session.add(entry) - driver = find_driver(row[4].strip(), row[3].strip(), session) + driver = Driver.fetch(row[4].strip(), row[3].strip(), session) entry.drivers.append(driver) elif len(row) == 2: - driver = find_driver(row[1].strip(), row[0].strip(), session) + driver = Driver.fetch(row[1].strip(), row[0].strip(), session) entry.drivers.append(driver) else: print row |