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 objects:

  • Dictionaries
  • Lists
  • Sets
  • Tuples
  • Pydantic models

Install

pip install deep-apply

Usage

Apply upper() on values

import deep_apply


# 1. Create your callback function. Will call upper() on strings.
def to_upper(value, **kwargs):
    """
    To uppercase.
    """

    # 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)

Result

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

Ignore keys

You can get the current key or the current depth from **kwargs and add a condition e.g. to skip a specific key everywhere.

def to_upper(value, **kwargs):
    """
    To uppercase.
    """

    key = kwargs.get("key")
    depth = kwargs.get("depth")

    ignore = False

    # Ignore the key/field id everywhere (dictionaries or pydantic models)
    if key == "id":
        ignore = True

    # Ignore the list of music found under hobbies
    elif depth == "hobbies:music":
        ignore = True

    # Apply upper() and return the value
    if not ignore and isinstance(value, str):
        return value.upper()

    return value

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.0.tar.gz (6.7 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.0-py3-none-any.whl (9.6 kB view details)

Uploaded Python 3

File details

Details for the file deep_apply-1.0.0.tar.gz.

File metadata

  • Download URL: deep_apply-1.0.0.tar.gz
  • Upload date:
  • Size: 6.7 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.0.tar.gz
Algorithm Hash digest
SHA256 1b3c4d8ab0d7c0f131ae731fdd8a8f685c5ca71dd956bb4a78a1b054ed6a2fc6
MD5 2e280376be394e1021a879aba20e6f1f
BLAKE2b-256 0568b3dee855590d9458e19fec85b25cba954f1ad5e976d6f80340ab70a24fa2

See more details on using hashes here.

File details

Details for the file deep_apply-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: deep_apply-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 9.6 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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c4d15dde1eaaa502fead52126e0866c03f5bcf047f6387a1dd166fa2eb014d56
MD5 b2345644a79c84c734a93f0c284b7fa1
BLAKE2b-256 af3b48e9498e78c91897804e421945d53d415b204aacc423e79d138640c6a2bf

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