From 11e9db5a7583f486eb2438a952d462842f0bafee Mon Sep 17 00:00:00 2001 From: gamesguru Date: Fri, 21 May 2021 20:03:17 -0400 Subject: [PATCH] cleanup python --- build.py | 2 +- sql/__init__.py | 23 +++++++++++++---------- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/build.py b/build.py index 1a4c548..07045ae 100755 --- a/build.py +++ b/build.py @@ -1,5 +1,5 @@ #!/usr/bin/env python3 - +"""Executable script for building nt.sqlite""" if __name__ == "__main__": from sql import build_ntsqlite diff --git a/sql/__init__.py b/sql/__init__.py index 7c6633b..34ccd06 100644 --- a/sql/__init__.py +++ b/sql/__init__.py @@ -1,14 +1,17 @@ #!/usr/bin/env python3 +"""Main module for building nt.sqlite""" + import csv import os import sqlite3 NT_DB_NAME = "nt.sqlite" +SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__)) def build_ntsqlite(verbose=False): + """Builds and inserts stock data into nt.sqlite""" # cd into this script's directory - SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__)) os.chdir(SCRIPT_DIR) if verbose: @@ -28,18 +31,18 @@ def build_ntsqlite(verbose=False): if verbose: print("-> Populate data") - for p in os.listdir("data"): - if not p.endswith(".csv"): + for file_path in os.listdir("data"): + if not file_path.endswith(".csv"): continue - t = os.path.splitext(os.path.basename(p))[0] - p = os.path.join("data", p) + table_name = os.path.splitext(os.path.basename(file_path))[0] + file_path_full = os.path.join("data", file_path) # Loop over CSV files - with open(p) as f: - reader = csv.DictReader(f) - q = ",".join("?" * len(reader.fieldnames)) - reader = csv.reader(f) - cur.executemany(f"INSERT INTO {t} VALUES ({q});", reader) + with open(file_path_full) as csv_file: + reader = csv.DictReader(csv_file) + values = ",".join("?" * len(reader.fieldnames)) + reader = csv.reader(csv_file) + cur.executemany(f"INSERT INTO {table_name} VALUES ({values});", reader) cur.close() con.commit() -- 2.52.0