From b0d16a037f2ad325e7d3649a9585cdaa9cec92d4 Mon Sep 17 00:00:00 2001 From: ChUrl Date: Sat, 4 Nov 2023 00:34:13 +0100 Subject: [PATCH] Reformat --- database_utils.py | 10 ++++++---- formula10.py | 8 +++++--- model.py | 27 ++++++++++++++++++++++----- 3 files changed, 33 insertions(+), 12 deletions(-) diff --git a/database_utils.py b/database_utils.py index f2215c1..10fa79c 100644 --- a/database_utils.py +++ b/database_utils.py @@ -1,19 +1,20 @@ import csv from model import * + def load_csv(filename): with open("data/" + filename + ".csv", newline="") as file: reader = csv.reader(file, delimiter=",") - next(reader, None) # skip header + next(reader, None) # skip header return list(reader) + # @todo CSV-Writer def write_csv(filename): with open("data/" + filename + ".csv", newline="") as file: writer = csv.writer(file, delimiter=",") -# @todo Complete CSV Files -# @todo Reload Static Data + # Reload static database data, this has to be called from the app context def reload_static_data(db): print("Initializing DataBase with Static Values...") @@ -45,6 +46,7 @@ def reload_static_data(db): db.session.commit() + # @todo Export Dynamic Data def export_dynamic_data(): - pass \ No newline at end of file + pass diff --git a/formula10.py b/formula10.py index cbb8dda..4d923d7 100644 --- a/formula10.py +++ b/formula10.py @@ -5,17 +5,19 @@ from database_utils import reload_static_data, export_dynamic_data app = Flask(__name__) -app.config['SQLALCHEMY_DATABASE_URI'] = "sqlite:///formula10.db"; -app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False; +app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///formula10.db" +app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False db.init_app(app) + @app.route("/") def index(): reload_static_data(db) return render_template("index.jinja") + # @app.route("/teams", methods=["GET", "POST"]) # def teams(): # if request.method == "POST": @@ -31,4 +33,4 @@ def index(): if __name__ == "__main__": - app.run(debug=False) \ No newline at end of file + app.run(debug=False) diff --git a/model.py b/model.py index 65f04bc..560ffa1 100644 --- a/model.py +++ b/model.py @@ -10,12 +10,14 @@ db = SQLAlchemy() # Modeling these entities separately should make it easier to add new guess categories, for example team guesses or GP guesses + def optional_date(date_string): if date_string == "\\N": return None return dt.date.fromisoformat(date_string) + def optional_time(time_string): if time_string == "\\N": return None @@ -26,18 +28,21 @@ def optional_time(time_string): return dt.time.fromisoformat(time_string) + def optional_int(int_string): if int_string == "\\N": return None return int(int_string) + def optional_str(str_string): if str_string == "\\N": return None return str(str_string) + class Circuit(db.Model): __tablename__ = "circuit" @@ -55,6 +60,7 @@ class Circuit(db.Model): location: Mapped[str] = mapped_column(String(128)) country: Mapped[str] = mapped_column(String(128)) + class Season(db.Model): __tablename__ = "season" @@ -62,7 +68,8 @@ class Season(db.Model): self.year = int(row[0]) return self - year: Mapped[int] = mapped_column(Integer, primary_key=True) # This is the year + year: Mapped[int] = mapped_column(Integer, primary_key=True) + class Race(db.Model): __tablename__ = "race" @@ -73,7 +80,9 @@ class Race(db.Model): self.round = int(row[2]) self.circuitId = int(row[3]) self.name = str(row[4]) - self.date = optional_date(row[5]) # optional_date shouldn't return None for this table + self.date = optional_date( + row[5] + ) # optional_date shouldn't return None for this table self.time = optional_time(row[6]) return self @@ -87,6 +96,7 @@ class Race(db.Model): circuit: Mapped["Circuit"] = relationship("Circuit", foreign_keys=[circuitId]) + class Constructor(db.Model): __tablename__ = "constructor" @@ -102,6 +112,7 @@ class Constructor(db.Model): name: Mapped[str] = mapped_column(String(64)) nationality: Mapped[str] = mapped_column(String(64)) + class Driver(db.Model): __tablename__ = "driver" @@ -112,7 +123,9 @@ class Driver(db.Model): self.code = str(row[3]) self.forename = str(row[4]) self.surname = str(row[5]) - self.dob = optional_date(row[6]) # optional_date shouldn't return None for this table + self.dob = optional_date( + row[6] + ) # optional_date shouldn't return None for this table self.nationality = str(row[7]) return self @@ -125,6 +138,7 @@ class Driver(db.Model): dob: Mapped[dt.date] = mapped_column(Date) nationality: Mapped[str] = mapped_column(String(32)) + class Status(db.Model): __tablename__ = "status" @@ -136,6 +150,7 @@ class Status(db.Model): statusId: Mapped[int] = mapped_column(Integer, primary_key=True) status: Mapped[str] = mapped_column(String(32)) + class Result(db.Model): __tablename__ = "result" @@ -181,7 +196,9 @@ class Result(db.Model): race: Mapped["Race"] = relationship("Race", foreign_keys=[raceId]) driver: Mapped["Driver"] = relationship("Driver", foreign_keys=[driverId]) - constructor: Mapped["Constructor"] = relationship("Constructor", foreign_keys=[constructorId]) + constructor: Mapped["Constructor"] = relationship( + "Constructor", foreign_keys=[constructorId] + ) status: Mapped["Status"] = relationship("Status", foreign_keys=[statusId]) @@ -224,4 +241,4 @@ class Result(db.Model): # season: Mapped["Season"] = relationship("Season", foreign_keys=[season_id]) # Redundant but should make things easier # p10: Mapped["Driver"] = relationship("Driver", foreign_keys=[p10_id]) # dnf: Mapped["Driver"] = relationship("Driver", foreign_keys=[dnf_id]) -# raceresult: Mapped["RaceResult"] = relationship("RaceResult", foreign_keys=[raceresult_id]) \ No newline at end of file +# raceresult: Mapped["RaceResult"] = relationship("RaceResult", foreign_keys=[raceresult_id])