Usage
When working with deeply nested dictionaries, handling missing keys can be cumbersome. Normally, you would need to catch errors or provide default values when a key does not exist.
FlexibleDict, which is a redefined version of Python’s built-in dictionary, eliminates the need for explicit error handling. You can chain multiple key lookups without worrying about whether each key exists. If a key is missing, a predefined default value is returned instead of raising an error.
An important feature is that the default value itself is also a FlexibleDict, meaning you can continue chaining key lookups without interruptions. Once the entire key chain has been traversed, if a missing key was encountered along the way, the predefined default value will be returned.
An example of how to use this class can be found in the how_to_use.py file included in this package. Below, we explain how to create a FlexibleDict object and the available methods for accessing values.
Creating a FlexibleDict
my_flexible_dict = FlexibleDict(input_dict=a_simple_dict, default=..., iterable_default=...)
input_dict: a regular Python dictionary that will be wrapped byFlexibleDict.default: the value to return whenever a missing key is accessed.iterable_default: a special default value used when an iterable is expected (explained below).
Available Methods (Reading Data)
To read nested keys, simply chain them one after another. There are three main ways to retrieve values:
1. .value
Use .value to get the plain Python value.
- If the keys exist, the actual value is returned.
- If not, the predefined
defaultvalue is returned.
Note: the returned value is always a standard Python type. For example, if it’s a dictionary, it will be a regular
dict, not aFlexibleDict.
This is the most common way to read values for everyday use.
2. .flexible_value
Use .flexible_value if you want the result to remain a FlexibleDict, allowing you to continue chaining key lookups even after missing keys.
- This is useful if you want to traverse further without worrying about whether the intermediate values exist.
To finally extract a standard Python type, you should still use .value.
3. .iterable_value
When iterating with for loops, you need to ensure the value is iterable.
.iterable_valueguarantees that behavior.- If the result is iterable, it will be returned as is.
- If not, the predefined
iterable_default(provided during object creation) will be returned instead.
This makes error handling easier when iterating over values. For example, you can set iterable_default to an empty list []. If the key doesn’t exist or the value is not iterable, the empty list is returned, and the for loop simply won’t execute.
How To Use
from flexible_dict import FlexibleDict
a = {'a': 'a',
'c': 30,
'b': 'b',
'cdefg': {
'c': 'c',
'defg': {
'd': 'd',
'efg':{
'e': 'e',
'fg': {'f': 'f', 'g': 'g'}
}
}
},
}
b = FlexibleDict(input_dict=a,
default='__MY_NONE__',
iterable_default='__MY_ITERABLE_DEFAULT__')
print(b['a'].value == 'a')
print(b['b'].value == 'b')
print(b['cdefg']['c'].value == 'c')
print(b['b']['c'].value == '__MY_NONE__')
print(b['b']['c']['k'].value == '__MY_NONE__')
print(b['cdefg']['defg']['d'].value == 'd')
print(b['cdefg']['defg']['kk'].value == '__MY_NONE__')
print(b['cdefg']['defg']['efg'].value == {'e': 'e', 'fg': {'f': 'f', 'g': 'g'}})
print(b['cdefg']['defg']['efg']['e'].value == 'e')
print(b['cdefg']['defg']['efg']['fg'].value == {'f': 'f', 'g': 'g'})
print(b['cdefg']['defg']['efg']['fg']['k'].value == '__MY_NONE__')
print(b['cdefg']['defg']['efg']['fg']['f'].value == 'f')
print(b['cdefg']['defg']['efg']['fg']['g']['k'].value == '__MY_NONE__')
print(b['a'].value)
print(b['a'].flexible_value)
print(b['a'].flexible_value.value)
print(b['cdefg'].flexible_value)
print(type(b['cdefg'].flexible_value))
print(b['cdefg'].flexible_value.value)
print(type(b['cdefg'].flexible_value.value))
print(b['cdefg'].value)
print(type(b['cdefg'].value))
print(b['cdefg'])
b['cdefg'] = {'f': 'f', 'g': 'g'}
print(b['cdefg'])
print(b['cdefg'].value)
print(type(b['cdefg'].value))
print(b['a'].iterable_value)
print(b['c'].iterable_value)
b['cdefg'].value['f'] = 'new'
print(b['cdefg']['f'].value)
b['cdefg']['f'].value = 'new'
print(b['cdefg']['f'].value)
b['cdefg']['f'] = 'new'
print(b['cdefg']['f'])
print(b['cdefg']['f'].value)
Metadata
Release files for deepflex-dict 1.2.1
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 |
|---|---|---|---|---|
| deepflex_dict-1.2.1.tar.gz | 4.2 kB | Without provenance | Details for deepflex_dict-1.2.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| deepflex_dict-1.2.1-py3-none-any.whl | Python 3 | none | any | Details for deepflex_dict-1.2.1-py3-none-any.whl |
Total release size: 9.2 kB
Release files / deepflex_dict-1.2.1.tar.gz
| Download URL | deepflex_dict-1.2.1.tar.gz |
|---|---|
| Size | 4.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
02f900e7990ee2c90ea279fe3b32d8d94d1e276167295fd71c8726bbe2eccdde
|
|
BLAKE2b-256 checksum How to use checksums |
812ac5425937f8e46d9c61ee787e9413733408811a66b95696e9fc1849f3f34a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.12.3
|
Release files / deepflex_dict-1.2.1-py3-none-any.whl
| Download URL | deepflex_dict-1.2.1-py3-none-any.whl |
|---|---|
| Size | 5.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
b46de10eddebaf415df94cbad838121a71ed41620d1eae3a8895cbd1aba96efe
|
|
BLAKE2b-256 checksum How to use checksums |
91baeb7d5bcb7914d04d367657001d600477e748693c5f05a44a33aee20b391b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.12.3
|
Security for deepflex-dict 1.2.1
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.