derafu-selector 1.0.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- derafu_selector-1.0.0/LICENSE +21 -0
- derafu_selector-1.0.0/PKG-INFO +94 -0
- derafu_selector-1.0.0/README.md +67 -0
- derafu_selector-1.0.0/pyproject.toml +162 -0
- derafu_selector-1.0.0/setup.cfg +4 -0
- derafu_selector-1.0.0/src/derafu_selector/__init__.py +24 -0
- derafu_selector-1.0.0/src/derafu_selector/py.typed +0 -0
- derafu_selector-1.0.0/src/derafu_selector/selector.py +690 -0
- derafu_selector-1.0.0/src/derafu_selector.egg-info/PKG-INFO +94 -0
- derafu_selector-1.0.0/src/derafu_selector.egg-info/SOURCES.txt +12 -0
- derafu_selector-1.0.0/src/derafu_selector.egg-info/dependency_links.txt +1 -0
- derafu_selector-1.0.0/src/derafu_selector.egg-info/requires.txt +2 -0
- derafu_selector-1.0.0/src/derafu_selector.egg-info/top_level.txt +1 -0
- derafu_selector-1.0.0/tests/test_selector.py +316 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Esteban De La Fuente Rubio / Derafu <https://www.derafu.org>
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: derafu-selector
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: Read and write dictionaries using selectors (paths, IF, JSONPath and JMESPath).
|
|
5
|
+
Author: Derafu
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Repository, https://github.com/derafu/selector-python
|
|
8
|
+
Project-URL: Issues, https://github.com/derafu/selector-python/issues
|
|
9
|
+
Keywords: dictionary,selector,jsonpath,jmespath,json
|
|
10
|
+
Classifier: Development Status :: 5 - Production/Stable
|
|
11
|
+
Classifier: Intended Audience :: Developers
|
|
12
|
+
Classifier: Operating System :: OS Independent
|
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
|
14
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.15
|
|
18
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
19
|
+
Classifier: Topic :: Text Processing
|
|
20
|
+
Classifier: Typing :: Typed
|
|
21
|
+
Requires-Python: >=3.13
|
|
22
|
+
Description-Content-Type: text/markdown
|
|
23
|
+
License-File: LICENSE
|
|
24
|
+
Requires-Dist: jmespath>=1.1.0
|
|
25
|
+
Requires-Dist: jsonpath>=0.82.2
|
|
26
|
+
Dynamic: license-file
|
|
27
|
+
|
|
28
|
+
# derafu-selector
|
|
29
|
+
|
|
30
|
+
Read and write dictionaries using selectors. The library does not depend on
|
|
31
|
+
Django.
|
|
32
|
+
|
|
33
|
+
Requires Python 3.13 or newer; tested on 3.13, 3.14 and 3.15.
|
|
34
|
+
|
|
35
|
+
## Installation
|
|
36
|
+
|
|
37
|
+
```sh
|
|
38
|
+
pip install derafu-selector
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## Usage
|
|
42
|
+
|
|
43
|
+
```python
|
|
44
|
+
from derafu_selector import selector
|
|
45
|
+
|
|
46
|
+
data = {'issuer': {'tax_id': 76192083, 'name': 'ACME'}, 'type': 33}
|
|
47
|
+
|
|
48
|
+
selector(data, 'issuer.tax_id') # 76192083
|
|
49
|
+
selector(data, 'issuer.activity||"no activity"') # 'no activity'
|
|
50
|
+
selector(data, 'issuer.activity', 'Services') # True: writes the value
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
Without `value` it reads: it returns the value, or `None` if it could not be
|
|
54
|
+
read. With `value` it writes: it returns `True` or `False`. With `debug=True`
|
|
55
|
+
the exceptions are raised instead of returning `None` or `False`, for example
|
|
56
|
+
`NotADictionaryError` (an `AttributeError`) or `NotAssignableError` and
|
|
57
|
+
`NotIterableError` (both `TypeError`). `force_write` and `debug` are
|
|
58
|
+
keyword-only.
|
|
59
|
+
|
|
60
|
+
## Selectors
|
|
61
|
+
|
|
62
|
+
| Form | Example |
|
|
63
|
+
|---|---|
|
|
64
|
+
| Simple and nested key | `a`, `a.b.c` |
|
|
65
|
+
| Element of a list | `list[1]` |
|
|
66
|
+
| Element of a list of dictionaries matched by another key | `list[key=value:result_key]` |
|
|
67
|
+
| Quoted strings and concatenation | `"Total: "(a.b)` |
|
|
68
|
+
| Alternatives (the first non-empty value) | `a\|\|b\|\|"default"` |
|
|
69
|
+
| Ternary IF | `((a) = "x" ? (b) : (c))` |
|
|
70
|
+
| JSONPath | `$.a.b` |
|
|
71
|
+
| JMESPath | `jmespath:a.b` |
|
|
72
|
+
|
|
73
|
+
IF operators: `=`, `==`, `!=`, `<>`, `>`, `>=`, `<`, `<=`, `contains`,
|
|
74
|
+
`length` and `is` (`"None"` or `"not None"`).
|
|
75
|
+
|
|
76
|
+
It also includes `dictionary_read_case_insensitive()`, which reads a key
|
|
77
|
+
ignoring upper and lower case.
|
|
78
|
+
|
|
79
|
+
## Development
|
|
80
|
+
|
|
81
|
+
```sh
|
|
82
|
+
make install-dev PYTHON=python3.15 # creates venv/ with the library and tools
|
|
83
|
+
make check # ruff, mypy, pyright and pytest
|
|
84
|
+
make build # builds dist/ and checks it with twine
|
|
85
|
+
make upload # builds dist/ and publishes it on PyPI
|
|
86
|
+
make docs # builds the Sphinx docs in docs/_build/html
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
`typings/jsonpath.pyi` provides the type stubs for `jsonpath`, which ships
|
|
90
|
+
without them.
|
|
91
|
+
|
|
92
|
+
## License
|
|
93
|
+
|
|
94
|
+
MIT. See [LICENSE](https://github.com/derafu/selector-python/blob/main/LICENSE).
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
# derafu-selector
|
|
2
|
+
|
|
3
|
+
Read and write dictionaries using selectors. The library does not depend on
|
|
4
|
+
Django.
|
|
5
|
+
|
|
6
|
+
Requires Python 3.13 or newer; tested on 3.13, 3.14 and 3.15.
|
|
7
|
+
|
|
8
|
+
## Installation
|
|
9
|
+
|
|
10
|
+
```sh
|
|
11
|
+
pip install derafu-selector
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
## Usage
|
|
15
|
+
|
|
16
|
+
```python
|
|
17
|
+
from derafu_selector import selector
|
|
18
|
+
|
|
19
|
+
data = {'issuer': {'tax_id': 76192083, 'name': 'ACME'}, 'type': 33}
|
|
20
|
+
|
|
21
|
+
selector(data, 'issuer.tax_id') # 76192083
|
|
22
|
+
selector(data, 'issuer.activity||"no activity"') # 'no activity'
|
|
23
|
+
selector(data, 'issuer.activity', 'Services') # True: writes the value
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
Without `value` it reads: it returns the value, or `None` if it could not be
|
|
27
|
+
read. With `value` it writes: it returns `True` or `False`. With `debug=True`
|
|
28
|
+
the exceptions are raised instead of returning `None` or `False`, for example
|
|
29
|
+
`NotADictionaryError` (an `AttributeError`) or `NotAssignableError` and
|
|
30
|
+
`NotIterableError` (both `TypeError`). `force_write` and `debug` are
|
|
31
|
+
keyword-only.
|
|
32
|
+
|
|
33
|
+
## Selectors
|
|
34
|
+
|
|
35
|
+
| Form | Example |
|
|
36
|
+
|---|---|
|
|
37
|
+
| Simple and nested key | `a`, `a.b.c` |
|
|
38
|
+
| Element of a list | `list[1]` |
|
|
39
|
+
| Element of a list of dictionaries matched by another key | `list[key=value:result_key]` |
|
|
40
|
+
| Quoted strings and concatenation | `"Total: "(a.b)` |
|
|
41
|
+
| Alternatives (the first non-empty value) | `a\|\|b\|\|"default"` |
|
|
42
|
+
| Ternary IF | `((a) = "x" ? (b) : (c))` |
|
|
43
|
+
| JSONPath | `$.a.b` |
|
|
44
|
+
| JMESPath | `jmespath:a.b` |
|
|
45
|
+
|
|
46
|
+
IF operators: `=`, `==`, `!=`, `<>`, `>`, `>=`, `<`, `<=`, `contains`,
|
|
47
|
+
`length` and `is` (`"None"` or `"not None"`).
|
|
48
|
+
|
|
49
|
+
It also includes `dictionary_read_case_insensitive()`, which reads a key
|
|
50
|
+
ignoring upper and lower case.
|
|
51
|
+
|
|
52
|
+
## Development
|
|
53
|
+
|
|
54
|
+
```sh
|
|
55
|
+
make install-dev PYTHON=python3.15 # creates venv/ with the library and tools
|
|
56
|
+
make check # ruff, mypy, pyright and pytest
|
|
57
|
+
make build # builds dist/ and checks it with twine
|
|
58
|
+
make upload # builds dist/ and publishes it on PyPI
|
|
59
|
+
make docs # builds the Sphinx docs in docs/_build/html
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
`typings/jsonpath.pyi` provides the type stubs for `jsonpath`, which ships
|
|
63
|
+
without them.
|
|
64
|
+
|
|
65
|
+
## License
|
|
66
|
+
|
|
67
|
+
MIT. See [LICENSE](https://github.com/derafu/selector-python/blob/main/LICENSE).
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=84.2.1"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "derafu-selector"
|
|
7
|
+
version = "1.0.0"
|
|
8
|
+
description = "Read and write dictionaries using selectors (paths, IF, JSONPath and JMESPath)."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = "MIT"
|
|
11
|
+
license-files = ["LICENSE"]
|
|
12
|
+
requires-python = ">=3.13"
|
|
13
|
+
authors = [{ name = "Derafu" }]
|
|
14
|
+
keywords = ["dictionary", "selector", "jsonpath", "jmespath", "json"]
|
|
15
|
+
classifiers = [
|
|
16
|
+
"Development Status :: 5 - Production/Stable",
|
|
17
|
+
"Intended Audience :: Developers",
|
|
18
|
+
"Operating System :: OS Independent",
|
|
19
|
+
"Programming Language :: Python :: 3",
|
|
20
|
+
"Programming Language :: Python :: 3 :: Only",
|
|
21
|
+
"Programming Language :: Python :: 3.13",
|
|
22
|
+
"Programming Language :: Python :: 3.14",
|
|
23
|
+
"Programming Language :: Python :: 3.15",
|
|
24
|
+
"Topic :: Software Development :: Libraries :: Python Modules",
|
|
25
|
+
"Topic :: Text Processing",
|
|
26
|
+
"Typing :: Typed",
|
|
27
|
+
]
|
|
28
|
+
dependencies = [
|
|
29
|
+
"jmespath>=1.1.0", # "jmespath:" queries
|
|
30
|
+
"jsonpath>=0.82.2", # "$." queries
|
|
31
|
+
]
|
|
32
|
+
|
|
33
|
+
[project.urls]
|
|
34
|
+
Repository = "https://github.com/derafu/selector-python"
|
|
35
|
+
Issues = "https://github.com/derafu/selector-python/issues"
|
|
36
|
+
|
|
37
|
+
[dependency-groups]
|
|
38
|
+
# Documentation tools: `pip install --group docs`
|
|
39
|
+
docs = [
|
|
40
|
+
"sphinx==9.2.0",
|
|
41
|
+
"sphinx-rtd-theme==3.1.0",
|
|
42
|
+
# docutils 0.22 is the newest that sphinx-rtd-theme accepts (<0.23)
|
|
43
|
+
"docutils==0.22.4",
|
|
44
|
+
"types-docutils==0.22.3.20260724", # types for docs/conf.py
|
|
45
|
+
]
|
|
46
|
+
# Development tools (lint, types, tests, packaging and documentation):
|
|
47
|
+
# `pip install --group dev`
|
|
48
|
+
dev = [
|
|
49
|
+
{ include-group = "docs" },
|
|
50
|
+
# runtime dependencies at their newest version, to test against them
|
|
51
|
+
"jmespath==1.1.0",
|
|
52
|
+
"jsonpath==0.82.2",
|
|
53
|
+
# lint, types and tests
|
|
54
|
+
"mypy==2.4.1",
|
|
55
|
+
"pyright==1.1.414",
|
|
56
|
+
"pytest==9.2.0",
|
|
57
|
+
"ruff==0.17.6",
|
|
58
|
+
"types-jmespath==1.1.0.20260724",
|
|
59
|
+
# build and check the distributions uploaded to PyPI
|
|
60
|
+
"build==1.6.1",
|
|
61
|
+
"twine==7.0.0",
|
|
62
|
+
]
|
|
63
|
+
|
|
64
|
+
[tool.setuptools.packages.find]
|
|
65
|
+
where = ["src"]
|
|
66
|
+
|
|
67
|
+
[tool.setuptools.package-data]
|
|
68
|
+
derafu_selector = ["py.typed"]
|
|
69
|
+
|
|
70
|
+
[tool.ruff]
|
|
71
|
+
include = ["**/*.py", "**/*.pyi"]
|
|
72
|
+
exclude = [".git", "__pycache__", "build", "dist", "venv"]
|
|
73
|
+
line-length = 79
|
|
74
|
+
# no target-version: Ruff takes the minimum version from requires-python, so
|
|
75
|
+
# it never suggests syntax that the oldest supported Python cannot run
|
|
76
|
+
respect-gitignore = true
|
|
77
|
+
fix = false
|
|
78
|
+
|
|
79
|
+
[tool.ruff.lint]
|
|
80
|
+
select = [
|
|
81
|
+
"D", # pydocstyle: docstrings
|
|
82
|
+
"F", # Pyflakes: code errors
|
|
83
|
+
"E", # pycodestyle: style
|
|
84
|
+
"W", # pycodestyle: warnings
|
|
85
|
+
"I", # isort: import order
|
|
86
|
+
"UP", # pyupgrade: modern syntax
|
|
87
|
+
"N", # pep8-naming: names
|
|
88
|
+
"B", # bugbear: common bug patterns
|
|
89
|
+
"A", # flake8-builtins: do not shadow builtins or stdlib names
|
|
90
|
+
"C90", # mccabe: complexity
|
|
91
|
+
"ANN", # flake8-annotations: type annotations
|
|
92
|
+
"ARG", # unused arguments
|
|
93
|
+
"DTZ", # timezone-aware dates
|
|
94
|
+
"TRY", # tryceratops: exception handling
|
|
95
|
+
"SLF", # private member access
|
|
96
|
+
"ERA", # commented-out code
|
|
97
|
+
"PL", # pylint
|
|
98
|
+
"PT", # pytest style
|
|
99
|
+
"SIM", # simplifications
|
|
100
|
+
"RET", # returns
|
|
101
|
+
"C4", # comprehensions
|
|
102
|
+
"PTH", # pathlib instead of os.path
|
|
103
|
+
"PIE", # miscellaneous
|
|
104
|
+
"RSE", # raise
|
|
105
|
+
"PERF", # performance
|
|
106
|
+
"FURB", # refurb: modern idioms
|
|
107
|
+
"PGH", # pygrep-hooks (forbids blanket ignores)
|
|
108
|
+
"RUF", # Ruff-specific rules
|
|
109
|
+
"FBT", # flake8-boolean-trap: boolean flags are keyword-only
|
|
110
|
+
"INP", # flake8-no-pep420: no implicit namespace packages
|
|
111
|
+
"PYI", # flake8-pyi: type stubs
|
|
112
|
+
# preview rules, enabled one by one (explicit-preview-rules)
|
|
113
|
+
"D421", # property docstrings do not start with a verb
|
|
114
|
+
"RUF052", # local dummy variables (_name) are not used
|
|
115
|
+
]
|
|
116
|
+
ignore = [
|
|
117
|
+
# Docstring style: Ruff cannot enforce both rules of each pair.
|
|
118
|
+
# - D211 is dropped in favor of D203 (blank line before a class
|
|
119
|
+
# docstring). The project does not use `ruff format`, which would
|
|
120
|
+
# remove that line.
|
|
121
|
+
# - D212 is dropped in favor of D213 (summary on the second line), the
|
|
122
|
+
# docstring style of the library.
|
|
123
|
+
"D211",
|
|
124
|
+
"D212",
|
|
125
|
+
]
|
|
126
|
+
|
|
127
|
+
# preview rules only apply when listed by code in `select`
|
|
128
|
+
preview = true
|
|
129
|
+
explicit-preview-rules = true
|
|
130
|
+
|
|
131
|
+
[tool.ruff.lint.mccabe]
|
|
132
|
+
max-complexity = 25
|
|
133
|
+
|
|
134
|
+
[tool.ruff.lint.pylint]
|
|
135
|
+
max-args = 10
|
|
136
|
+
max-branches = 25
|
|
137
|
+
max-statements = 60
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
[tool.mypy]
|
|
141
|
+
# no python_version: mypy checks against the running interpreter, so each CI
|
|
142
|
+
# job (3.13, 3.14 and 3.15) checks its own version
|
|
143
|
+
strict = true
|
|
144
|
+
# methods that override a parent method must use @override
|
|
145
|
+
enable_error_code = ["explicit-override"]
|
|
146
|
+
files = ["src", "tests", "docs"]
|
|
147
|
+
# own type stubs for jsonpath, which ships without types
|
|
148
|
+
mypy_path = "typings"
|
|
149
|
+
|
|
150
|
+
[tool.pyright]
|
|
151
|
+
include = ["src", "tests", "docs"]
|
|
152
|
+
exclude = ["**/__pycache__", "build", "dist", "venv"]
|
|
153
|
+
# no pythonVersion: `make typecheck` passes the venv interpreter
|
|
154
|
+
# (--pythonpath), so pyright checks against its version
|
|
155
|
+
typeCheckingMode = "strict"
|
|
156
|
+
# own type stubs for jsonpath (default stubPath: typings)
|
|
157
|
+
venvPath = "."
|
|
158
|
+
venv = "venv"
|
|
159
|
+
|
|
160
|
+
[tool.pytest.ini_options]
|
|
161
|
+
testpaths = ["tests"]
|
|
162
|
+
addopts = "-ra --strict-markers --strict-config"
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Read and write dictionaries using selectors.
|
|
3
|
+
|
|
4
|
+
Usage: ``from derafu_selector import selector``. The selector syntax is
|
|
5
|
+
described in the docstring of ``derafu_selector.selector``.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from derafu_selector.selector import (
|
|
9
|
+
NotADictionaryError,
|
|
10
|
+
NotAssignableError,
|
|
11
|
+
NotIterableError,
|
|
12
|
+
UnsupportedOperatorError,
|
|
13
|
+
dictionary_read_case_insensitive,
|
|
14
|
+
selector,
|
|
15
|
+
)
|
|
16
|
+
|
|
17
|
+
__all__ = [
|
|
18
|
+
'NotADictionaryError',
|
|
19
|
+
'NotAssignableError',
|
|
20
|
+
'NotIterableError',
|
|
21
|
+
'UnsupportedOperatorError',
|
|
22
|
+
'dictionary_read_case_insensitive',
|
|
23
|
+
'selector',
|
|
24
|
+
]
|
|
File without changes
|