deep-replacer


Namedeep-replacer JSON
Version 0.0.5 PyPI version JSON
download
home_pagehttps://github.com/zaironjacobs/deep-replacer
SummaryGiven 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.
upload_time2023-09-04 13:27:37
maintainer
docs_urlNone
authorZairon Jacobs
requires_python>=3
licenseMIT
keywords replace replacer deep
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # 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

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

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

### Example using `key_depth_rules` argument

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

```json
[
  {
    "name": "JOHN DOE",
    "hobbies": {
      "sport": [
        "football",
        "tennis"
      ],
      "music": [
        "SINGING",
        "GUITAR",
        "PIANO"
      ]
    }
  }
]
```

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/zaironjacobs/deep-replacer",
    "name": "deep-replacer",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3",
    "maintainer_email": "",
    "keywords": "replace,replacer,deep",
    "author": "Zairon Jacobs",
    "author_email": "zaironjacobs@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/65/b8/b528fb2be7d11d3ba04730e0868e43c44996aafbc7d59ab839f7dbddc22c/deep-replacer-0.0.5.tar.gz",
    "platform": null,
    "description": "# Deep Replacer\r\n\r\nGiven a list, set, tuple or dictionary as data input, loop through the data and replace all values that are not a list,\r\nset, tuple or dictionary using a replace function.\r\n\r\n## How to use\r\n\r\n### Basic example\r\n\r\n```python\r\nfrom deep_replacer import DeepReplacer\r\n\r\nreplacer = DeepReplacer()\r\n\r\n\r\ndef my_replace_func(value: str):\r\n    \"\"\"Return value to upper case\"\"\"\r\n\r\n    return value.upper()\r\n\r\n\r\ndata = [\r\n    {\r\n        \"name\": \"John Doe\",\r\n        \"hobbies\": {\r\n            \"sport\": [\"football\", \"tennis\"],\r\n            \"music\": [\"singing\", \"guitar\", \"piano\"],\r\n        },\r\n    }\r\n]\r\ndata_replaced = replacer.replace(data=data, replace_func=my_replace_func)\r\n\r\nprint(data_replaced)\r\n```\r\n\r\n### Output:\r\n\r\n```json\r\n[\r\n  {\r\n    \"name\": \"JOHN DOE\",\r\n    \"hobbies\": {\r\n      \"sport\": [\r\n        \"FOOTBALL\",\r\n        \"TENNIS\"\r\n      ],\r\n      \"music\": [\r\n        \"SINGING\",\r\n        \"GUITAR\",\r\n        \"PIANO\"\r\n      ]\r\n    }\r\n  }\r\n]\r\n```\r\n\r\n### Example using `key_depth_rules` argument\r\n\r\n```python\r\nfrom deep_replacer import DeepReplacer\r\nfrom deep_replacer import key_depth_rules\r\n\r\nreplacer = DeepReplacer()\r\n\r\n\r\ndef my_replace_func(value: str):\r\n    \"\"\"Return value to upper case\"\"\"\r\n\r\n    return value.upper()\r\n\r\n\r\ndata = [\r\n    {\r\n        \"name\": \"John Doe\",\r\n        \"hobbies\": {\r\n            \"sport\": [\"football\", \"tennis\"],\r\n            \"music\": [\"singing\", \"guitar\", \"piano\"],\r\n        },\r\n    }\r\n]\r\ndata_replaced = replacer.replace(\r\n    data=data,\r\n    replace_func=my_replace_func,\r\n    key_depth_rules={\"hobbies:sport\": [key_depth_rules.IGNORE]},  # Ignore key at depth 'hobbies:sport'\r\n)\r\n\r\nprint(data_replaced)\r\n```\r\n\r\n### Output:\r\n\r\n```json\r\n[\r\n  {\r\n    \"name\": \"JOHN DOE\",\r\n    \"hobbies\": {\r\n      \"sport\": [\r\n        \"football\",\r\n        \"tennis\"\r\n      ],\r\n      \"music\": [\r\n        \"SINGING\",\r\n        \"GUITAR\",\r\n        \"PIANO\"\r\n      ]\r\n    }\r\n  }\r\n]\r\n```\r\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "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.",
    "version": "0.0.5",
    "project_urls": {
        "Download": "https://github.com/zaironjacobs/deep-replacer/archive/v0.0.5.tar.gz",
        "Homepage": "https://github.com/zaironjacobs/deep-replacer"
    },
    "split_keywords": [
        "replace",
        "replacer",
        "deep"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "65b8b528fb2be7d11d3ba04730e0868e43c44996aafbc7d59ab839f7dbddc22c",
                "md5": "0ff41dc63d07b717d02c8a6a12435d9b",
                "sha256": "076588c8a604fab6a211542d411c690847b421747da96ecad515463309b0e46d"
            },
            "downloads": -1,
            "filename": "deep-replacer-0.0.5.tar.gz",
            "has_sig": false,
            "md5_digest": "0ff41dc63d07b717d02c8a6a12435d9b",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3",
            "size": 4722,
            "upload_time": "2023-09-04T13:27:37",
            "upload_time_iso_8601": "2023-09-04T13:27:37.212119Z",
            "url": "https://files.pythonhosted.org/packages/65/b8/b528fb2be7d11d3ba04730e0868e43c44996aafbc7d59ab839f7dbddc22c/deep-replacer-0.0.5.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-09-04 13:27:37",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "zaironjacobs",
    "github_project": "deep-replacer",
    "github_not_found": true,
    "lcname": "deep-replacer"
}
        
Elapsed time: 0.40806s