How to snapshot program data structures during the lifetime of a program in python?

486 views Asked by At

I want to keep track of the changes that occur in certain data structures (dictionaries) during the lifetime of my python program. We can achieve this by either taking snapshots or backups. Backups are not 'memory friendly', as they require to store the whole structure in memory. So the neat way to do this is to take a snapshot instead (something like the way Git stages files).

  1. Are there modules in python that offer this feature?
  2. If not, are there any known algorithms for describing changes in data structures without copying the data?

I don't want to use databases.

1

There are 1 answers

2
Banana On

You can use pickle to save python data structures. If you then use the date(time) module for creating the filenames, you would get something like a snapshot:

import pickle
import datetime

def backup(your_data):
    with open(f"backup {datetime.datetime.now()}", "wb") as file:
        pickle.dump(your_data, file)

For getting the data, use the pickle.load method.