deep-apply


Namedeep-apply JSON
Version 1.0.2 PyPI version JSON
download
home_pagehttps://github.com/zaironjacobs/deep-apply
SummaryDeep traverse through an object and apply a function on its values.
upload_time2025-02-16 08:19:24
maintainerNone
docs_urlNone
authorZairon Jacobs
requires_python>=3.11
licenseMIT
keywords deep traverse apply object list set tuple pydantic dict
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # 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": {
            "id": "OlVZysGsIywW",
            "sport": ["football", "tennis"],
            "music": ["singing", "guitar", "piano"],
        },
    }
]

# 3. Run apply().
data = deep_apply.apply(data=data, func=to_upper)
```

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

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/zaironjacobs/deep-apply",
    "name": "deep-apply",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.11",
    "maintainer_email": null,
    "keywords": "deep, traverse, apply, object, list, set, tuple, pydantic, dict",
    "author": "Zairon Jacobs",
    "author_email": "zaironjacobs@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/60/99/11e6651f1e775ddf487577c68d21486f92cc1c2c917b6680a94ff286b248/deep_apply-1.0.2.tar.gz",
    "platform": null,
    "description": "# Deep Apply\n\n[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/deep-apply?color=blue)](https://pypi.python.org/pypi/deep-apply)\n[![PyPI](https://img.shields.io/pypi/v/deep-apply?color=blue)](https://pypi.python.org/pypi/deep-apply)\n[![PyPI - License](https://img.shields.io/pypi/l/deep-apply)](https://pypi.python.org/pypi/deep-apply)\n\n[![tests](https://github.com/zaironjacobs/deep-apply/actions/workflows/test.yml/badge.svg)](https://github.com/zaironjacobs/deep-apply/actions/workflows/test.yml)\n\nDeep traverse through an object and apply a function on its values.\n\nSupports the following object types:\n\n* Dictionaries\n* Lists\n* Sets\n* Tuples\n* Pydantic models\n\n### Install\n\n```bash\npip install deep-apply\n```\n\n### Usage\n\n#### Apply upper() on values\n\n```python\nimport deep_apply\n\n\n# 1. Create your callback function.\ndef to_upper(value, **kwargs):\n    \"\"\"\n    To uppercase.\n    \"\"\"\n    \n    # Other arguments passed to the callback function:\n    # key: str = kwargs[\"key\"]\n    # dept_level: int = kwargs[\"depth_level\"]\n    # depth_key: str = kwargs[\"depth_key\"]\n\n    # Apply upper() and return the value\n    if isinstance(value, str):\n        return value.upper()\n\n    return value\n\n\n# 2. Your data.\ndata = [\n    {\n        \"id\": \"pZnZMffPCpJx\",\n        \"name\": \"John Doe\",\n        \"hobbies\": {\n            \"id\": \"OlVZysGsIywW\",\n            \"sport\": [\"football\", \"tennis\"],\n            \"music\": [\"singing\", \"guitar\", \"piano\"],\n        },\n    }\n]\n\n# 3. Run apply().\ndata = deep_apply.apply(data=data, func=to_upper)\n```\n\n```console\n[{'hobbies': {'id': 'OLVZYSGSIYWW',\n              'music': ['SINGING', 'GUITAR', 'PIANO'],\n              'sport': ['FOOTBALL', 'TENNIS']},\n  'id': 'PZNZMFFPCPJX',\n  'name': 'JOHN DOE'}]\n```\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Deep traverse through an object and apply a function on its values.",
    "version": "1.0.2",
    "project_urls": {
        "Download": "https://github.com/zaironjacobs/deep-apply/archive/v1.0.2.tar.gz",
        "Homepage": "https://github.com/zaironjacobs/deep-apply"
    },
    "split_keywords": [
        "deep",
        " traverse",
        " apply",
        " object",
        " list",
        " set",
        " tuple",
        " pydantic",
        " dict"
    ],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "4555d5c487fb21e98fd4c902c47384de87f7ddab919e652fbe399c5b4b063b56",
                "md5": "572f9619e1e4987fcf8b28c2771d0086",
                "sha256": "0d755a8cf377cf29f48715f2fb3b43f1f650c9460d63e088e2104b896f6476e5"
            },
            "downloads": -1,
            "filename": "deep_apply-1.0.2-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "572f9619e1e4987fcf8b28c2771d0086",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.11",
            "size": 13166,
            "upload_time": "2025-02-16T08:19:22",
            "upload_time_iso_8601": "2025-02-16T08:19:22.709054Z",
            "url": "https://files.pythonhosted.org/packages/45/55/d5c487fb21e98fd4c902c47384de87f7ddab919e652fbe399c5b4b063b56/deep_apply-1.0.2-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "609911e6651f1e775ddf487577c68d21486f92cc1c2c917b6680a94ff286b248",
                "md5": "82b3be73af8444687c8d85e767af84fd",
                "sha256": "37709e6e0e75b7d25fe427ddc08d33c68c1ee98f74a218517972c0962ace7470"
            },
            "downloads": -1,
            "filename": "deep_apply-1.0.2.tar.gz",
            "has_sig": false,
            "md5_digest": "82b3be73af8444687c8d85e767af84fd",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.11",
            "size": 8044,
            "upload_time": "2025-02-16T08:19:24",
            "upload_time_iso_8601": "2025-02-16T08:19:24.667130Z",
            "url": "https://files.pythonhosted.org/packages/60/99/11e6651f1e775ddf487577c68d21486f92cc1c2c917b6680a94ff286b248/deep_apply-1.0.2.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-02-16 08:19:24",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "zaironjacobs",
    "github_project": "deep-apply",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "deep-apply"
}
        
Elapsed time: 5.72676s