digin 0.4.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.
- digin-0.4.0/LICENSE +21 -0
- digin-0.4.0/PKG-INFO +352 -0
- digin-0.4.0/README.md +337 -0
- digin-0.4.0/digin/__init__.py +3 -0
- digin-0.4.0/digin/nests.py +163 -0
- digin-0.4.0/digin.egg-info/PKG-INFO +352 -0
- digin-0.4.0/digin.egg-info/SOURCES.txt +10 -0
- digin-0.4.0/digin.egg-info/dependency_links.txt +1 -0
- digin-0.4.0/digin.egg-info/top_level.txt +1 -0
- digin-0.4.0/pyproject.toml +22 -0
- digin-0.4.0/setup.cfg +4 -0
- digin-0.4.0/tests/tests.py +263 -0
digin-0.4.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 Ijaz Ur Rahim
|
|
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.
|
digin-0.4.0/PKG-INFO
ADDED
|
@@ -0,0 +1,352 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: digin
|
|
3
|
+
Version: 0.4.0
|
|
4
|
+
Summary: Dig into nested data structures using delimiter-separated key paths
|
|
5
|
+
Author-email: Ijaz Ur Rahim <ijazkhan095@gmail.com>
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/MrDebugger/digin
|
|
8
|
+
Keywords: nested,dict,list,tuple,json,access,delimiter,path
|
|
9
|
+
Classifier: Programming Language :: Python :: 3
|
|
10
|
+
Classifier: Operating System :: OS Independent
|
|
11
|
+
Requires-Python: >=3.8
|
|
12
|
+
Description-Content-Type: text/markdown
|
|
13
|
+
License-File: LICENSE
|
|
14
|
+
Dynamic: license-file
|
|
15
|
+
|
|
16
|
+
[](https://pypi.python.org/pypi/digin/)
|
|
17
|
+
[](https://pypi.python.org/pypi/digin/)
|
|
18
|
+
[](https://pypi.python.org/pypi/digin/)
|
|
19
|
+
[](https://pypi.python.org/pypi/digin/)
|
|
20
|
+
[](https://github.com/MrDebugger/digin/stargazers)
|
|
21
|
+
[](https://github.com/MrDebugger/digin/issues)
|
|
22
|
+
[](https://github.com/MrDebugger/digin/commits)
|
|
23
|
+
|
|
24
|
+
<h1 align="center">digin</h1>
|
|
25
|
+
|
|
26
|
+
<div align="center">
|
|
27
|
+
|
|
28
|
+
Access and modify deeply nested data structures using simple delimiter-separated paths.
|
|
29
|
+
No more chaining `[]` operators — just `"a->b->0"`.
|
|
30
|
+
|
|
31
|
+
**Python 3.8+** | Zero dependencies
|
|
32
|
+
|
|
33
|
+
</div>
|
|
34
|
+
|
|
35
|
+
---
|
|
36
|
+
|
|
37
|
+
<details open>
|
|
38
|
+
<summary><b>Table of Contents</b></summary>
|
|
39
|
+
<br>
|
|
40
|
+
|
|
41
|
+
| Section | Description |
|
|
42
|
+
|---------|-------------|
|
|
43
|
+
| [Installation](#installation) | How to install |
|
|
44
|
+
| [Quick Start](#quick-start) | Basic usage |
|
|
45
|
+
| [Access Methods](#access-methods) | get, [], dot notation, callable |
|
|
46
|
+
| [Modification](#modification) | set, []=, dot notation, callable |
|
|
47
|
+
| [Options](#options) | Delimiters, defaults, modify flag |
|
|
48
|
+
| [Classes](#classes) | NestedDict, NestedList, NestedTuple |
|
|
49
|
+
| [API Reference](#api-reference) | Full method reference |
|
|
50
|
+
| [Contributing](#contributing) | How to contribute |
|
|
51
|
+
|
|
52
|
+
</details>
|
|
53
|
+
|
|
54
|
+
---
|
|
55
|
+
|
|
56
|
+
## Installation
|
|
57
|
+
|
|
58
|
+
```bash
|
|
59
|
+
pip install -U digin
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
---
|
|
63
|
+
|
|
64
|
+
## Quick Start
|
|
65
|
+
|
|
66
|
+
```python
|
|
67
|
+
from digin import NestedDict
|
|
68
|
+
|
|
69
|
+
data = {
|
|
70
|
+
"users": {
|
|
71
|
+
"alice": {"age": 30, "hobbies": ["reading", "coding"]},
|
|
72
|
+
"bob": {"age": 25, "hobbies": ["gaming", "cooking"]}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
nd = NestedDict(data)
|
|
77
|
+
|
|
78
|
+
# Read nested values
|
|
79
|
+
nd.get("users->alice->age") # 30
|
|
80
|
+
nd["users->bob->hobbies->0"] # 'gaming'
|
|
81
|
+
nd.users.alice.hobbies # NestedList(['reading', 'coding'])
|
|
82
|
+
|
|
83
|
+
# Modify nested values
|
|
84
|
+
nd.set("users->alice->age", 31)
|
|
85
|
+
nd["users->bob->hobbies->1"] = "hiking"
|
|
86
|
+
nd.users.bob.age = 26
|
|
87
|
+
nd("users->alice->hobbies->0", "writing") # set via callable
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
---
|
|
91
|
+
|
|
92
|
+
## Access Methods
|
|
93
|
+
|
|
94
|
+
<details open>
|
|
95
|
+
<summary><b>get()</b></summary>
|
|
96
|
+
<br>
|
|
97
|
+
|
|
98
|
+
```python
|
|
99
|
+
nd = NestedDict(data)
|
|
100
|
+
|
|
101
|
+
# String path with delimiter
|
|
102
|
+
nd.get("users->alice->age") # 30
|
|
103
|
+
|
|
104
|
+
# List/tuple of keys
|
|
105
|
+
nd.get(["users", "alice", "age"]) # 30
|
|
106
|
+
nd.get(("users", "alice", "hobbies", 0)) # 'reading'
|
|
107
|
+
|
|
108
|
+
# Integer key (for lists)
|
|
109
|
+
nl = NestedList([10, 20, 30])
|
|
110
|
+
nl.get(1) # 20
|
|
111
|
+
|
|
112
|
+
# Default value if not found
|
|
113
|
+
nd.get("users->charlie->age", default=0) # 0
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
</details>
|
|
117
|
+
|
|
118
|
+
<details open>
|
|
119
|
+
<summary><b>[] bracket notation</b></summary>
|
|
120
|
+
<br>
|
|
121
|
+
|
|
122
|
+
```python
|
|
123
|
+
nd["users->alice->age"] # 30
|
|
124
|
+
nd[["users", "bob", "hobbies"]] # NestedList(['gaming', 'cooking'])
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
</details>
|
|
128
|
+
|
|
129
|
+
<details>
|
|
130
|
+
<summary><b>Dot notation</b></summary>
|
|
131
|
+
<br>
|
|
132
|
+
|
|
133
|
+
```python
|
|
134
|
+
nd.users.alice.age # 30
|
|
135
|
+
nd.users.bob.hobbies # NestedList(['gaming', 'cooking'])
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
Note: dot notation only works with string keys that are valid Python identifiers.
|
|
139
|
+
|
|
140
|
+
</details>
|
|
141
|
+
|
|
142
|
+
<details>
|
|
143
|
+
<summary><b>Callable</b></summary>
|
|
144
|
+
<br>
|
|
145
|
+
|
|
146
|
+
```python
|
|
147
|
+
# Get value
|
|
148
|
+
nd("users->alice->age") # 30
|
|
149
|
+
|
|
150
|
+
# Get with default
|
|
151
|
+
nd("users->charlie->age", default="N/A") # 'N/A'
|
|
152
|
+
|
|
153
|
+
# Set value (pass value as second argument)
|
|
154
|
+
nd("users->alice->age", 31) # sets age to 31
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
</details>
|
|
158
|
+
|
|
159
|
+
---
|
|
160
|
+
|
|
161
|
+
## Modification
|
|
162
|
+
|
|
163
|
+
<details open>
|
|
164
|
+
<summary><b>set()</b></summary>
|
|
165
|
+
<br>
|
|
166
|
+
|
|
167
|
+
```python
|
|
168
|
+
nd.set("users->alice->age", 31)
|
|
169
|
+
nd.set(["users", "bob", "age"], 26)
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
</details>
|
|
173
|
+
|
|
174
|
+
<details open>
|
|
175
|
+
<summary><b>[]= bracket assignment</b></summary>
|
|
176
|
+
<br>
|
|
177
|
+
|
|
178
|
+
```python
|
|
179
|
+
nd["users->alice->age"] = 31
|
|
180
|
+
nd[["users", "bob", "age"]] = 26
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
</details>
|
|
184
|
+
|
|
185
|
+
<details>
|
|
186
|
+
<summary><b>Dot notation assignment</b></summary>
|
|
187
|
+
<br>
|
|
188
|
+
|
|
189
|
+
```python
|
|
190
|
+
nd.users.alice.age = 31
|
|
191
|
+
nd.users.bob.age = 26
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
</details>
|
|
195
|
+
|
|
196
|
+
<details>
|
|
197
|
+
<summary><b>Callable assignment</b></summary>
|
|
198
|
+
<br>
|
|
199
|
+
|
|
200
|
+
```python
|
|
201
|
+
nd("users->alice->age", 31)
|
|
202
|
+
nd("users->bob->hobbies->0", "swimming")
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
Works with falsy values too — `0`, `""`, `False`, `[]`, `{}` are all valid:
|
|
206
|
+
|
|
207
|
+
```python
|
|
208
|
+
nd("users->alice->active", False) # sets to False, not a get
|
|
209
|
+
nd("users->alice->score", 0) # sets to 0
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
</details>
|
|
213
|
+
|
|
214
|
+
---
|
|
215
|
+
|
|
216
|
+
## Options
|
|
217
|
+
|
|
218
|
+
<details open>
|
|
219
|
+
<summary><b>Custom Delimiter</b></summary>
|
|
220
|
+
<br>
|
|
221
|
+
|
|
222
|
+
Default delimiter is `"->"`. Change it in the constructor:
|
|
223
|
+
|
|
224
|
+
```python
|
|
225
|
+
nd = NestedDict(data, delimiter=".")
|
|
226
|
+
nd.get("users.alice.age") # 30
|
|
227
|
+
nd["users.bob.hobbies.0"] # 'gaming'
|
|
228
|
+
|
|
229
|
+
nd2 = NestedDict(data, delimiter="/")
|
|
230
|
+
nd2.get("users/alice/age") # 30
|
|
231
|
+
```
|
|
232
|
+
|
|
233
|
+
</details>
|
|
234
|
+
|
|
235
|
+
<details>
|
|
236
|
+
<summary><b>Modify Flag</b></summary>
|
|
237
|
+
<br>
|
|
238
|
+
|
|
239
|
+
By default, `get()` wraps dicts/lists/tuples in Nested objects. Use `modify=True` to get the raw value:
|
|
240
|
+
|
|
241
|
+
```python
|
|
242
|
+
# Returns NestedDict (wrapped)
|
|
243
|
+
nd.get("users->alice")
|
|
244
|
+
# NestedDict({'age': 30, 'hobbies': ['reading', 'coding']})
|
|
245
|
+
|
|
246
|
+
# Returns plain dict (raw)
|
|
247
|
+
nd.get("users->alice", modify=True)
|
|
248
|
+
# {'age': 30, 'hobbies': ['reading', 'coding']}
|
|
249
|
+
```
|
|
250
|
+
|
|
251
|
+
</details>
|
|
252
|
+
|
|
253
|
+
---
|
|
254
|
+
|
|
255
|
+
## Classes
|
|
256
|
+
|
|
257
|
+
<details open>
|
|
258
|
+
<summary><b>NestedDict</b></summary>
|
|
259
|
+
<br>
|
|
260
|
+
|
|
261
|
+
Extends `dict` with nested path access. Supports all dict methods plus nested get/set:
|
|
262
|
+
|
|
263
|
+
```python
|
|
264
|
+
from digin import NestedDict
|
|
265
|
+
|
|
266
|
+
nd = NestedDict({"a": {"b": {"c": 1}}})
|
|
267
|
+
nd.get("a->b->c") # 1
|
|
268
|
+
nd.keys() # dict_keys(['a'])
|
|
269
|
+
len(nd) # 1
|
|
270
|
+
```
|
|
271
|
+
|
|
272
|
+
</details>
|
|
273
|
+
|
|
274
|
+
<details>
|
|
275
|
+
<summary><b>NestedList</b></summary>
|
|
276
|
+
<br>
|
|
277
|
+
|
|
278
|
+
Extends `list` with nested path access:
|
|
279
|
+
|
|
280
|
+
```python
|
|
281
|
+
from digin import NestedList
|
|
282
|
+
|
|
283
|
+
nl = NestedList([{"name": "Alice"}, {"name": "Bob"}])
|
|
284
|
+
nl.get("0->name") # 'Alice'
|
|
285
|
+
nl[1] # {'name': 'Bob'}
|
|
286
|
+
len(nl) # 2
|
|
287
|
+
```
|
|
288
|
+
|
|
289
|
+
</details>
|
|
290
|
+
|
|
291
|
+
<details>
|
|
292
|
+
<summary><b>NestedTuple</b></summary>
|
|
293
|
+
<br>
|
|
294
|
+
|
|
295
|
+
Extends `tuple` with nested path access (read-only):
|
|
296
|
+
|
|
297
|
+
```python
|
|
298
|
+
from digin import NestedTuple
|
|
299
|
+
|
|
300
|
+
nt = NestedTuple(({"a": 1}, {"b": 2}))
|
|
301
|
+
nt.get("0->a") # 1
|
|
302
|
+
nt[0] # {'a': 1}
|
|
303
|
+
```
|
|
304
|
+
|
|
305
|
+
</details>
|
|
306
|
+
|
|
307
|
+
---
|
|
308
|
+
|
|
309
|
+
## API Reference
|
|
310
|
+
|
|
311
|
+
<details open>
|
|
312
|
+
<summary><b>Nested (base class)</b></summary>
|
|
313
|
+
<br>
|
|
314
|
+
|
|
315
|
+
| Method | Description |
|
|
316
|
+
|--------|-------------|
|
|
317
|
+
| `__init__(data, delimiter="->")` | Wrap data with given delimiter |
|
|
318
|
+
| `.get(key, default, modify=False)` | Get value at key path |
|
|
319
|
+
| `.set(key, value)` | Set value at key path |
|
|
320
|
+
| `.parse(value, modify=False)` | Wrap value in Nested subclass |
|
|
321
|
+
| `(key)` | Get value (callable) |
|
|
322
|
+
| `(key, value)` | Set value (callable) |
|
|
323
|
+
| `[key]` | Get value (bracket) |
|
|
324
|
+
| `[key] = value` | Set value (bracket) |
|
|
325
|
+
| `.key` | Get value (dot notation) |
|
|
326
|
+
| `.key = value` | Set value (dot notation) |
|
|
327
|
+
|
|
328
|
+
</details>
|
|
329
|
+
|
|
330
|
+
<details open>
|
|
331
|
+
<summary><b>Key formats</b></summary>
|
|
332
|
+
<br>
|
|
333
|
+
|
|
334
|
+
| Format | Example | Description |
|
|
335
|
+
|--------|---------|-------------|
|
|
336
|
+
| String | `"a->b->0"` | Delimiter-separated path |
|
|
337
|
+
| List | `["a", "b", 0]` | List of keys |
|
|
338
|
+
| Tuple | `("a", "b", 0)` | Tuple of keys |
|
|
339
|
+
| Int | `0` | Direct index |
|
|
340
|
+
| None | `None` | Returns self |
|
|
341
|
+
|
|
342
|
+
</details>
|
|
343
|
+
|
|
344
|
+
---
|
|
345
|
+
|
|
346
|
+
## Contributing
|
|
347
|
+
|
|
348
|
+
See [CONTRIBUTING.md](CONTRIBUTING.md) for development setup, versioning guide, and how to submit changes.
|
|
349
|
+
|
|
350
|
+
<a href="https://github.com/MrDebugger/digin/graphs/contributors">
|
|
351
|
+
<img src="https://contrib.rocks/image?repo=MrDebugger/digin"/>
|
|
352
|
+
</a>
|
digin-0.4.0/README.md
ADDED
|
@@ -0,0 +1,337 @@
|
|
|
1
|
+
[](https://pypi.python.org/pypi/digin/)
|
|
2
|
+
[](https://pypi.python.org/pypi/digin/)
|
|
3
|
+
[](https://pypi.python.org/pypi/digin/)
|
|
4
|
+
[](https://pypi.python.org/pypi/digin/)
|
|
5
|
+
[](https://github.com/MrDebugger/digin/stargazers)
|
|
6
|
+
[](https://github.com/MrDebugger/digin/issues)
|
|
7
|
+
[](https://github.com/MrDebugger/digin/commits)
|
|
8
|
+
|
|
9
|
+
<h1 align="center">digin</h1>
|
|
10
|
+
|
|
11
|
+
<div align="center">
|
|
12
|
+
|
|
13
|
+
Access and modify deeply nested data structures using simple delimiter-separated paths.
|
|
14
|
+
No more chaining `[]` operators — just `"a->b->0"`.
|
|
15
|
+
|
|
16
|
+
**Python 3.8+** | Zero dependencies
|
|
17
|
+
|
|
18
|
+
</div>
|
|
19
|
+
|
|
20
|
+
---
|
|
21
|
+
|
|
22
|
+
<details open>
|
|
23
|
+
<summary><b>Table of Contents</b></summary>
|
|
24
|
+
<br>
|
|
25
|
+
|
|
26
|
+
| Section | Description |
|
|
27
|
+
|---------|-------------|
|
|
28
|
+
| [Installation](#installation) | How to install |
|
|
29
|
+
| [Quick Start](#quick-start) | Basic usage |
|
|
30
|
+
| [Access Methods](#access-methods) | get, [], dot notation, callable |
|
|
31
|
+
| [Modification](#modification) | set, []=, dot notation, callable |
|
|
32
|
+
| [Options](#options) | Delimiters, defaults, modify flag |
|
|
33
|
+
| [Classes](#classes) | NestedDict, NestedList, NestedTuple |
|
|
34
|
+
| [API Reference](#api-reference) | Full method reference |
|
|
35
|
+
| [Contributing](#contributing) | How to contribute |
|
|
36
|
+
|
|
37
|
+
</details>
|
|
38
|
+
|
|
39
|
+
---
|
|
40
|
+
|
|
41
|
+
## Installation
|
|
42
|
+
|
|
43
|
+
```bash
|
|
44
|
+
pip install -U digin
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
---
|
|
48
|
+
|
|
49
|
+
## Quick Start
|
|
50
|
+
|
|
51
|
+
```python
|
|
52
|
+
from digin import NestedDict
|
|
53
|
+
|
|
54
|
+
data = {
|
|
55
|
+
"users": {
|
|
56
|
+
"alice": {"age": 30, "hobbies": ["reading", "coding"]},
|
|
57
|
+
"bob": {"age": 25, "hobbies": ["gaming", "cooking"]}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
nd = NestedDict(data)
|
|
62
|
+
|
|
63
|
+
# Read nested values
|
|
64
|
+
nd.get("users->alice->age") # 30
|
|
65
|
+
nd["users->bob->hobbies->0"] # 'gaming'
|
|
66
|
+
nd.users.alice.hobbies # NestedList(['reading', 'coding'])
|
|
67
|
+
|
|
68
|
+
# Modify nested values
|
|
69
|
+
nd.set("users->alice->age", 31)
|
|
70
|
+
nd["users->bob->hobbies->1"] = "hiking"
|
|
71
|
+
nd.users.bob.age = 26
|
|
72
|
+
nd("users->alice->hobbies->0", "writing") # set via callable
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
---
|
|
76
|
+
|
|
77
|
+
## Access Methods
|
|
78
|
+
|
|
79
|
+
<details open>
|
|
80
|
+
<summary><b>get()</b></summary>
|
|
81
|
+
<br>
|
|
82
|
+
|
|
83
|
+
```python
|
|
84
|
+
nd = NestedDict(data)
|
|
85
|
+
|
|
86
|
+
# String path with delimiter
|
|
87
|
+
nd.get("users->alice->age") # 30
|
|
88
|
+
|
|
89
|
+
# List/tuple of keys
|
|
90
|
+
nd.get(["users", "alice", "age"]) # 30
|
|
91
|
+
nd.get(("users", "alice", "hobbies", 0)) # 'reading'
|
|
92
|
+
|
|
93
|
+
# Integer key (for lists)
|
|
94
|
+
nl = NestedList([10, 20, 30])
|
|
95
|
+
nl.get(1) # 20
|
|
96
|
+
|
|
97
|
+
# Default value if not found
|
|
98
|
+
nd.get("users->charlie->age", default=0) # 0
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
</details>
|
|
102
|
+
|
|
103
|
+
<details open>
|
|
104
|
+
<summary><b>[] bracket notation</b></summary>
|
|
105
|
+
<br>
|
|
106
|
+
|
|
107
|
+
```python
|
|
108
|
+
nd["users->alice->age"] # 30
|
|
109
|
+
nd[["users", "bob", "hobbies"]] # NestedList(['gaming', 'cooking'])
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
</details>
|
|
113
|
+
|
|
114
|
+
<details>
|
|
115
|
+
<summary><b>Dot notation</b></summary>
|
|
116
|
+
<br>
|
|
117
|
+
|
|
118
|
+
```python
|
|
119
|
+
nd.users.alice.age # 30
|
|
120
|
+
nd.users.bob.hobbies # NestedList(['gaming', 'cooking'])
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
Note: dot notation only works with string keys that are valid Python identifiers.
|
|
124
|
+
|
|
125
|
+
</details>
|
|
126
|
+
|
|
127
|
+
<details>
|
|
128
|
+
<summary><b>Callable</b></summary>
|
|
129
|
+
<br>
|
|
130
|
+
|
|
131
|
+
```python
|
|
132
|
+
# Get value
|
|
133
|
+
nd("users->alice->age") # 30
|
|
134
|
+
|
|
135
|
+
# Get with default
|
|
136
|
+
nd("users->charlie->age", default="N/A") # 'N/A'
|
|
137
|
+
|
|
138
|
+
# Set value (pass value as second argument)
|
|
139
|
+
nd("users->alice->age", 31) # sets age to 31
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
</details>
|
|
143
|
+
|
|
144
|
+
---
|
|
145
|
+
|
|
146
|
+
## Modification
|
|
147
|
+
|
|
148
|
+
<details open>
|
|
149
|
+
<summary><b>set()</b></summary>
|
|
150
|
+
<br>
|
|
151
|
+
|
|
152
|
+
```python
|
|
153
|
+
nd.set("users->alice->age", 31)
|
|
154
|
+
nd.set(["users", "bob", "age"], 26)
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
</details>
|
|
158
|
+
|
|
159
|
+
<details open>
|
|
160
|
+
<summary><b>[]= bracket assignment</b></summary>
|
|
161
|
+
<br>
|
|
162
|
+
|
|
163
|
+
```python
|
|
164
|
+
nd["users->alice->age"] = 31
|
|
165
|
+
nd[["users", "bob", "age"]] = 26
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
</details>
|
|
169
|
+
|
|
170
|
+
<details>
|
|
171
|
+
<summary><b>Dot notation assignment</b></summary>
|
|
172
|
+
<br>
|
|
173
|
+
|
|
174
|
+
```python
|
|
175
|
+
nd.users.alice.age = 31
|
|
176
|
+
nd.users.bob.age = 26
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
</details>
|
|
180
|
+
|
|
181
|
+
<details>
|
|
182
|
+
<summary><b>Callable assignment</b></summary>
|
|
183
|
+
<br>
|
|
184
|
+
|
|
185
|
+
```python
|
|
186
|
+
nd("users->alice->age", 31)
|
|
187
|
+
nd("users->bob->hobbies->0", "swimming")
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
Works with falsy values too — `0`, `""`, `False`, `[]`, `{}` are all valid:
|
|
191
|
+
|
|
192
|
+
```python
|
|
193
|
+
nd("users->alice->active", False) # sets to False, not a get
|
|
194
|
+
nd("users->alice->score", 0) # sets to 0
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
</details>
|
|
198
|
+
|
|
199
|
+
---
|
|
200
|
+
|
|
201
|
+
## Options
|
|
202
|
+
|
|
203
|
+
<details open>
|
|
204
|
+
<summary><b>Custom Delimiter</b></summary>
|
|
205
|
+
<br>
|
|
206
|
+
|
|
207
|
+
Default delimiter is `"->"`. Change it in the constructor:
|
|
208
|
+
|
|
209
|
+
```python
|
|
210
|
+
nd = NestedDict(data, delimiter=".")
|
|
211
|
+
nd.get("users.alice.age") # 30
|
|
212
|
+
nd["users.bob.hobbies.0"] # 'gaming'
|
|
213
|
+
|
|
214
|
+
nd2 = NestedDict(data, delimiter="/")
|
|
215
|
+
nd2.get("users/alice/age") # 30
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
</details>
|
|
219
|
+
|
|
220
|
+
<details>
|
|
221
|
+
<summary><b>Modify Flag</b></summary>
|
|
222
|
+
<br>
|
|
223
|
+
|
|
224
|
+
By default, `get()` wraps dicts/lists/tuples in Nested objects. Use `modify=True` to get the raw value:
|
|
225
|
+
|
|
226
|
+
```python
|
|
227
|
+
# Returns NestedDict (wrapped)
|
|
228
|
+
nd.get("users->alice")
|
|
229
|
+
# NestedDict({'age': 30, 'hobbies': ['reading', 'coding']})
|
|
230
|
+
|
|
231
|
+
# Returns plain dict (raw)
|
|
232
|
+
nd.get("users->alice", modify=True)
|
|
233
|
+
# {'age': 30, 'hobbies': ['reading', 'coding']}
|
|
234
|
+
```
|
|
235
|
+
|
|
236
|
+
</details>
|
|
237
|
+
|
|
238
|
+
---
|
|
239
|
+
|
|
240
|
+
## Classes
|
|
241
|
+
|
|
242
|
+
<details open>
|
|
243
|
+
<summary><b>NestedDict</b></summary>
|
|
244
|
+
<br>
|
|
245
|
+
|
|
246
|
+
Extends `dict` with nested path access. Supports all dict methods plus nested get/set:
|
|
247
|
+
|
|
248
|
+
```python
|
|
249
|
+
from digin import NestedDict
|
|
250
|
+
|
|
251
|
+
nd = NestedDict({"a": {"b": {"c": 1}}})
|
|
252
|
+
nd.get("a->b->c") # 1
|
|
253
|
+
nd.keys() # dict_keys(['a'])
|
|
254
|
+
len(nd) # 1
|
|
255
|
+
```
|
|
256
|
+
|
|
257
|
+
</details>
|
|
258
|
+
|
|
259
|
+
<details>
|
|
260
|
+
<summary><b>NestedList</b></summary>
|
|
261
|
+
<br>
|
|
262
|
+
|
|
263
|
+
Extends `list` with nested path access:
|
|
264
|
+
|
|
265
|
+
```python
|
|
266
|
+
from digin import NestedList
|
|
267
|
+
|
|
268
|
+
nl = NestedList([{"name": "Alice"}, {"name": "Bob"}])
|
|
269
|
+
nl.get("0->name") # 'Alice'
|
|
270
|
+
nl[1] # {'name': 'Bob'}
|
|
271
|
+
len(nl) # 2
|
|
272
|
+
```
|
|
273
|
+
|
|
274
|
+
</details>
|
|
275
|
+
|
|
276
|
+
<details>
|
|
277
|
+
<summary><b>NestedTuple</b></summary>
|
|
278
|
+
<br>
|
|
279
|
+
|
|
280
|
+
Extends `tuple` with nested path access (read-only):
|
|
281
|
+
|
|
282
|
+
```python
|
|
283
|
+
from digin import NestedTuple
|
|
284
|
+
|
|
285
|
+
nt = NestedTuple(({"a": 1}, {"b": 2}))
|
|
286
|
+
nt.get("0->a") # 1
|
|
287
|
+
nt[0] # {'a': 1}
|
|
288
|
+
```
|
|
289
|
+
|
|
290
|
+
</details>
|
|
291
|
+
|
|
292
|
+
---
|
|
293
|
+
|
|
294
|
+
## API Reference
|
|
295
|
+
|
|
296
|
+
<details open>
|
|
297
|
+
<summary><b>Nested (base class)</b></summary>
|
|
298
|
+
<br>
|
|
299
|
+
|
|
300
|
+
| Method | Description |
|
|
301
|
+
|--------|-------------|
|
|
302
|
+
| `__init__(data, delimiter="->")` | Wrap data with given delimiter |
|
|
303
|
+
| `.get(key, default, modify=False)` | Get value at key path |
|
|
304
|
+
| `.set(key, value)` | Set value at key path |
|
|
305
|
+
| `.parse(value, modify=False)` | Wrap value in Nested subclass |
|
|
306
|
+
| `(key)` | Get value (callable) |
|
|
307
|
+
| `(key, value)` | Set value (callable) |
|
|
308
|
+
| `[key]` | Get value (bracket) |
|
|
309
|
+
| `[key] = value` | Set value (bracket) |
|
|
310
|
+
| `.key` | Get value (dot notation) |
|
|
311
|
+
| `.key = value` | Set value (dot notation) |
|
|
312
|
+
|
|
313
|
+
</details>
|
|
314
|
+
|
|
315
|
+
<details open>
|
|
316
|
+
<summary><b>Key formats</b></summary>
|
|
317
|
+
<br>
|
|
318
|
+
|
|
319
|
+
| Format | Example | Description |
|
|
320
|
+
|--------|---------|-------------|
|
|
321
|
+
| String | `"a->b->0"` | Delimiter-separated path |
|
|
322
|
+
| List | `["a", "b", 0]` | List of keys |
|
|
323
|
+
| Tuple | `("a", "b", 0)` | Tuple of keys |
|
|
324
|
+
| Int | `0` | Direct index |
|
|
325
|
+
| None | `None` | Returns self |
|
|
326
|
+
|
|
327
|
+
</details>
|
|
328
|
+
|
|
329
|
+
---
|
|
330
|
+
|
|
331
|
+
## Contributing
|
|
332
|
+
|
|
333
|
+
See [CONTRIBUTING.md](CONTRIBUTING.md) for development setup, versioning guide, and how to submit changes.
|
|
334
|
+
|
|
335
|
+
<a href="https://github.com/MrDebugger/digin/graphs/contributors">
|
|
336
|
+
<img src="https://contrib.rocks/image?repo=MrDebugger/digin"/>
|
|
337
|
+
</a>
|