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"]
# dept_level: int = kwargs["depth_level"]
# depth_key: str = kwargs["depth_key"]
# 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": {
"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.3.tar.gz
(8.1 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.3.tar.gz.
File metadata
- Download URL: deep_apply-1.0.3.tar.gz
- Upload date:
- Size: 8.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
21ffe3bc563c32c7d572c17bd2fbebe544f82a2daba5ff7bcb497b1607a6f03c
|
|
| MD5 |
15ab23e994d7bee4388f432b4f14194e
|
|
| BLAKE2b-256 |
206a8e00dd9932f9214c2c5bbc270ab89f38326718be8bea58c1505e848cce86
|
File details
Details for the file deep_apply-1.0.3-py3-none-any.whl.
File metadata
- Download URL: deep_apply-1.0.3-py3-none-any.whl
- Upload date:
- Size: 13.2 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 |
aacc5c6384c9477915ac3ea6f7bc8711305ce80ca56780f62c351bf6435c5b0f
|
|
| MD5 |
1b6aacd4648985c33385142443d6a52d
|
|
| BLAKE2b-256 |
b7d0ed80c54f4fb51e20fcb9d5a9d17b54e3c2d1980554ab552407f2f3f906b7
|