run command to install
pip install pyfastkvjson
jsonstore kv
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]
Additional encryptions
TEST_PASSWORD="123456"
secret_file_name = mktemp() + ".aes"
store_ram = JsonStore(secret_file_name, indent=None, auto_commit=True, password=TEST_PASSWORD)
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
Metadata
Release files for pyfastkvjson 3.1.16
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| pyfastkvjson-3.1.16.tar.gz | 159.4 kB | Without provenance | Details for pyfastkvjson-3.1.16.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| pyfastkvjson-3.1.16-py2.py3-none-any.whl | Python 2, Python 3 | none | any | Details for pyfastkvjson-3.1.16-py2.py3-none-any.whl |
Total release size: 166.0 kB
Release files / pyfastkvjson-3.1.16.tar.gz
| Download URL | pyfastkvjson-3.1.16.tar.gz |
|---|---|
| Size | 159.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
1df9ccaa469cf17bc1d1fa55ec00381bfd2c813e307f3a84c3eb31e8ed350cf0
|
|
BLAKE2b-256 checksum How to use checksums |
d5f431a3dd0beb7aa02a94a2af0482f81c0df9274e22cba90b31e89f99d44a98
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.9.12
|
Release files / pyfastkvjson-3.1.16-py2.py3-none-any.whl
| Download URL | pyfastkvjson-3.1.16-py2.py3-none-any.whl |
|---|---|
| Size | 6.6 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
3bee4f6ee81f7e9d460dc1efc5c6376e4f150d831c14ca67fe0fa53107d53cf2
|
|
BLAKE2b-256 checksum How to use checksums |
da3b7d1aeaac56c22e5bc3955691c321bdbe4d6c8d76491243ab873d0f1f5d12
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.9.12
|
Security for pyfastkvjson 3.1.16
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.