Extended dict with the capability of accessing and manipulating nested itens using a path notation.
Installation
pip install pathdict
Examples
You can change your chained multiple access operator with a dotted path notation:
from pathdict import PathDict
path_dict = PathDict({
'dogs': {
'male': 'Xablau',
'female': 'Xena'
},
'enterprises': ['B2W', 'Sieve'],
'author': '@diogommartins'
})
# Same as path_dict['dogs']['female']
print(path_dict['dogs.female'])
>>> 'Xena'
Lists are also accessible by the path notation:
from pathdict import PathDict
path_dict = PathDict({
'mammalia': {
'ferae': {
'carnivora': [
'carnidae',
'felidae',
'mustelidae',
'ursidae',
'viverridae'
]
},
'something': {
'wiki': 'pedia'
}
}
})
# Same as path_dict['mammalia']['ferae']['carnivora'][3]
print(path_dict['mammalia.ferae.carnivora.3'])
>>> 'ursidae'
Everything you do with a normal python dict will work.
Customization
The default separator is . but you can change it using the separator kwarg.
from pathdict import PathDict
path_dict = PathDict({
'mammalia': {
'ferae': {
'carnivora': [
'carnidae',
'felidae',
'mustelidae',
'ursidae',
'viverridae'
]
},
'something': {
'wiki': 'pedia'
}
}
}, separator='/')
print(path_dict['mammalia/ferae/carnivora/3'])
>>> 'ursidae'
It is possible to let PathDict generate the steps to a given path:
from pathdict import PathDict
path_dict = PathDict(create_if_not_exists=True)
path_dict['the.answer.to.all.problems'] = 42
print(path_dict)
>>> {'the': {'answer': {'to': {'all': {'problems': 42}}}}}
Which also works for nested list fields:
from pathdict import PathDict
path_dict = PathDict(create_if_not_exists=True)
path_dict['some.letters'] = ['a', 'b']
path_dict['some.letters.2'] = 'c'
print(path_dict)
>>> {'some': {'letters': ['a', 'b', 'c']}}
By default, list values inside a PathDict are type casted to a pathdict.collection.StringIndexableList. You can change that behavior with the list_class keyword argument. Beware that if the list_class class doesn’t implement the StringIndexableList Protocol, pathdict’s lists path will fail.
from pathdict import PathDict
path_dict = PathDict()
path_dict["list"] = [1, 2, 3]
type(path_dict["list"])
>>> pathdict.collection.StringIndexableList
path_dict["list.1"]
>>> 2
custom_path_dict = PathDict(list_class=list)
type(custom_path_dict["list"])
>>> list
custom_path_dict["list.1"]
>>> TypeError: list indices must be integers or slices, not str
Metadata
Release files for pathdict 0.0.9
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 | |
|---|---|---|---|---|
| pathdict-0.0.9.tar.gz | 3.1 kB | Without provenance | Details |
Release files / pathdict-0.0.9.tar.gz
| Download URL | pathdict-0.0.9.tar.gz |
|---|---|
| Size | 3.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
7fa8c111b5d0b47396841d4515a1a3c98823fb84fffc8172e08d157348621e8c
|
|
BLAKE2b-256 checksum How to use checksums |
9770bcf4f65183c6f863d7e21332f93adf02c9935db41302c6fc7a960787ac50
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Security for pathdict 0.0.9
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.