functionapplydict


Namefunctionapplydict JSON
Version 0.10 PyPI version JSON
download
home_pagehttps://github.com/hansalemaos/functionapplydict
SummaryApplies a given function to the values of a nested dictionary
upload_time2023-09-04 23:57:39
maintainer
docs_urlNone
authorJohannes Fischer
requires_python
licenseMIT
keywords nested dict apply
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            
# Applies a given function to the values of a nested dictionary

## Tested against Windows 10 / Python 3.10 / Anaconda

## pip install functionapplydict

```python
apply_function_dict(d: dict, fu=lambda keys, item, d: item):
    Apply a given function to the values of a nested dictionary and return a modified dictionary.

    Parameters:
    - d (dict): The input dictionary to be modified.
    - fu (function): The function to be applied to each value in the dictionary.
        - keys (list): The list of keys leading to the current value.
        - item (any): The current value in the dictionary.
        - d (dict): The input dictionary.

    Returns:
    - dict: A new dictionary with the same structure as the input, but with modified values.

    Example:
        from functionapplydict import apply_function_dict

        child1 = {"name": "Emil", "year": 2004}
        child2 = {"name": "Tobias", "year": 2007}
        child3 = {"name": "Linus", "year": 2011}

        myfamily = {"child1": child1, "child2": child2, "child3": child3}


        def function1(keys, item, d):
            if isinstance(item, int):
                item = item * 8
            return item


        dnew = apply_function_dict(d=myfamily, fu=function1)
        dnew2 = apply_function_dict(d=myfamily, fu=lambda keys, item, d: str(item))

        ditest = {1: {2: [4, 56, 2, 4, 56]}, 2: {24: [4, 56, 2, 4, 56, 444]}}
        dnew3 = apply_function_dict(d=ditest, fu=lambda keys, item, d: sum(item))
        dnew4 = apply_function_dict(d=ditest, fu=lambda keys, item, d: str(item))

        print(dnew)
        print(dnew2)
        print(dnew3)
        print(dnew4)

        # {'child1': {'name': 'Emil', 'year': 16032}, 'child2': {'name': 'Tobias', 'year': 16056}, 'child3': {'name': 'Linus', 'year': 16088}}
        # {'child1': {'name': 'Emil', 'year': '2004'}, 'child2': {'name': 'Tobias', 'year': '2007'}, 'child3': {'name': 'Linus', 'year': '2011'}}
        # {1: {2: 122}, 2: {24: 566}} # sum can be applied to lists
        # {1: {2: '[4, 56, 2, 4, 56]'}, 2: {24: '[4, 56, 2, 4, 56, 444]'}} # the whole list is converted to a string
```



