Skip to main content

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 by FlexibleDict.
  • 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 default value 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 a FlexibleDict.

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_value guarantees 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.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for deepflex-dict 1.2.0
File Size Uploaded Provenance Actions
deepflex_dict-1.2.0.tar.gz 4.2 kB Without provenance Details for deepflex_dict-1.2.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for deepflex-dict 1.2.0
File Interpreter ABI Platform Actions
deepflex_dict-1.2.0-py3-none-any.whl Python 3 none any Details for deepflex_dict-1.2.0-py3-none-any.whl

Total release size: 9.3 kB

Release files / deepflex_dict-1.2.0.tar.gz

Download URL deepflex_dict-1.2.0.tar.gz
Size 4.2 kB
Tags Source
SHA-256 checksum
How to use checksums
42cdc1faad0909b6cef483a23d21e2a0cec36693c75c88b2ad027900f433349f
BLAKE2b-256 checksum
How to use checksums
1318a7eceab8677f3107fec4d64832dd7ed26cdf2b0b3719b9a2bb2a41573d05
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.3

Release files / deepflex_dict-1.2.0-py3-none-any.whl

Download URL deepflex_dict-1.2.0-py3-none-any.whl
Size 5.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
23bc83a24ece262d77b5924ea850ee9f68b35ff854e01b6b71e97fd80c6dc112
BLAKE2b-256 checksum
How to use checksums
efd8b57688fe0b299c7300f1a2fba5183e620c32ee77a6eb28dc14ab4395933f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.3

Security for deepflex-dict 1.2.0

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.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page