user123123123 user123123123 - 6 months ago 14
Python Question

How to date-stamp a pickled object

I have a script that obtains certain information online and stores it in a list. I'd like to keep the list for future executions of the script, and then retrieve the information online again after a certain period of time (let's say 3 days), just in case it's gone stale. My understanding is that I can pickle the list (but please let me know if there's a more advisable way to store it -- EDIT: should I use shelve or json instead?).

My main question is this: what's the best and most idiomatic way to store the date and time of the pickle and then evaluate if 3 days have passed?

Answer

An approach that completely avoids having to store and manage a separate file object for tracking the timestamp is to use os.path.getmtime() to get the date modified timestamp that linux records for the list/file that you are refreshing.

For example:

import os
import datetime
import time
threshold = datetime.timedelta(days=3) # can also be minutes, seconds, etc.
filetime = os.path.getmtime(filename) # filename is the path to the local file you are refreshing
now = time.time()
delta = datetime.timedelta(seconds=now-filetime)
if delta > threshold:
    # do something