Skip to main content

Json key store with secured feature.

Project description

PyPi Package Build Status Codacy Rating

jsonstore

This module provides a class that maps keys and values from a JSON file onto its attributes.

The goal was to provide a convenient way of loading and saving configuration in a familiar human readable format. This is a bit more flexible than the configparser module which is included with Python.

This works is tested and working on Python 2.7+ and Python 3.3+. It will not work on 2.6 or lower, but is expected to work on 3.0-3.2. The tests do not work in 3.2.6 due to mistreating the 💩 when parsing the test code. This is also tested on pypy and pypy3.

Examples

Basics

# by default JsonStore commits on every change unless in a transaction
store = JsonStore('config.json')
store.a_string = "something"
store.a_list = [1, 2, 3]
store.a_dictionary = {
  'dict-list': [{}],
  'ln(2)': 0.69314718056,
  'for-you': u"💐",
}

# you can use […] to set/get/delete string keys
store['some_key'] = "a value"
# the key is split on '.'s and works on dictionaries
del store['a_dictionary.dict-list']
store['a_dictionary.new_value'] = "old value"
#  you can also use the syntactic sugar for tuple keys (explicit lists work too)
assert store['a_dictionary', 'new_value'] == "old value"
# you can traverse lists too
assert store['a_list', -1] == 3
# you can use slices in lists
assert len(store['a_list', 1:]) == 2

# deep copies are made when assigning values
my_list = ['fun']
store.a_list = my_list
assert store.a_list is not my_list
assert 'a_list' in store

# deep copies are also returned to avoid unsanitary changes being made
store.a_dictionary['new_value'] = "new value"  # won't update the store!
assert store.a_dictionary['new_value'] == "old value"
assert store.a_dictionary is not store.a_dictionary

# Appending to, extending a list
>>> store.list = [1, 2, 3]

# Because of the fact that .append won't modify the list in the actual file,
# but only a copy...
>>> store.list.append(4)
>>> store.list
[1, 2, 3]

# ... we need to rather use the += operator to append to a list.
>>> store.list += [4]
>>> store.list
[1, 2, 3, 4]

# Similarly, we can extend the list
>>> store.list += [5, 6]
>>> store.list
[1, 2, 3, 4, 5, 6]

Transactions

JsonStore objects can be used as context managers to provide transactions which are rolled back in the event of an exception. The transaction model is primitive; you can only nest transactions.

While a store is put into a transaction, it will not save changes to file until all of the transactions have been closed.

from jsonstore import JsonStore

# even with auto_commit=True, the file won't be saved until the last contexts has been closed
with JsonStore('config.json', indent=None, auto_commit=False) as store:
  self.value = 1

# the context manager will roll back changes made if an exception is raised
store = JsonStore('config.json', indent=None)
try:
  with store:
    store.value = "new"
    raise Exception
except Exception:
  pass
# here we see the value that was saved previously
assert store.value == 1

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

pyfastkvjson-2.1.103.tar.gz (15.2 kB view details)

Uploaded Source

Built Distribution

pyfastkvjson-2.1.103-py2.py3-none-any.whl (6.0 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file pyfastkvjson-2.1.103.tar.gz.

File metadata

  • Download URL: pyfastkvjson-2.1.103.tar.gz
  • Upload date:
  • Size: 15.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.25.1 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.49.0 CPython/3.8.5

File hashes

Hashes for pyfastkvjson-2.1.103.tar.gz
Algorithm Hash digest
SHA256 1d48d1d95f531ca9adcff4582466561b46c37dbda8fe0c847b33eefd0b5ae020
MD5 e3c64ab6d37bf120b33e814d9faad78c
BLAKE2b-256 408ab2f93e044e95e7f16d0d9ad645dea335fa68fef40f4ccc94bf3a926796b1

See more details on using hashes here.

File details

Details for the file pyfastkvjson-2.1.103-py2.py3-none-any.whl.

File metadata

  • Download URL: pyfastkvjson-2.1.103-py2.py3-none-any.whl
  • Upload date:
  • Size: 6.0 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.25.1 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.49.0 CPython/3.8.5

File hashes

Hashes for pyfastkvjson-2.1.103-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 238978da36f65b51d6f1b17c1424855604ce80059c68b623d22d285cd7c8c84c
MD5 4c18a9e6a77e39fad9242f26b3c1ff38
BLAKE2b-256 7fc184ec45d7731b02360eceba75fe56549ee0bc16197cee165924c5cbdb7d6a

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