Skip to main content

A dictionary with flexible options

Project description

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)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

deepflex_dict-1.0.3.tar.gz (3.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

deepflex_dict-1.0.3-py3-none-any.whl (3.9 kB view details)

Uploaded Python 3

File details

Details for the file deepflex_dict-1.0.3.tar.gz.

File metadata

  • Download URL: deepflex_dict-1.0.3.tar.gz
  • Upload date:
  • Size: 3.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for deepflex_dict-1.0.3.tar.gz
Algorithm Hash digest
SHA256 e16b87cf67f1a818e66c0179c709467940ef8b064bc1170a17f57127a02f4a23
MD5 0863587e513a965ef4972b8a85821ab3
BLAKE2b-256 8f08770062a25aee7cbf92b0654818e86edea5a1480222ea31685af69152293c

See more details on using hashes here.

File details

Details for the file deepflex_dict-1.0.3-py3-none-any.whl.

File metadata

  • Download URL: deepflex_dict-1.0.3-py3-none-any.whl
  • Upload date:
  • Size: 3.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for deepflex_dict-1.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 d79fa3059abf6ff1459adc2808a65fac5eb31d71fa68cba6d917c9d19c484260
MD5 0830f6c97b21cadd997545e242c9b646
BLAKE2b-256 f89161bf4a0b5334831cdba2128f8372640f0f6b70183f646d6213160deb1e72

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page