From 8675f1da476349277dcb3fffbae59564d073531c Mon Sep 17 00:00:00 2001 From: Foxxtail1 <11395428+Foxxtail1@users.noreply.github.com> Date: Tue, 25 Jun 2024 22:11:44 -0400 Subject: [PATCH 1/2] Fixed typo --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 398a274..005cbb5 100644 --- a/README.md +++ b/README.md @@ -40,7 +40,7 @@ Open a command shell (powershell on windows) and run the command lines below > pip install -e . ``` ### Package configuration -Some environment variables must ne setup for proper package usage. If working in a local environment create a .env file in the root folder of your project and add the following lines: +Some environment variables must be setup for proper package usage. If working in a local environment create a .env file in the root folder of your project and add the following lines: ``` FMPAD_API_KEY=somerandomkeyfromFFPAD DB_SQL_LIMIT = 1000 # 0 no limits applied to SQL Requests From 238b9013bf4b7c08ca974116ee0e41d371017173 Mon Sep 17 00:00:00 2001 From: Foxxtail1 <11395428+Foxxtail1@users.noreply.github.com> Date: Tue, 25 Jun 2024 23:10:14 -0400 Subject: [PATCH 2/2] Auto create db file and folder if they do not exist --- hobbytrader/database.py | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/hobbytrader/database.py b/hobbytrader/database.py index 2900956..ce2e2e8 100644 --- a/hobbytrader/database.py +++ b/hobbytrader/database.py @@ -43,7 +43,9 @@ def generate_ID(prices_df): prices_df['ID'] = prices_df.ID + prices_df.Symbol return prices_df -def create_db_and_table(db_file) -> sqlite3.Connection: +def create_db_and_table(db_file: str) -> sqlite3.Connection: + if not isinstance(db_file, str): + raise TypeError('Parameter not a string.') # Special function to create a database with a primary key that will ignore insert of an existing key sql_create_prices_table = """ CREATE TABLE IF NOT EXISTS prices ( ID text PRIMARY KEY ON CONFLICT IGNORE, @@ -55,21 +57,14 @@ def create_db_and_table(db_file) -> sqlite3.Connection: Open real, Volume real ); """ - - if not isinstance(db_file, str): - raise TypeError('Parameter not a string.') - conn = None + os.makedirs(os.path.dirname(db_file), exist_ok=True) + conn = sqlite3.connect(db_file) + cursor = conn.cursor() if not os.path.isfile(db_file): - conn = sqlite3.connect(db_file) - cursor = conn.cursor() cursor.execute(sql_create_prices_table) - return conn else: - #raise ValueError(f'Database already exists: {db_file}') - conn = sqlite3.connect(db_file) - cursor = conn.cursor() print(f'Database already exists, using file: {db_file}') - return conn + return conn # # Save to specific file formats