From: gamesguru Date: Sat, 22 May 2021 00:03:17 +0000 (-0400) Subject: cleanup python X-Git-Tag: 0.0.2~1 X-Git-Url: https://git.nutra.tk/v2?a=commitdiff_plain;h=11e9db5a7583f486eb2438a952d462842f0bafee;p=nutratech%2Fnt-sqlite.git cleanup python --- 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()