Simple utils to get stuff out of Mappings (dict-like objects).
Project description
yv
Simple utils to get stuff out of Mappings (dict-like objects).
To install: pip install yv
Overview
The yv
package provides a collection of utilities designed to facilitate the extraction, manipulation, and querying of data from dictionary-like objects in Python. These utilities are particularly useful when dealing with nested or complex data structures commonly encountered in data processing tasks.
Features
- Key Chain Extraction: Retrieve values from a dictionary using a list of potential keys.
- Subdictionary Access: Enhanced dictionary class allowing for easy subdictionary creation and batch key retrieval.
- MongoDB-style Querying: Filter dictionary items using MongoDB-like query language.
- Nested Key Paths: Functions to handle nested dictionary paths for setting and getting values.
- Utility Functions: Includes various utility functions for dictionary manipulation and querying.
Usage Examples
Key Chain Extraction
Retrieve values from a dictionary using a list of potential keys, with the first found key's value being returned.
from yv import key_chain
d = {'a': 1, 'b': 2, 'c': 3}
result = key_chain(d, 'x', 'c', 'b') # Returns 3
Subdictionary Access
Enhanced dictionary class that interprets lists and sets of keys as requests for subdictionaries.
from yv import Subdict
d = {'a': 1, 'b': 2, 'c': 3}
dd = Subdict(d)
subdict = dd('a', 'b', c=100, d=100) # Returns {'a': 1, 'b': 2, 'c': 3, 'd': 100}
MongoDB-style Querying
Filter dictionary items using a MongoDB-like query.
from yv import dict_filt_from_mg_filt
mg_filt = {'a': {'$gte': 10, '$lt': 20}}
filt = dict_filt_from_mg_filt(mg_filt)
result = list(filter(filt, [{'a': x} for x in range(9, 22)])) # Returns [{'a': 10}, {'a': 15}]
Nested Key Paths
Set and get values in a dictionary using dot-separated string paths or lists of keys.
from yv import set_value_in_nested_key_path, get_value_in_key_path
d = {}
set_value_in_nested_key_path(d, 'a.b.c', 100)
print(d) # Outputs: {'a': {'b': {'c': 100}}}
value = get_value_in_key_path(d, 'a.b.c') # Returns 100
Utility Functions
Various utility functions to manipulate and query dictionaries.
from yv import left_union, all_but
d = {'a': 1, 'b': 2}
defaults = {'b': 3, 'c': 4}
new_d = left_union(d, defaults) # Returns {'b': 2, 'c': 4, 'a': 1}
filtered_d = all_but(d, ['a']) # Returns {'b': 2}
Documentation
Each function and class within the yv
package is documented with docstrings, providing detailed usage instructions and examples. This documentation can be accessed via Python's built-in help system or by reading the source code directly.
For more detailed examples and usage, refer to the function and class definitions in the source code.
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
Built Distribution
File details
Details for the file yv-0.0.6.tar.gz
.
File metadata
- Download URL: yv-0.0.6.tar.gz
- Upload date:
- Size: 9.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.13
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 |
ca16122e1992b9ebde3ea4b3ec3503ed9e264ed791473fc3cb1fe805b78c6a7b
|
|
MD5 |
b971f2b637d1a6ff6398f2769cf9eb9b
|
|
BLAKE2b-256 |
640f64031b38f11d798c8a78c5a0e4cca28a82854668807ef970c5b38943e87a
|
File details
Details for the file yv-0.0.6-py3-none-any.whl
.
File metadata
- Download URL: yv-0.0.6-py3-none-any.whl
- Upload date:
- Size: 10.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.13
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 |
6e3d263c606e0d53382161f6345696d16a7179979824b73f7701405721f5875f
|
|
MD5 |
d1d9a9645dc2f4ad85c58bf054bd0eba
|
|
BLAKE2b-256 |
1add2221ac4eec455d46fbe821dfb525d6635c487ef38c38ace5a37cf3ac7186
|