Skip to main content

Given a list, set, tuple or dictionary as data input, loop through the data and replace all values that are not a list, set, tuple or dictionary using a replace function.

Project description

Deep Replacer

Given a list, set, tuple or dictionary as data input, loop through the data and replace all values that are not a list, set, tuple or dictionary using a replace function.

How to use

Basic example

from deep_replacer import DeepReplacer

replacer = DeepReplacer()


def my_replace_func(value: str):
    """Return value to upper case"""

    return value.upper()


data = [
    {
        "name": "John Doe",
        "hobbies": {
            "sport": ["football", "tennis"],
            "music": ["singing", "guitar", "piano"],
        },
    }
]
data_replaced = replacer.replace(data=data, replace_func=my_replace_func)

print(data_replaced)

Output:

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

Example using key_depth_rules argument

from deep_replacer import DeepReplacer
from deep_replacer import key_depth_rules

replacer = DeepReplacer()


def my_replace_func(value: str):
    """Return value to upper case"""

    return value.upper()


data = [
    {
        "name": "John Doe",
        "hobbies": {
            "sport": ["football", "tennis"],
            "music": ["singing", "guitar", "piano"],
        },
    }
]
data_replaced = replacer.replace(
    data=data,
    replace_func=my_replace_func,
    key_depth_rules={"hobbies:sport": [key_depth_rules.IGNORE]},  # Ignore key at depth 'hobbies:sport'
)

print(data_replaced)

Output:

[
  {
    "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-replacer-0.0.4.tar.gz (4.7 kB view details)

Uploaded Source

File details

Details for the file deep-replacer-0.0.4.tar.gz.

File metadata

  • Download URL: deep-replacer-0.0.4.tar.gz
  • Upload date:
  • Size: 4.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.12

File hashes

Hashes for deep-replacer-0.0.4.tar.gz
Algorithm Hash digest
SHA256 eefaf6bbaf978acf63e47a0ff3b1689358caf2dd876250e7ef2abe4b2f3a2799
MD5 36f1f0771e091f5b9e0e4eee3bd173f7
BLAKE2b-256 32a0d1f99c2b7fa73a18efd04cacb288b7cfa706404faa40c739b3a0be1674a2

See more details on using hashes here.

Supported by

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