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 object types:
- Dictionaries
- Lists
- Sets
- Tuples
- Pydantic models
Install
pip install deep-apply
Usage
Apply upper() on values
import deep_apply
# 1. Create your callback function.
def to_upper(value, **kwargs):
"""
To uppercase.
"""
# Other arguments passed to the callback function:
# key: str = kwargs["key"]
# depth_level: int = kwargs["depth_level"]
# depth_key: str = kwargs["depth_key"]
# Apply upper() and return the value
if isinstance(value, str):
return value.upper()
# Always return the unedited value
return value
# 2. Your data.
data = [
{
"id": "pZnZMffPCpJx",
"name": "John Doe",
"hobbies": {
"sport": ["football", "tennis"],
"music": ["singing", "guitar", "piano"],
},
}
]
# 3. Run apply().
data = deep_apply.apply(data=data, func=to_upper)
[
{
'id': 'PZNZMFFPCPJX',
'name': 'JOHN DOE',
'hobbies': {
'sport': ['FOOTBALL', 'TENNIS'],
'music': ['SINGING', 'GUITAR', 'PIANO']
}
}
]
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
deep_apply-1.0.5.tar.gz
(7.6 kB
view details)
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.5.tar.gz.
File metadata
- Download URL: deep_apply-1.0.5.tar.gz
- Upload date:
- Size: 7.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3198a8fbd32224f7c032f361b442b2c66022f52127c394f13f6f49b2a19997c5
|
|
| MD5 |
f6e4e9ba425b4413692fde457aaebaa8
|
|
| BLAKE2b-256 |
6b1c5cac30ec5a21e6bbc449908849883d232c48fb0c967f19236e8283c92f30
|
File details
Details for the file deep_apply-1.0.5-py3-none-any.whl.
File metadata
- Download URL: deep_apply-1.0.5-py3-none-any.whl
- Upload date:
- Size: 13.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d36f22a7a5ccf557ed6b29dffaa1dcf33183e93a73ae8d9b1f848b37bd7d0c80
|
|
| MD5 |
cd395a0f62fbe98f3a744b451668330b
|
|
| BLAKE2b-256 |
13fe93db2222b5658f96026bc21fefe2e4b37e65de756e4961ede23804c800d0
|