Skip to main content

Simple, transparent on-disk JSON store

Project description

mrjsonstore

Simple, transparent on-disk JSON store using atomicwrites.

Basic Usage

Without context

store = JsonStore('example.json')
assert isinstance(store.content, dict)
store.content['woohoo'] = 'I am just a Python dictionary'
result = store.commit()
if not result:
    print(f'There was a problem when writing: {result})

You can also use transactions...

store = JsonStore('example.json')
t = store.transaction()
store.content['woohoo'] = 'I am just a Python dictionary'
result = t.commit()
if not result:
    print(f'There was a problem when writing: {result})

... and possibly roll them back:

store = JsonStore('example.json')
store.content['woohoo'] = 'I am just a Python dictionary'
t = store.transaction()
store.content['woohoo'] = 'I am going to be rolled back!'
t.rollback()
assert store.content['woohoo'] == 'I am just a Python dictionary'

With context

store = JsonStore('example.json')
with store.transaction() as t:
    store.content['woohoo'] = 'I am just a Python dictionary'

Changes will be committed on context exit, unless there is an exception:

store = JsonStore('example.json')
store.content['woohoo'] = 'I am just a Python dictionary'
with store.transaction() as t:
    store.content['woohoo'] = 'I am going to be rolled back!'
    raise RuntimeError()
[...]
assert store.content['woohoo'] == 'I am just a Python dictionary'

If you want to commit regardless of exceptions, you can choose not to rollback:

store = JsonStore('example.json')
store.content['woohoo'] = 'I am just a Python dictionary'
with store.transaction(rollback=False) as t:
    store.content['woohoo'] = 'I am not going to be rolled back!'
    raise RuntimeError()
[...]
assert store.content['woohoo'] == 'I am not going to be rolled back!'

Changes will be committed to disk then.

TODO

  • Add support for concurrency?

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

mrjsonstore-0.5.0.tar.gz (6.5 kB view hashes)

Uploaded Source

Built Distribution

mrjsonstore-0.5.0-py3-none-any.whl (7.2 kB view hashes)

Uploaded Python 3

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