Skip to main content

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

Project description

Deep Apply

PyPI - Python Version PyPI PyPI - License

tests

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)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

deep_apply-1.0.4-py3-none-any.whl (13.2 kB view details)

Uploaded Python 3

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

Hashes for deep_apply-1.0.4.tar.gz
Algorithm Hash digest
SHA256 fafa2276419f389917c92075787bad2c07394796726363e4b81f6a048595a7fa
MD5 5bb5947b2a5384625b0c8476bc4c9126
BLAKE2b-256 004238f832f47e8fe7aa53871cda24773dd331fb557deb411d8a239bac75ec4a

See more details on using hashes here.

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

Hashes for deep_apply-1.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 b4b8be30b6209cbf1c54ab42d73ba18aea044deae16a35d06e21be96db855250
MD5 bfecec03e1cacbbbe215253fcb6d5e80
BLAKE2b-256 d8e3eec51f9c23061183c92dac45a65570bde0b9eeb745f43af3fec673721436

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page