Read and write dictionaries using selectors. The library does not depend on Django.
Requires Python 3.13 or newer; tested on 3.13, 3.14 and 3.15.
pip install derafu-selectorfrom derafu_selector import selector
data = {'issuer': {'tax_id': 76192083, 'name': 'ACME'}, 'type': 33}
selector(data, 'issuer.tax_id') # 76192083
selector(data, 'issuer.activity||"no activity"') # 'no activity'
selector(data, 'issuer.activity', 'Services') # True: writes the valueWithout value it reads: it returns the value, or None if it could not be
read. With value it writes: it returns True or False. With debug=True
the exceptions are raised instead of returning None or False, for example
NotADictionaryError (an AttributeError) or NotAssignableError and
NotIterableError (both TypeError). force_write and debug are
keyword-only.
| Form | Example |
|---|---|
| Simple and nested key | a, a.b.c |
| Element of a list | list[1] |
| Element of a list of dictionaries matched by another key | list[key=value:result_key] |
| Quoted strings and concatenation | "Total: "(a.b) |
| Alternatives (the first non-empty value) | a||b||"default" |
| Ternary IF | ((a) = "x" ? (b) : (c)) |
| JSONPath | $.a.b |
| JMESPath | jmespath:a.b |
IF operators: =, ==, !=, <>, >, >=, <, <=, contains,
length and is ("None" or "not None").
It also includes dictionary_read_case_insensitive(), which reads a key
ignoring upper and lower case.
make install-dev PYTHON=python3.15 # creates venv/ with the library and tools
make check # ruff, mypy, pyright and pytest
make build # builds dist/ and checks it with twine
make upload # builds dist/ and publishes it on PyPI
make docs # builds the Sphinx docs in docs/_build/htmltypings/jsonpath.pyi provides the type stubs for jsonpath, which ships
without them.
MIT. See LICENSE.