flatten-everything


Nameflatten-everything JSON
Version 0.41 PyPI version JSON
download
home_pagehttps://github.com/hansalemaos/flatten_everything
SummaryFlattens everything - lists, tuples, dicts, np, pd... Option to protect iterables from being flattened
upload_time2022-10-02 05:04:06
maintainer
docs_urlNone
authorJohannes Fischer
requires_python
licenseMIT
keywords flatten pandas dict list numpy tuple tagsiter nested iterable listsoflists flattenjson iter explode squeeze
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            
## Update:



**2022/09/30:** fixed ProtectedDict, ProtectedList, ProtectedTuple, ProtectedSet - Sometimes didn't protect!



**2022/09/30:** Added doc strings



## What does it do?



It flattens any iterable, it doesn't matter how deeply it is nested. If there are dicts in you iterable, it will only return the values. If you need the keys too, have a look at this package: [flatten-any-dict-iterable-or-whatsoever ยท PyPI](https://pypi.org/project/flatten-any-dict-iterable-or-whatsoever/)



## Install it:



```python

pip install flatten-everything

```



## Import it:



```python

from flatten_everything import flatten_everything, ProtectedDict,ProtectedList,ProtectedTuple,ProtectedSet

```



## Use it:



```python

{

    "id": "001",

    "company": "XYZ pvt ltd",

    "location": "London",

    "info": {

        "president": "Rakesh Kapoor",

        "contacts": {"email": "contact@xyz.com", "tel": "9876543210"},

        "onemorefortesting": {

            "name": {"name": "John", "age": "27", "sex": "Male"},

            "Peter2": {"name": "Marie", "age": "22", "sex": "Female"},

            "sdfsdf": {"name": "Luna", "age": "24", "sex": "Female"},

            "another_nested_something": [(2, 1), (3, 2), (1, 2), (1, 3), (1, 3), (2, 3), (1, 1), (3, 3), (2, 1), (1, 1), (1, 2), (3, 1), (3, 1), (3, 2), (1, 2), (1, 1), (3, 2), (2, 1), (1, 1), (3, 1)],

            "Peter": {"name": "Peter", "age": "29", "sex": "Male"},

        },

    },

},

{

    "id": "002",

    "company": "PQR Associates",

    "location": "Abu Dhabi",

    "info": {

        "president": "Neelam Subramaniyam",

        "contacts": {"email": "contact@pqr.com", "tel": "8876443210"},

    },

},

]



list(flatten_everything(data))



Result:

['001', 'XYZ pvt ltd', 'London', 'Rakesh Kapoor', 'contact@xyz.com', '9876543210', 'John', '27', 'Male', 'Marie', '22', 'Female', 'Luna', '24', 'Female', 2, 1, 3, 2, 1, 2, 1, 3, 1, 3, 2, 3, 1, 1, 3, 3, 2, 1, 1, 1, 1, 2, 3, 1, 3, 1, 3, 2, 1, 2, 1, 1, 3, 2, 2, 1, 1, 1, 3, 1, 'Peter', '29', 'Male', '002', 'PQR Associates', 'Abu Dhabi', 'Neelam Subramaniyam', 'contact@pqr.com', '8876443210']





    #If you want to protect iterables from being flattened, you can use:



data = [

{

"id": "001",

"company": "XYZ pvt ltd",'protect_test':ProtectedTuple((333,332,555)),

"location": "London",

"info": {

    "president": "Rakesh Kapoor",

    "contacts": {"email": "contact@xyz.com", "tel": "9876543210"}, 'onemorefortesting': {

        "name": {"name": "John", "age": "27", "sex": "Male"},

        "Peter2": {"name": "Marie", "age": "22", "sex": "Female"},

        "sdfsdf": {"name": "Luna", "age": "24", "sex": "Female"}, 'another_nested_something': ProtectedList([(2, 1), (3, 2), (1, 2), (1, 3), (1, 3), (2, 3), (1, 1), (3, 3), (2, 1), (1, 1), (1, 2), (3, 1), (3, 1), (3, 2), (1, 2), (1, 1), (3, 2), (2, 1), (1, 1), (3, 1)]),

        "Peter": ProtectedDict({"name": "Peter", "age": "29", "sex": "Male"}),

    },},},{"id": "002",

"company": "PQR Associates",

"location": "Abu Dhabi",

"info": {    "president": "Neelam Subramaniyam",

    "contacts": {"email": "contact@pqr.com", "tel": "8876443210"},},},]

print(list(flatten_everything(data)))

['001', 'XYZ pvt ltd', (333, 332, 555), 'London', 'Rakesh Kapoor', 'contact@xyz.com', '9876543210', 'John', '27', 'Male', 'Marie', '22', 'Female', 'Luna', '24', 'Female', [(2, 1), (3, 2), (1, 2), (1, 3), (1, 3), (2, 3), (1, 1), (3, 3), (2, 1), (1, 1), (1, 2), (3, 1), (3, 1), (3, 2), (1, 2), (1, 1), (3, 2), (2, 1), (1, 1), (3, 1)], {'name': 'Peter', 'age': '29', 'sex': 'Male'}, '002', 'PQR Associates', 'Abu Dhabi', 'Neelam Subramaniyam', 'contact@pqr.com', '8876443210']



#Parameters:

#    item: Any

#        Input iterable

#        Most of the time you will be using only this parameter.

#    forbidden: tuple

#        Data dtype which cannot be returned

#        (default=(list, tuple, set, frozenset))

#    allowed: tuple

#        Data dtype which can be returned

#        default (

#        str,

#        int,

#        float,

#        complex,

#        bool,

#        bytes,

#        type(None),

#        ProtectedTuple,  # Inherits from tuple but is protected, this is how you protected iterables

#        ProtectedList,  # same here

#        ProtectedDict, # same here

#        ProtectedSet, # same here

#        Tuppsub  #Inherit from tuple and exclude it from being flattened -

#

#        )

#    dict_variation: tuple

#        Due to recent changes, might not be necessary anymore, used to filter dict variations

#        (default =

#        (

#        "collections.defaultdict",

#        "collections.UserDict",

#        "collections.OrderedDict",

#        )

#Returns:

#    Generator

```


            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/hansalemaos/flatten_everything",
    "name": "flatten-everything",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "flatten,pandas,dict,list,numpy,tuple,Tagsiter,nested,iterable,listsoflists,flattenjson,iter,explode,squeeze",
    "author": "Johannes Fischer",
    "author_email": "<aulasparticularesdealemaosp@gmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/84/f7/856d3633c3edefe1c6db9b066f22e525d2f1f689ae08be4d52153f1f2fbb/flatten_everything-0.41.tar.gz",
    "platform": null,
    "description": "\n## Update:\n\n\n\n**2022/09/30:** fixed ProtectedDict, ProtectedList, ProtectedTuple, ProtectedSet - Sometimes didn't protect!\n\n\n\n**2022/09/30:** Added doc strings\n\n\n\n## What does it do?\n\n\n\nIt flattens any iterable, it doesn't matter how deeply it is nested. If there are dicts in you iterable, it will only return the values. If you need the keys too, have a look at this package: [flatten-any-dict-iterable-or-whatsoever \u00b7 PyPI](https://pypi.org/project/flatten-any-dict-iterable-or-whatsoever/)\n\n\n\n## Install it:\n\n\n\n```python\n\npip install flatten-everything\n\n```\n\n\n\n## Import it:\n\n\n\n```python\n\nfrom flatten_everything import flatten_everything, ProtectedDict,ProtectedList,ProtectedTuple,ProtectedSet\n\n```\n\n\n\n## Use it:\n\n\n\n```python\n\n{\n\n    \"id\": \"001\",\n\n    \"company\": \"XYZ pvt ltd\",\n\n    \"location\": \"London\",\n\n    \"info\": {\n\n        \"president\": \"Rakesh Kapoor\",\n\n        \"contacts\": {\"email\": \"contact@xyz.com\", \"tel\": \"9876543210\"},\n\n        \"onemorefortesting\": {\n\n            \"name\": {\"name\": \"John\", \"age\": \"27\", \"sex\": \"Male\"},\n\n            \"Peter2\": {\"name\": \"Marie\", \"age\": \"22\", \"sex\": \"Female\"},\n\n            \"sdfsdf\": {\"name\": \"Luna\", \"age\": \"24\", \"sex\": \"Female\"},\n\n            \"another_nested_something\": [(2, 1), (3, 2), (1, 2), (1, 3), (1, 3), (2, 3), (1, 1), (3, 3), (2, 1), (1, 1), (1, 2), (3, 1), (3, 1), (3, 2), (1, 2), (1, 1), (3, 2), (2, 1), (1, 1), (3, 1)],\n\n            \"Peter\": {\"name\": \"Peter\", \"age\": \"29\", \"sex\": \"Male\"},\n\n        },\n\n    },\n\n},\n\n{\n\n    \"id\": \"002\",\n\n    \"company\": \"PQR Associates\",\n\n    \"location\": \"Abu Dhabi\",\n\n    \"info\": {\n\n        \"president\": \"Neelam Subramaniyam\",\n\n        \"contacts\": {\"email\": \"contact@pqr.com\", \"tel\": \"8876443210\"},\n\n    },\n\n},\n\n]\n\n\n\nlist(flatten_everything(data))\n\n\n\nResult:\n\n['001', 'XYZ pvt ltd', 'London', 'Rakesh Kapoor', 'contact@xyz.com', '9876543210', 'John', '27', 'Male', 'Marie', '22', 'Female', 'Luna', '24', 'Female', 2, 1, 3, 2, 1, 2, 1, 3, 1, 3, 2, 3, 1, 1, 3, 3, 2, 1, 1, 1, 1, 2, 3, 1, 3, 1, 3, 2, 1, 2, 1, 1, 3, 2, 2, 1, 1, 1, 3, 1, 'Peter', '29', 'Male', '002', 'PQR Associates', 'Abu Dhabi', 'Neelam Subramaniyam', 'contact@pqr.com', '8876443210']\n\n\n\n\n\n    #If you want to protect iterables from being flattened, you can use:\n\n\n\ndata = [\n\n{\n\n\"id\": \"001\",\n\n\"company\": \"XYZ pvt ltd\",'protect_test':ProtectedTuple((333,332,555)),\n\n\"location\": \"London\",\n\n\"info\": {\n\n    \"president\": \"Rakesh Kapoor\",\n\n    \"contacts\": {\"email\": \"contact@xyz.com\", \"tel\": \"9876543210\"}, 'onemorefortesting': {\n\n        \"name\": {\"name\": \"John\", \"age\": \"27\", \"sex\": \"Male\"},\n\n        \"Peter2\": {\"name\": \"Marie\", \"age\": \"22\", \"sex\": \"Female\"},\n\n        \"sdfsdf\": {\"name\": \"Luna\", \"age\": \"24\", \"sex\": \"Female\"}, 'another_nested_something': ProtectedList([(2, 1), (3, 2), (1, 2), (1, 3), (1, 3), (2, 3), (1, 1), (3, 3), (2, 1), (1, 1), (1, 2), (3, 1), (3, 1), (3, 2), (1, 2), (1, 1), (3, 2), (2, 1), (1, 1), (3, 1)]),\n\n        \"Peter\": ProtectedDict({\"name\": \"Peter\", \"age\": \"29\", \"sex\": \"Male\"}),\n\n    },},},{\"id\": \"002\",\n\n\"company\": \"PQR Associates\",\n\n\"location\": \"Abu Dhabi\",\n\n\"info\": {    \"president\": \"Neelam Subramaniyam\",\n\n    \"contacts\": {\"email\": \"contact@pqr.com\", \"tel\": \"8876443210\"},},},]\n\nprint(list(flatten_everything(data)))\n\n['001', 'XYZ pvt ltd', (333, 332, 555), 'London', 'Rakesh Kapoor', 'contact@xyz.com', '9876543210', 'John', '27', 'Male', 'Marie', '22', 'Female', 'Luna', '24', 'Female', [(2, 1), (3, 2), (1, 2), (1, 3), (1, 3), (2, 3), (1, 1), (3, 3), (2, 1), (1, 1), (1, 2), (3, 1), (3, 1), (3, 2), (1, 2), (1, 1), (3, 2), (2, 1), (1, 1), (3, 1)], {'name': 'Peter', 'age': '29', 'sex': 'Male'}, '002', 'PQR Associates', 'Abu Dhabi', 'Neelam Subramaniyam', 'contact@pqr.com', '8876443210']\n\n\n\n#Parameters:\n\n#    item: Any\n\n#        Input iterable\n\n#        Most of the time you will be using only this parameter.\n\n#    forbidden: tuple\n\n#        Data dtype which cannot be returned\n\n#        (default=(list, tuple, set, frozenset))\n\n#    allowed: tuple\n\n#        Data dtype which can be returned\n\n#        default (\n\n#        str,\n\n#        int,\n\n#        float,\n\n#        complex,\n\n#        bool,\n\n#        bytes,\n\n#        type(None),\n\n#        ProtectedTuple,  # Inherits from tuple but is protected, this is how you protected iterables\n\n#        ProtectedList,  # same here\n\n#        ProtectedDict, # same here\n\n#        ProtectedSet, # same here\n\n#        Tuppsub  #Inherit from tuple and exclude it from being flattened -\n\n#\n\n#        )\n\n#    dict_variation: tuple\n\n#        Due to recent changes, might not be necessary anymore, used to filter dict variations\n\n#        (default =\n\n#        (\n\n#        \"collections.defaultdict\",\n\n#        \"collections.UserDict\",\n\n#        \"collections.OrderedDict\",\n\n#        )\n\n#Returns:\n\n#    Generator\n\n```\n\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Flattens everything - lists, tuples, dicts, np, pd... Option to protect iterables from being flattened",
    "version": "0.41",
    "project_urls": {
        "Homepage": "https://github.com/hansalemaos/flatten_everything"
    },
    "split_keywords": [
        "flatten",
        "pandas",
        "dict",
        "list",
        "numpy",
        "tuple",
        "tagsiter",
        "nested",
        "iterable",
        "listsoflists",
        "flattenjson",
        "iter",
        "explode",
        "squeeze"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "d04ae37e9657d07cbb8d876b0a9d1745080cbe8491a502f3bde1cc2fecfd2037",
                "md5": "28d21efc16df487f85c79a932920d7f3",
                "sha256": "9dd6623e230d8a79494d769aa5821186b0e9ff8da5bbae6e8043a8f69575b11a"
            },
            "downloads": -1,
            "filename": "flatten_everything-0.41-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "28d21efc16df487f85c79a932920d7f3",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 8774,
            "upload_time": "2022-10-02T05:04:05",
            "upload_time_iso_8601": "2022-10-02T05:04:05.326403Z",
            "url": "https://files.pythonhosted.org/packages/d0/4a/e37e9657d07cbb8d876b0a9d1745080cbe8491a502f3bde1cc2fecfd2037/flatten_everything-0.41-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "84f7856d3633c3edefe1c6db9b066f22e525d2f1f689ae08be4d52153f1f2fbb",
                "md5": "56e1f042418c1d2d43e5286f75cad02c",
                "sha256": "81918e9a1d0a7131f0bcab8fbb91a234728da6d536497befe51d5a6dbdcca507"
            },
            "downloads": -1,
            "filename": "flatten_everything-0.41.tar.gz",
            "has_sig": false,
            "md5_digest": "56e1f042418c1d2d43e5286f75cad02c",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 6660,
            "upload_time": "2022-10-02T05:04:06",
            "upload_time_iso_8601": "2022-10-02T05:04:06.819623Z",
            "url": "https://files.pythonhosted.org/packages/84/f7/856d3633c3edefe1c6db9b066f22e525d2f1f689ae08be4d52153f1f2fbb/flatten_everything-0.41.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2022-10-02 05:04:06",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "hansalemaos",
    "github_project": "flatten_everything",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "flatten-everything"
}
        
Elapsed time: 1.23859s