Skip to main content

Map JSON files onto Python attributes for simple configuration storage

Project description

PyPI Build Status Coverage Report

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.6+. It will not work on 2.6 or lower. The tests are also executed on PyPy.

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
del store['a_list', :2]
assert store.a_list == [3]

# 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

Download files

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

Source Distribution

python_jsonstore-1.3.3.tar.gz (8.5 kB view details)

Uploaded Source

Built Distribution

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

python_jsonstore-1.3.3-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

Details for the file python_jsonstore-1.3.3.tar.gz.

File metadata

  • Download URL: python_jsonstore-1.3.3.tar.gz
  • Upload date:
  • Size: 8.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for python_jsonstore-1.3.3.tar.gz
Algorithm Hash digest
SHA256 39cb537ce63af179e425b662d62dfdf93d2dac9de7e0e4d4332926533f0b4d3f
MD5 8e96bd5110dcb0a3f69dca0ea07b48bb
BLAKE2b-256 5f34fe92cbfc6a151c4cda14647aa772ed1486c39ffe3248201870fbd31dd1e7

See more details on using hashes here.

Provenance

The following attestation bundles were made for python_jsonstore-1.3.3.tar.gz:

Publisher: publish.yml on Code0x58/python-jsonstore

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file python_jsonstore-1.3.3-py3-none-any.whl.

File metadata

File hashes

Hashes for python_jsonstore-1.3.3-py3-none-any.whl
Algorithm Hash digest
SHA256 03b70d5416166d287d0e5fd8f404925ed956d66af85fd5e38687e8f2c6d912ec
MD5 5414f869d5c23ea0e4d3dba74169940c
BLAKE2b-256 2421989283518e0e685da09fa60fd023369cb8ac9c10a2964ad0218599e9ff0b

See more details on using hashes here.

Provenance

The following attestation bundles were made for python_jsonstore-1.3.3-py3-none-any.whl:

Publisher: publish.yml on Code0x58/python-jsonstore

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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