Skip to main content

A simple `MutableMapping` (dict-like) that transparently persists its contents to a JSON file on disk. Uses only the most reliable file opening modes (`r` and `w`). Maximum compatibility with network filesystems (e.g. NAS, SSHFS, NFS), at the cost of atomicity and data race safety.

Project description

simple-json-mapped-dict

A simple MutableMapping (dict-like) that transparently persists its contents to a JSON file on disk. Uses only the most reliable file opening modes (r and w). Maximum compatibility with network filesystems (e.g. NAS, SSHFS, NFS), at the cost of atomicity and data race safety.

Features

  • Behaves just like a Python dict
  • Contents always kept in sync with a backing JSON file
  • All values must be JSON-serializable (numbers, strings, lists, objects, etc.)
    • If you try to set a value that cannot be serialized, the in-memory state will roll back and an exception will be thrown
  • Uses only standard JSON and file IO, no dependencies outside the standard library
  • [De]serializes through Python's OrderedDict - key order is preserved on disk

When to Use

  • When you need a persistent, human-readable, cross-platform key/value store, but don't require full ACID transactional reliability, or multiprocess safety
  • When you must use filesystems with flaky support for "advanced" file locking/modes, such as SSHFS, NFS, Samba, or network-attached storage (NAS)
  • When "good enough" durability, simplicity, and auditability are your main needs

When Not to Use

  • Do not use for concurrent/multi-user/multiprocess writes: This class is not race-safe—simultaneous modifications may corrupt or lose data
  • Do not use for large data (entire dict is serialized/deserialized each time)
  • Do not store non-JSON-serializable objects

Installation

pip install simple-json-mapped-dict

Usage

Basic Example

# coding=utf-8
from simple_json_mapped_dict import SimpleJSONMappedDict

d = SimpleJSONMappedDict('mydata.json')
d[u'foo'] = u'bar'
d.update({u'baz': 123})
print(d[u'foo'])  # 'bar'
del d[u'foo']

After running the above, mydata.json would look like:

{"baz": 123}

Alternatives

Contributing

Contributions are welcome! Please submit pull requests or open issues on the GitHub repository.

License

This project is licensed under the MIT License.

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

simple_json_mapped_dict-0.1.0a0.tar.gz (4.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

simple_json_mapped_dict-0.1.0a0-py2.py3-none-any.whl (4.5 kB view details)

Uploaded Python 2Python 3

File details

Details for the file simple_json_mapped_dict-0.1.0a0.tar.gz.

File metadata

File hashes

Hashes for simple_json_mapped_dict-0.1.0a0.tar.gz
Algorithm Hash digest
SHA256 388e7c8cb49a29db2ea6358fd01d12f6be438b1bd6b847def2e16bb3cb4a07f1
MD5 624feb9f07513df0e3b463bccc0e3ac8
BLAKE2b-256 37eaa854977a25d0ae846e6dbcd630d91ef9cc4c32a08008fecfa05ad5ddd8b7

See more details on using hashes here.

File details

Details for the file simple_json_mapped_dict-0.1.0a0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for simple_json_mapped_dict-0.1.0a0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 1859bada0923a56abd56ba508ca59af7f2ac1bafa34fe73bb2e3191140831f19
MD5 fa26854b58c7ca76f99e0b862685ae79
BLAKE2b-256 70c19941d13a00d40151b7092a53951fc5333b230aae2765265f2ba692849cab

See more details on using hashes here.

Supported by

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