How to save a variable and read it on the next run

Question:

How to compare today’s date with the date from the last run (could be today’s or yesterday’s date)?

curr_date = date.today()
run_date = # depends on last run

this run_date variable should contain previous day’s date. After the script has run the run date should be updated

run_date = curr_date

The variable curr_date will be updated everyday when the program is run using job to execute it every 24 hours.

What I want to do is when the first time program is executed both curr_date and run_date should contain same values, which is today’s date. But when next time curr_date and run_date are compared, the date of the last run should be assigned to run_date and todays date to curr_date.

wait will not work because I don’t want to pause my program just for some condition.

this is the condition I want to run:

if curr_date > run_date:
    batch_id = batch_id + 1
Asked By: Mayank Singh

||

Answers:

I assume that you don’t have a background task that just sleeps for 24hrs and then wakes up again, but indeed you have a script execution per day, maybe scheduled by a cronjob.

To transport data/variables from one execution to the next, you need to "save" the data from the last run and reload it whenever the script is executed.
I suggest a solution using pickle.

I also assume that you only need to save last_run date and can get todays date with date.today().


Here is an example script that reads the datetime object, corresponding to the last_run, from a pickle file. Whenever you execute the script a check is performed if the pickle store exists. If not it is assumed that this is the first run and the last run date is set as todays date.

import os
import pickle
from datetime import date


STORE = os.path.join(os.path.dirname(__file__), 'last_run.pickle')

today = date.today()
print("Today is", today)

# Load the stored date from last run:
if os.path.isfile(STORE):
    print('Read last_run from:', STORE)
    with open(STORE, 'rb') as store:
        last_run = pickle.load(store)
else:
    print('No STORE detected. Assuming this is the first run...')
    last_run = today

print("Last run was", last_run)
if last_run < today:
    # Do what needs to be done
    pass

# store todays run date for the next run:
with open(STORE, 'wb') as store:
    pickle.dump(today, store)

print('Saved last_run to:', STORE)

For the very first run we get

Today is 2022-12-16
No STORE detected. Assuming this is the first run...
Last run was 2022-12-16
Saved last_run to: last_run.pickle

If a pickle file exists the output would read like

Today is 2022-12-16
Read last_run from: last_run.pickle
Last run was 2022-12-15
Saved last_run to: last_run.pickle

Please note that this is a very basc example that does not cover everything you might want to think of. E.g. error handling: What happens if an error is raised. Here there would be no saving of the last_run data. That could be desired or not, depending on your requirements.

Answered By: Cpt.Hook
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.