From 6ee5586b74e59408baf004686dd06ffab61cd369 Mon Sep 17 00:00:00 2001 From: Marcel Schwarz Date: Sat, 19 Dec 2020 15:41:35 +0100 Subject: [PATCH] Adjust naming conventions --- projects/project-3/openapi/db_init.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/projects/project-3/openapi/db_init.py b/projects/project-3/openapi/db_init.py index 6d4a358..9ad85d0 100644 --- a/projects/project-3/openapi/db_init.py +++ b/projects/project-3/openapi/db_init.py @@ -7,11 +7,13 @@ from datetime import datetime import requests +DB_NAME = "bike-data.db" + logFormatter = logging.Formatter("%(asctime)-15s [%(levelname)8s] [%(threadName)s] - %(message)s") LOG = logging.getLogger() LOG.setLevel(logging.DEBUG) -fileHandler = logging.FileHandler("importer.log") +fileHandler = logging.FileHandler("db_init.log") fileHandler.setFormatter(logFormatter) LOG.addHandler(fileHandler) @@ -57,7 +59,7 @@ def get_online_files_list(subdir_filter=None, file_extension_filter=None): def init_database(): LOG.info("Try to create tables") - conn = sqlite3.connect("bike-data.db", timeout=300) + conn = sqlite3.connect(DB_NAME, timeout=300) conn.execute("""CREATE TABLE IF NOT EXISTS usage_stats( rental_id INTEGER PRIMARY KEY, duration INTEGER, @@ -92,7 +94,7 @@ def init_database(): def create_indexes(): LOG.info("Try to create indexes") - conn = sqlite3.connect("bike-data.db", timeout=300) + conn = sqlite3.connect(DB_NAME, timeout=300) conn.execute("""CREATE INDEX IF NOT EXISTS idx_date_of_start_date ON usage_stats (date(start_date, "unixepoch"))""") conn.commit() @@ -102,7 +104,7 @@ def create_indexes(): def import_bikepoints(): LOG.info("Importing bikepoints") - conn = sqlite3.connect("bike-data.db", timeout=300) + conn = sqlite3.connect(DB_NAME, timeout=300) points = json.loads(requests.get("https://api.tfl.gov.uk/BikePoint").text) points = list(map(lambda p: (p['id'], p['commonName'], p['lat'], p['lon']), points)) @@ -115,7 +117,7 @@ def import_bikepoints(): def import_accidents(year): LOG.info("Importing accidents") - conn = sqlite3.connect("bike-data.db", timeout=300) + conn = sqlite3.connect(DB_NAME, timeout=300) def filter_pedal_cycles(accident): for vehicle in accident['vehicles']: @@ -136,7 +138,7 @@ def import_accidents(year): def import_usage_stats_file(export_file: ApiExportFile): - conn = sqlite3.connect("bike-data.db", timeout=300) + conn = sqlite3.connect(DB_NAME, timeout=300) rows = conn.execute("SELECT * FROM read_files WHERE etag LIKE ?", (export_file.etag,)).fetchall() if len(rows) != 0: