This is kind of a weird problem and I'm not entirely sure how to ask it appropriately but I'll give it my best shot.
I have a custom class that is basically a wrapper for an API that updates an SQLite database with new data on each call (I can't add it to the question because it's massive and private).
What's weird is that it seems some information is being cached (I don't think this is possible but that's the only thing it reminds me of, like when you make edits in web dev and they don't update) because it works the first time, but when I try to reinitialize the object and run it again, it doesn't add any new data (when I know there is new data to be added) to the DB.
I know the code works because if I restart the kernel and run it again, it updates no problem.
I've tried deleting the object (del InitializedClass
), re-initializing, and initializing with different values but nothing seems to work. It won't update the DB unless the kernel is restarted.
Has anyone ever had an issue like this? I'm happy to provide more information if this isn't enough but I don't know how else to describe it.
Thank you!!
EDIT
The below psuedocode is basically exactly what is happening
from something import SomeClass
while True:
obj = SomeClass() # <--------- How can I "reset" this on each loop?
obj.get_new_data_from_api()
obj.update_raw_db()
obj.process_raw_data()
obj.update_processed_db()
# i tried different combinations of deleting the object
del obj
del SomeClass
from something import SomeClass
EDIT 2:
So as everyone mentioned, it was an issue with the class itself, but I still don't really understand why the error was happening. Basically, the end
argument was not being updated (I thought it would have updated to the current time each time it was called) when I made the datetime.now()
function call as the default kwarg (even after deleting the class and creating a new instance, this did not update). The issue is illustrated below:
class SomeBrokenClass():
def __init__(self):
pass
def get_endpoint(self, start, end):
return 'https://some.api.com?start_date=%s&end_date=%s' % (start, end)
# THE PROBLEM WAS WITH THIS METHOD ( .get_data() ):
# When re-initializing the class, the `end` argument
# was not being updated for some reason. Even if I completely
# delete the instance of the class, the end time would not update.
def get_data(self, start, end = int(datetime.now().timestamp() * 1000)):
return pd.read_json(self.get_endpoint(start, end))
def get_new_data_from_api(self):
start_date = self.get_start_date()
df = self.get_data(start_date)
return df
class SomeWorkingClass():
def __init__(self):
pass
def get_endpoint(self, start, end):
return 'https://some.api.com?start_date=%s&end_date=%s' % (start, end)
def get_data(self, start, end):
return pd.read_json(self.get_endpoint(start, end))
def get_new_data_from_api(self):
start_date = self.get_start_date()
end_date = int(datetime.now().timestamp() * 1000) # BUT THIS WORKS FINE
df = self.get_data(start_date, end_date)
return df