savearchive#

savearchive(filename, obj, files=None, folder=None, comments=None, require=None, user=True, caller=True, git=True, pipfreeze=True, method='dill', allow_nonzip=False, dumpargs=None, **kwargs)[source]#

Save any object as a pickled zip file, including metadata as a separate JSON file.

Pickles are usually not good for long-term data storage, since they rely on importing the libraries that were used to create the pickled object. This function partly addresses that by storing metadata along with the saved pickle. While there may still be issues opening the pickle, the metadata (which is stored separately) should give enough information to figure out how to reconstruct the original environment (allowing the pickle to be loaded, and then re-saved in a more persistent format if desired).

Note: Since this function relies on pickle, it can potentially execute arbitrary code, so you should only use it with sources you trust. For more information, see: https://docs.python.org/3/library/pickle.html

Parameters:
  • filename (str/path) – the file to save to (must end in .zip)

  • obj (any) – the object to save

  • files (str/list) – any additional files or folders to save

  • comments (str/dict) – other comments/information to store in the metadata (must be JSON-compatible)

  • require (str/dict) – if provided, an additional manual set of requirements

  • caller (bool) – store information on the current user in the metadata (see sc.metadata())

  • caller – store information on the calling file in the metadata (see sc.metadata())

  • git (bool) – store the git version in the metadata (see sc.metadata())

  • pipfreeze (bool) – store the output of “pip freeze” in the metadata (see sc.metadata())

  • method (str) – the method to use saving the data; default “dill” for more robustness, but “pickle” is faster

  • allow_nonzip (bool) – whether to permit extensions other than .zip (note, may cause problems!)

  • dumpargs (dict) – passed to sc.dumpstr()

  • kwargs (dict) – passed to sc.savezip()

Example:

obj = MyClass() # Create an arbitrary object
sc.savearchive('my-class.zip', obj)

# Much later...
obj = sc.loadarchive('my-class.zip')

New in version 3.0.0.