Skip to main content

Special dicts

Project description

ys

Special dicts

To install: pip install ys

Overview

The ys package provides specialized dictionary classes that extend the functionality of Python's standard dict and collections.defaultdict. These classes include:

  • keydefaultdict: A defaultdict variant where the default value for a missing key is generated dynamically based on the key itself.
  • DictDefaultDict: A dictionary that provides default values for missing keys based on a predefined dictionary of key-value pairs.
  • KeyPathDict: An advanced dictionary allowing access and modification of nested dictionary values using a key path (a dot-separated string or a list of keys).

Features

keydefaultdict

This class behaves like collections.defaultdict but allows the default factory function to receive the missing key as an argument. This is useful when the default value for each key needs to be determined dynamically.

DictDefaultDict

This dictionary class is initialized with another dictionary that specifies default values for certain keys. When accessing a key that does not exist in the DictDefaultDict, it will return the default value from the provided default dictionary if available.

KeyPathDict

This dictionary class supports accessing and setting values through a key path. A key path can be a dot-separated string or a list of nested keys, allowing for deep access into nested dictionaries. This feature is particularly useful for dealing with complex, nested data structures.

Usage Examples

Using keydefaultdict

from ys import keydefaultdict

def generate_default(key):
    return len(key)

kd = keydefaultdict(generate_default)
print(kd['hello'])  # Outputs 5, as the default factory uses the length of the key

Using DictDefaultDict

from ys import DictDefaultDict

defaults = {'missing': 'default value', 'another': 42}
dd = DictDefaultDict(defaults)
print(dd['missing'])  # Outputs 'default value'
print(dd['not_there'])  # Outputs KeyError as 'not_there' is not a predefined default

Using KeyPathDict

from ys import KeyPathDict

data = {
    'user': {
        'name': 'John Doe',
        'address': {
            'street': '123 Elm St',
            'city': 'Somewhere'
        }
    }
}
kpd = KeyPathDict(data)
print(kpd['user.address.street'])  # Outputs '123 Elm St'
kpd['user.address.zip_code'] = '12345'  # Adds a new nested key
print(kpd['user.address'])  # Outputs {'street': '123 Elm St', 'city': 'Somewhere', 'zip_code': '12345'}

These specialized dictionaries provide enhanced flexibility and functionality for handling complex data structures and dynamic default values in Python applications.

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

ys-0.0.5.tar.gz (7.8 kB view details)

Uploaded Source

Built Distribution

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

ys-0.0.5-py3-none-any.whl (8.0 kB view details)

Uploaded Python 3

File details

Details for the file ys-0.0.5.tar.gz.

File metadata

  • Download URL: ys-0.0.5.tar.gz
  • Upload date:
  • Size: 7.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.13

File hashes

Hashes for ys-0.0.5.tar.gz
Algorithm Hash digest
SHA256 099ca69a1426ee5643fbe3236f77cbb67c7f39630baa9f453d67bdd9e249c6cd
MD5 711591e8e4c840372eb597b5c60500c9
BLAKE2b-256 b2652c761c6531742cfa7b14bdfd3a14c8de858d3d5439901c7c18be666802d7

See more details on using hashes here.

File details

Details for the file ys-0.0.5-py3-none-any.whl.

File metadata

  • Download URL: ys-0.0.5-py3-none-any.whl
  • Upload date:
  • Size: 8.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.13

File hashes

Hashes for ys-0.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 b8ee13f77f39559fdd1103a570e9e2b4075b048754e776f220e4e145f24eb9cb
MD5 c00a2d100a0a48e1d782b774dff66c32
BLAKE2b-256 6679c16dad326ae7114acf33ab870ac865edbba96472f88574687b573384d349

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