Metadata-Version: 2.2
Name: deep-apply
Version: 1.0.3
Summary: Deep traverse through an object and apply a function on its values.
Home-page: https://github.com/zaironjacobs/deep-apply
Download-URL: https://github.com/zaironjacobs/deep-apply/archive/v1.0.3.tar.gz
Author: Zairon Jacobs
Author-email: zaironjacobs@gmail.com
License: MIT
Keywords: deep,traverse,apply,object,list,set,tuple,pydantic,dict
Classifier: Development Status :: 5 - Production/Stable
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3.11
Classifier: Natural Language :: English
Requires-Python: >=3.11
Description-Content-Type: text/markdown
License-File: LICENSE
Dynamic: author
Dynamic: author-email
Dynamic: classifier
Dynamic: description
Dynamic: download-url
Dynamic: home-page
Dynamic: keywords
Dynamic: license
Dynamic: requires-python
Dynamic: summary

# Deep Apply

[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/deep-apply?color=blue)](https://pypi.python.org/pypi/deep-apply)
[![PyPI](https://img.shields.io/pypi/v/deep-apply?color=blue)](https://pypi.python.org/pypi/deep-apply)
[![PyPI - License](https://img.shields.io/pypi/l/deep-apply)](https://pypi.python.org/pypi/deep-apply)

[![tests](https://github.com/zaironjacobs/deep-apply/actions/workflows/test.yml/badge.svg)](https://github.com/zaironjacobs/deep-apply/actions/workflows/test.yml)

Deep traverse through an object and apply a function on its values.

Supports the following object types:

* Dictionaries
* Lists
* Sets
* Tuples
* Pydantic models

### Install

```bash
pip install deep-apply
```

### Usage

#### Apply upper() on values

```python
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)
```

```console
[
    {
        'id': 'PZNZMFFPCPJX',
        'name': 'JOHN DOE',
        'hobbies': {
            'sport': ['FOOTBALL', 'TENNIS'],
            'music': ['SINGING', 'GUITAR', 'PIANO']
        }
    }
]
```
