From 777483f8bf709b6c681ae44879b626c6428b056b Mon Sep 17 00:00:00 2001 From: Derek Holloway Date: Wed, 11 Feb 2026 20:03:25 -0800 Subject: [PATCH] move tensorflow as a lazyload and wrap in main --- main.py | 95 ++++++++++++++++++++++++++++++--------------------------- 1 file changed, 50 insertions(+), 45 deletions(-) diff --git a/main.py b/main.py index 0dd85287..40cb69aa 100644 --- a/main.py +++ b/main.py @@ -1,59 +1,64 @@ -import tensorflow as tf -import keras import sqlite3 from datetime import date, datetime +from datapuller import DataPuller -# Initilize SqLite -dbconn = sqlite3.connect("./data/appdata.db") -dbcursor = dbconn.cursor() +def main(): + # Initilize SqLite + dbconn = sqlite3.connect("./data/appdata.db") + dbcursor = dbconn.cursor() -# Create keystore table if it doesnt exist already -dbcursor.execute(''' -CREATE TABLE IF NOT EXISTS keystore ( - key TEXT PRIMARY KEY, - value TEXT -) -''') + # Create keystore table if it doesnt exist already + dbcursor.execute(''' + CREATE TABLE IF NOT EXISTS keystore ( + key TEXT PRIMARY KEY, + value TEXT + ) + ''') -def SetKey(Key, Value): - dbcursor.execute('INSERT OR REPLACE INTO keystore (key, value) VALUES (?, ?)', (Key, Value)) - dbconn.commit() + def SetKey(Key, Value): + dbcursor.execute('INSERT OR REPLACE INTO keystore (key, value) VALUES (?, ?)', (Key, Value)) + dbconn.commit() -def GetKey(Key): - dbcursor.execute('SELECT value FROM keystore WHERE key = ?', (Key, )) - result = dbcursor.fetchone() - return result[0] if result else None + def GetKey(Key): + dbcursor.execute('SELECT value FROM keystore WHERE key = ?', (Key, )) + result = dbcursor.fetchone() + return result[0] if result else None -# Pull the last run data -lastrundata = GetKey("LastRun") -lastrun = None -if lastrundata != None: - lastrun = datetime.strptime( lastrundata, "%Y-%m-%d" ) -else: - lastrun = datetime.strptime( "2000-01-01", "%Y-%m-%d" ) + # Pull the last run data + lastrundata = GetKey("LastRun") + lastrun = None + if lastrundata != None: + lastrun = datetime.strptime( lastrundata, "%Y-%m-%d" ) + else: + lastrun = datetime.strptime( "2000-01-01", "%Y-%m-%d" ) -# If the strings dont match becuase were only checking day it has to be a new day so update the data -if lastrun != str(date.today()): - # This will update the data store in the data folder - import datapuller - # Update the last run to today - SetKey("LastRun", str(date.today())) + # If the strings dont match becuase were only checking day it has to be a new day so update the data + if lastrun != str(date.today()): + # This will update the data store in the data folder + DataPuller.pull() + # Update the last run to today + SetKey("LastRun", str(date.today())) -# Load in the AI algorithm -from keras.layers import Dense, Flatten, Conv2D -from keras import Model + # Load in the AI algorithm late so yfinance works properly + import tensorflow as tf + import keras + from keras.layers import Dense, Flatten, Conv2D + from keras import Model -mnist = keras.datasets.mnist + mnist = keras.datasets.mnist -(x_train, y_train), (x_test, y_test) = mnist.load_data() -x_train, x_test = x_train / 255.0, x_test / 255.0 + (x_train, y_train), (x_test, y_test) = mnist.load_data() + x_train, x_test = x_train / 255.0, x_test / 255.0 -# Add a channels dimension -x_train = x_train[..., tf.newaxis].astype("float32") -x_test = x_test[..., tf.newaxis].astype("float32") + # Add a channels dimension + x_train = x_train[..., tf.newaxis].astype("float32") + x_test = x_test[..., tf.newaxis].astype("float32") -# batch and shuffle the dataset -train_ds = tf.data.Dataset.from_tensor_slices( - (x_train, y_train)).shuffle(10000).batch(32) + # batch and shuffle the dataset + train_ds = tf.data.Dataset.from_tensor_slices( + (x_train, y_train)).shuffle(10000).batch(32) -test_ds = tf.data.Dataset.from_tensor_slices((x_test, y_test)).batch(32) \ No newline at end of file + test_ds = tf.data.Dataset.from_tensor_slices((x_test, y_test)).batch(32) + +if __name__ == "__main__": + main() \ No newline at end of file