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.
Metadata
Release files for ys 0.0.6
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| ys-0.0.6.tar.gz | 8.4 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| ys-0.0.6-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 16.4 kB
Release files / ys-0.0.6.tar.gz
| Download URL | ys-0.0.6.tar.gz |
|---|---|
| Size | 8.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
e7ac54a43049c492003ed13efeb04457106a9b698bd4472782d15072beb6e61f
|
|
BLAKE2b-256 checksum How to use checksums |
fc39fafa388987a9f73b2d170e94d20b64347bf17a9080287089d9bf212ac19e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.10.10 {"installer":{"name":"uv","version":"0.10.10","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
|
Release files / ys-0.0.6-py3-none-any.whl
| Download URL | ys-0.0.6-py3-none-any.whl |
|---|---|
| Size | 8.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
d8229caccbca57650be2088c80fb9b7468c8232d0756747d6ab00fbd3bc35095
|
|
BLAKE2b-256 checksum How to use checksums |
52a2f73f007a9203697497df888085c86310105bd3258469657957270d84d24d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.10.10 {"installer":{"name":"uv","version":"0.10.10","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
|