I was wondering if python has a simple method for caching a sequence of values, where the sequence can be updated each time the script is run. For example, let's say I have a list of tuples where each tuple is a datetime and a float. The datetime represents the time a speed was recorded by an anemometer and the float is the speed recorded. When I run my script, new values should be added to my list and remember the next time I run the script. When I first started programming, the way I solved this was using a pickle, as follows:
import os
import pickle
import datetime
db_path = "speeds.p"
# get all our previous speeds
speeds = []
if os.path.exists(db_path):
with open(db_path, "rb") as f:
speeds = pickle.load(f)
def data_from_endpoint():
data = (
(datetime.datetime(2022, 10, 22, 21, 15), 13),
(datetime.datetime(2022, 10, 22, 21, 30), 24),
(datetime.datetime(2022, 10, 22, 21, 45), 37)
)
for i in data:
yield i
try:
# add new speeds
for t, v in data_from_endpoint():
if len(speeds) == 0 or t > speeds[-1][0]:
print(f"Adding {t}, {v}")
speeds.append((t, v))
finally:
# save all speeds
with open(db_path, "wb") as f:
pickle.dump(speeds, f)
print(f"Number of values: {len(speeds)}")
The way I would solve this now is to use a sqlite database. Both solutions involve a lot of code for something so simple and I'm wondering if python has a simpler way of doing this.
json, so I can human-read the data in the file. But of course, it depends on how much data you handle. With pickle and json, you write all the data each time, and load everything into memory, while sqlite is a bit smarter.