```python
apply_function_dict_deep(d: dict, fu=lambda keys, item, d: item):
    Apply a given function to the values of a nested dictionary deeply and return a modified dictionary.

    Parameters:
    - d (dict): The input dictionary to be modified.
    - fu (function): The function to be applied to each value in the dictionary.
        - keys (list): The list of keys leading to the current value.
        - item (any): The current value in the dictionary.
        - d (dict): The input dictionary.

    Returns:
    - dict: A new dictionary with the same structure as the input, but with modified values.

    Example:
        from functionapplydict import apply_function_dict_deep
        child1 = {"name": "Emil", "year": 2004}
        child2 = {"name": "Tobias", "year": 2007}
        child3 = {"name": "Linus", "year": 2011}

        myfamily = {"child1": child1, "child2": child2, "child3": child3}


        def function1(keys, item, d):
            if isinstance(item, int):
                item = item * 8
            return item

        dnew = apply_function_dict_deep(d=myfamily, fu=function1)
        dnew2 = apply_function_dict_deep(d=myfamily, fu=lambda keys, item, d: str(item))

        ditest = {1: {2: [4, 56, 2, 4, 56]}, 2: {24: [4, 56, 2, 4, 56, 444]}}
        dnew3 = apply_function_dict_deep(d=ditest, fu=lambda keys, item, d: sum(item))
        dnew4 = apply_function_dict_deep(d=ditest, fu=lambda keys, item, d: str(item))

        print(dnew)
        print(dnew2)
        print(dnew3)
        print(dnew4)

        # {'child1': {'name': 'Emil', 'year': 16032}, 'child2': {'name': 'Tobias', 'year': 16056}, 'child3': {'name': 'Linus', 'year': 16088}}
        # {'child1': {'name': 'Emil', 'year': '2004'}, 'child2': {'name': 'Tobias', 'year': '2007'}, 'child3': {'name': 'Linus', 'year': '2011'}}
        # {1: {2: [4, 56, 2, 4, 56]}, 2: {24: [4, 56, 2, 4, 56, 444]}} # no change, because sum can't be applied to int
        # {1: {2: ['4', '56', '2', '4', '56']}, 2: {24: ['4', '56', '2', '4', '56', '444']}} # each int is converted to a string, but not the whole list


```

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/hansalemaos/functionapplydict",
    "name": "functionapplydict",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "nested,dict,apply",
    "author": "Johannes Fischer",
    "author_email": "aulasparticularesdealemaosp@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/02/e3/fb60c527345842065a3d86da585762cec408273d332b1196b615a6c947c9/functionapplydict-0.10.tar.gz",
    "platform": null,
    "description": "\r\n# Applies a given function to the values of a nested dictionary\r\n\r\n## Tested against Windows 10 / Python 3.10 / Anaconda\r\n\r\n## pip install functionapplydict\r\n\r\n```python\r\napply_function_dict(d: dict, fu=lambda keys, item, d: item):\r\n    Apply a given function to the values of a nested dictionary and return a modified dictionary.\r\n\r\n    Parameters:\r\n    - d (dict): The input dictionary to be modified.\r\n    - fu (function): The function to be applied to each value in the dictionary.\r\n        - keys (list): The list of keys leading to the current value.\r\n        - item (any): The current value in the dictionary.\r\n        - d (dict): The input dictionary.\r\n\r\n    Returns:\r\n    - dict: A new dictionary with the same structure as the input, but with modified values.\r\n\r\n    Example:\r\n        from functionapplydict import apply_function_dict\r\n\r\n        child1 = {\"name\": \"Emil\", \"year\": 2004}\r\n        child2 = {\"name\": \"Tobias\", \"year\": 2007}\r\n        child3 = {\"name\": \"Linus\", \"year\": 2011}\r\n\r\n        myfamily = {\"child1\": child1, \"child2\": child2, \"child3\": child3}\r\n\r\n\r\n        def function1(keys, item, d):\r\n            if isinstance(item, int):\r\n                item = item * 8\r\n            return item\r\n\r\n\r\n        dnew = apply_function_dict(d=myfamily, fu=function1)\r\n        dnew2 = apply_function_dict(d=myfamily, fu=lambda keys, item, d: str(item))\r\n\r\n        ditest = {1: {2: [4, 56, 2, 4, 56]}, 2: {24: [4, 56, 2, 4, 56, 444]}}\r\n        dnew3 = apply_function_dict(d=ditest, fu=lambda keys, item, d: sum(item))\r\n        dnew4 = apply_function_dict(d=ditest, fu=lambda keys, item, d: str(item))\r\n\r\n        print(dnew)\r\n        print(dnew2)\r\n        print(dnew3)\r\n        print(dnew4)\r\n\r\n        # {'child1': {'name': 'Emil', 'year': 16032}, 'child2': {'name': 'Tobias', 'year': 16056}, 'child3': {'name': 'Linus', 'year': 16088}}\r\n        # {'child1': {'name': 'Emil', 'year': '2004'}, 'child2': {'name': 'Tobias', 'year': '2007'}, 'child3': {'name': 'Linus', 'year': '2011'}}\r\n        # {1: {2: 122}, 2: {24: 566}} # sum can be applied to lists\r\n        # {1: {2: '[4, 56, 2, 4, 56]'}, 2: {24: '[4, 56, 2, 4, 56, 444]'}} # the whole list is converted to a string\r\n```\r\n\r\n\r\n\r\n```python\r\napply_function_dict_deep(d: dict, fu=lambda keys, item, d: item):\r\n    Apply a given function to the values of a nested dictionary deeply and return a modified dictionary.\r\n\r\n    Parameters:\r\n    - d (dict): The input dictionary to be modified.\r\n    - fu (function): The function to be applied to each value in the dictionary.\r\n        - keys (list): The list of keys leading to the current value.\r\n        - item (any): The current value in the dictionary.\r\n        - d (dict): The input dictionary.\r\n\r\n    Returns:\r\n    - dict: A new dictionary with the same structure as the input, but with modified values.\r\n\r\n    Example:\r\n        from functionapplydict import apply_function_dict_deep\r\n        child1 = {\"name\": \"Emil\", \"year\": 2004}\r\n        child2 = {\"name\": \"Tobias\", \"year\": 2007}\r\n        child3 = {\"name\": \"Linus\", \"year\": 2011}\r\n\r\n        myfamily = {\"child1\": child1, \"child2\": child2, \"child3\": child3}\r\n\r\n\r\n        def function1(keys, item, d):\r\n            if isinstance(item, int):\r\n                item = item * 8\r\n            return item\r\n\r\n        dnew = apply_function_dict_deep(d=myfamily, fu=function1)\r\n        dnew2 = apply_function_dict_deep(d=myfamily, fu=lambda keys, item, d: str(item))\r\n\r\n        ditest = {1: {2: [4, 56, 2, 4, 56]}, 2: {24: [4, 56, 2, 4, 56, 444]}}\r\n        dnew3 = apply_function_dict_deep(d=ditest, fu=lambda keys, item, d: sum(item))\r\n        dnew4 = apply_function_dict_deep(d=ditest, fu=lambda keys, item, d: str(item))\r\n\r\n        print(dnew)\r\n        print(dnew2)\r\n        print(dnew3)\r\n        print(dnew4)\r\n\r\n        # {'child1': {'name': 'Emil', 'year': 16032}, 'child2': {'name': 'Tobias', 'year': 16056}, 'child3': {'name': 'Linus', 'year': 16088}}\r\n        # {'child1': {'name': 'Emil', 'year': '2004'}, 'child2': {'name': 'Tobias', 'year': '2007'}, 'child3': {'name': 'Linus', 'year': '2011'}}\r\n        # {1: {2: [4, 56, 2, 4, 56]}, 2: {24: [4, 56, 2, 4, 56, 444]}} # no change, because sum can't be applied to int\r\n        # {1: {2: ['4', '56', '2', '4', '56']}, 2: {24: ['4', '56', '2', '4', '56', '444']}} # each int is converted to a string, but not the whole list\r\n\r\n\r\n```\r\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Applies a given function to the values of a nested dictionary",
    "version": "0.10",
    "project_urls": {
        "Homepage": "https://github.com/hansalemaos/functionapplydict"
    },
    "split_keywords": [
        "nested",
        "dict",
        "apply"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "c320ecb9cfd8be56c9c0116bae1030bd5e51017d9098df1705da19f857beca35",
                "md5": "2743bbece5112990f7db744e875b00f8",
                "sha256": "edca94fa3e512e1b9f4f7c89e1678dace09fbbc80e0d6a0eea6318a91cba4f36"
            },
            "downloads": -1,
            "filename": "functionapplydict-0.10-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "2743bbece5112990f7db744e875b00f8",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 6777,
            "upload_time": "2023-09-04T23:57:37",
            "upload_time_iso_8601": "2023-09-04T23:57:37.346060Z",
            "url": "https://files.pythonhosted.org/packages/c3/20/ecb9cfd8be56c9c0116bae1030bd5e51017d9098df1705da19f857beca35/functionapplydict-0.10-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "02e3fb60c527345842065a3d86da585762cec408273d332b1196b615a6c947c9",
                "md5": "52d8a56636e1170f860ccd329a495817",
                "sha256": "0efbc66184926ee73c9687bcca2ec192a0b1bb19ebf37d453dc712dafc1bb38d"
            },
            "downloads": -1,
            "filename": "functionapplydict-0.10.tar.gz",
            "has_sig": false,
            "md5_digest": "52d8a56636e1170f860ccd329a495817",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 5028,
            "upload_time": "2023-09-04T23:57:39",
            "upload_time_iso_8601": "2023-09-04T23:57:39.202316Z",
            "url": "https://files.pythonhosted.org/packages/02/e3/fb60c527345842065a3d86da585762cec408273d332b1196b615a6c947c9/functionapplydict-0.10.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-09-04 23:57:39",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "hansalemaos",
    "github_project": "functionapplydict",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "requirements": [],
    "lcname": "functionapplydict"
}
        
Elapsed time: 0.14092s