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.4.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.4.tar.gz.
File metadata
- Download URL: deep_apply-1.0.4.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 |
fafa2276419f389917c92075787bad2c07394796726363e4b81f6a048595a7fa
|
|
| MD5 |
5bb5947b2a5384625b0c8476bc4c9126
|
|
| BLAKE2b-256 |
004238f832f47e8fe7aa53871cda24773dd331fb557deb411d8a239bac75ec4a
|
File details
Details for the file deep_apply-1.0.4-py3-none-any.whl.
File metadata
- Download URL: deep_apply-1.0.4-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 |
b4b8be30b6209cbf1c54ab42d73ba18aea044deae16a35d06e21be96db855250
|
|
| MD5 |
bfecec03e1cacbbbe215253fcb6d5e80
|
|
| BLAKE2b-256 |
d8e3eec51f9c23061183c92dac45a65570bde0b9eeb745f43af3fec673721436
|