Skip to main content

SaveState - persistent storage of arbitrary python objects

Project description

SaveState - persistent storage of arbitrary python objects

SaveState is meant to be a cross-platform fast file storage for arbitrary python objects, similarly to python's shelve-module. It's mostly a rewrite of semidbm2, but with more mapping-like functions, a context manager, and the aforementioned support for arbitrary python objects.

Implementation details:

  • No requirements or dependencies
  • A dict-like interface (no unions)
  • Same, single file on windows and linux (unlike shelve)
  • Key and value integrity can be evaluated with a checksum, which will detect data corruption on key access.
    • This is also used to recover from data corruption by skipping data which can't be validated
  • Objects have to support pickling so that they can be used with savestate. Note the security implications of this!
  • All the keys of the savestate are kept in memory, which limits the savestate size (not a problem for most applications)
  • NOT Thread safe, so can't be accessed by multiple processes
  • File is append-only, so the more non-read operations you do, the more the filesize is going to balloon
    • Howevever, you can compact the savestate, usually on savestate.close(), which will replace the savestate with a new file with only the current non-deleted data. This will impact performance a little, but not by much

Performance:

  • About 50-60% of the performance of shelve with gdbm (linux), but >5000% compared to shelve with dumbdbm (windows) (>20000% for deletes!)
    • Performance is more favourable with large keys and values when compared to gdbm, but gdbm's still faster on subsequent reads/writes thanks to it's caching
  • A dbm-mode for about double the speed of reqular mode, but only string-type keys and value
    • This is about 25-30% of the performance of gdbm on its own.

Source code includes a benchmark that you can run to get more accurate performance on your specific machine.

Using SaveState:

Use with open and close:

>>> import savestate
>>> 
>>> # Open savestate
>>> state = savestate.open("savestate", "c")
>>> 
>>> # Add data to savestate
>>> state["foo"] = "bar"
>>> 
>>> # Get item from savestate
>>> print(state["foo"])
bar

>>> # Delete key from savestate
>>> del state["foo"]
>>> 
>>> # Close the savestate
>>> state.close()

Use as a contect manager:

>>> with savestate.open("filename.savestate", "c") as state:   
>>>     state["foo"] = "baz"                                                   
>>>     ...

Documentation:

savestate.open(filename, flag, verify_checksums, compact, dbm_mode)
  • filename: str - The name of the savestate. Will have .savestate added to it, if it doesn't have it.
  • flag: Literal["r", "w", "c", "n"] - Specifies how the savestate should be opened.
    • "r" = Open existing savestate for reading only (default).
    • "w" = Open existing savestate for reading and writing.
    • "c" = Open savestate for reading and writing, creating it if it doesn't exist.
    • "n" = Always create a new, empty savestate, open for reading and writing.
  • verify_checksum: bool - Verify that the checksums for each value are correct on every __getitem__ call
  • compact: bool - Indicate whether or not to compact the savestate before closing it. No effect in read only mode.
  • dbm_mode: bool - Operate in dbm mode. This is faster, but only allows strings for keys and values.

'Read-Only' mode:

>>>  # Magic methods
>>> savestate[key]
>>> key in savestate
>>> len(savestate)
>>> iter(savestate)
>>> reversed(savestate)
>>> str(savestate)
>>> repr(savestate)
>>>
>>>  # Properties
>>> savestate.filepath
>>> savestate.filename
>>> savestate.isopen
>>>
>>>  # Mapping-like methods
>>> savestate.keys()
>>> savestate.values()
>>> savestate.items()
>>> savestate.get(key: Any, default: Any = None)
>>>
>>>  # Special methods
>>> savestate.close()
>>>  ### Closes the savestate. Accessing keys after this 
>>>  ### will cause an AttributeError.

'Read-Write', 'Create' and 'New' modes:

  • Extend read-only mode with these methods
>>> # Magic methods
>>> savestate[key] = value
>>> del savestate[key]
>>> 
>>> # Mapping-like methods
>>> savestate.pop(key: Any, default: Any = None)
>>> savestate.popitem() -> tuple[Any, Any]
>>> savestate.clear()
>>> savestate.setdefault(key: Any, default: Any = None)
>>> savestate.update(other: Mapping[Any, Any], **kwargs: Any)
>>> savestate.copy(new_filename: str)
>>> ### AssertionError if new filename is same as current one.
>>> ### THIS WILL OVERWRITE ANY FILES WITH THE GIVEN FILENAME!
>>> ### Note: new filename will have '.savestate' added to it, 
>>> ### if it doesn't have it
>>>
>>> # Special methods
>>> savestate.sync()
>>> ### Flushes existing databuffers and ensures that data
>>> ### is written to the disk. Always called on savestate.close()
>>> savestate.compact()
>>> ### Rewrite the contents of the files, which will
>>> ### reduce the size of the file due to implementation details
>>> savestate.close(compact: bool = False)
>>> ### Setting compact=True will compact the savestate
>>> ### even if it was not set so at savestate.open()

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

savestate-0.0.1.tar.gz (16.6 kB view details)

Uploaded Source

Built Distribution

savestate-0.0.1-py3-none-any.whl (16.3 kB view details)

Uploaded Python 3

File details

Details for the file savestate-0.0.1.tar.gz.

File metadata

  • Download URL: savestate-0.0.1.tar.gz
  • Upload date:
  • Size: 16.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.25.1 setuptools/54.0.0 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.9.0

File hashes

Hashes for savestate-0.0.1.tar.gz
Algorithm Hash digest
SHA256 5aaa1aa727135e37df667fba8e9e5e122ad4603725765ee455186239f7844574
MD5 4524c2749a754d3660bf0ba92b2b8938
BLAKE2b-256 bded8228d7b14471f4bf30223eced7628790e2bbafd3bedc79cad4ff85696904

See more details on using hashes here.

File details

Details for the file savestate-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: savestate-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 16.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.25.1 setuptools/54.0.0 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.9.0

File hashes

Hashes for savestate-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d5bea5078c68b8bcb4d03d8c32bc30e521ef6b2b8d7335d68b114ac479319a22
MD5 5f054033a30d087b11e67697fc7751a9
BLAKE2b-256 2ac577b9cc31a788d363bca0fdc8108f5ecae00473cada6d4e36e4af2f5005f2

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page