Deep traverse through an object and apply a function on its values.
Project description
Deep Apply
Deep traverse through an object and apply a function on its values.
Supports the following objects:
- Dictionaries
- Lists
- Sets
- Tuples
- Pydantic models
Install
pip install deep-apply
Usage
Apply upper() on values
import deep_apply
# 1. Create your callback function. Will call upper() on strings.
def to_upper(value, **_kwargs):
"""
To uppercase.
"""
# Apply upper() and return the value
if isinstance(value, str):
return value.upper()
return value
# 2. Your data.
data = [
{
"id": "pZnZMffPCpJx",
"name": "John Doe",
"hobbies": {
"id": "OlVZysGsIywW",
"sport": ["football", "tennis"],
"music": ["singing", "guitar", "piano"],
},
}
]
# 3. Run apply().
data = deep_apply.apply(data=data, func=to_upper)
Result
[
{
"id": "PZNZMFFPCPJX",
"name": "JOHN DOE",
"hobbies": {
"id": "OLVZYSGSIYWW",
"sport": [
"FOOTBALL",
"TENNIS"
],
"music": [
"SINGING",
"GUITAR",
"PIANO"
]
}
}
]
Ignore keys
You can get the current key or the current depth from **kwargs and add a condition e.g. to skip a specific key
everywhere.
def to_upper(value, **kwargs):
"""
To uppercase.
"""
key = kwargs.get("key")
depth = kwargs.get("depth")
ignore = False
# Ignore the key/field id everywhere (dictionaries or pydantic models)
if key == "id":
ignore = True
# Ignore the list of music found under hobbies
elif depth == "hobbies:music":
ignore = True
# Apply upper() and return the value
if not ignore and isinstance(value, str):
return value.upper()
return value
Only allow specific types
If you only want to traverse through specific object types e.g. lists and dictionaires, use the argument
allowed_types.
data = deep_apply.apply(data=data, func=to_upper, allowed_types=["list", "dict"])
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file deep_apply-1.0.1.tar.gz.
File metadata
- Download URL: deep_apply-1.0.1.tar.gz
- Upload date:
- Size: 8.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8ec44810b4ef363fbed85a42aa1db44d4e55982946d079d368e70fc97d557feb
|
|
| MD5 |
adf8f768327040624d8857259df3a686
|
|
| BLAKE2b-256 |
37e6914cab77d9e5fec0797622a955fbf87035f98ec7d517dcc5b888b9a65e86
|
File details
Details for the file deep_apply-1.0.1-py3-none-any.whl.
File metadata
- Download URL: deep_apply-1.0.1-py3-none-any.whl
- Upload date:
- Size: 12.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d864e4f1651f7b28bf187b88227ac822d7354081f3ab88a6e5cdb50e33eb904f
|
|
| MD5 |
a935fb1347fb7f039db4165590502ff8
|
|
| BLAKE2b-256 |
53489c74772f193985fb371b53422c6a16309254f850e2279570cc5e6b7e19a9
|