Skip to content

Commit

Permalink
chore: Update short-sale-volume-data-importer.py to handle directory …
Browse files Browse the repository at this point in the history
…input and catch IntegrityError
  • Loading branch information
namuan committed Dec 31, 2024
1 parent cbe03bf commit b06b278
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions short-sale-volume-data-importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import sqlite3
from argparse import ArgumentParser, RawDescriptionHelpFormatter
from pathlib import Path
from sqlite3 import IntegrityError

import pandas as pd

Expand Down Expand Up @@ -58,7 +59,7 @@ def parse_args():
"--input",
type=Path,
required=True,
help="Path to the input text file containing short sale volume data",
help="Path to the input directory containing short sale volume data files",
)
parser.add_argument(
"-d",
Expand Down Expand Up @@ -120,18 +121,18 @@ def import_data(input_file, db_path):
df.to_sql("short_sale_volume", conn, if_exists="append", index=False)

logging.info(f"Successfully imported data from {input_file} to {db_path}")
except Exception as e:
except IntegrityError as e:
logging.error(f"Error importing data: {str(e)}")
raise


def main(args):
# Create the output SQLite database if it doesn't exist
args.database.parent.mkdir(parents=True, exist_ok=True)
create_database_schema(args.database)

# Import the data
import_data(args.input, args.database)
# Process all files in the input directory
for input_file in args.input.glob("*.txt"):
import_data(input_file, args.database)


if __name__ == "__main__":
Expand Down

0 comments on commit b06b278

Please sign in to comment.