levelflatten


Namelevelflatten JSON
Version 0.10 PyPI version JSON
download
home_pagehttps://github.com/hansalemaos/levelflatten
Summaryperforms customizable level-wise flattening of nested iterables.
upload_time2023-07-11 03:55:35
maintainer
docs_urlNone
authorJohannes Fischer
requires_python
licenseMIT
keywords nested iterables flatten
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            
# performs customizable level-wise flattening of nested iterables.

## pip install levelflatten 

#### Tested against Windows 10 / Python 3.10 / Anaconda 


## The level_flatten function provides the following advantages:

### Customizable flattening: 

It allows you to control the flattening behavior for different data types. 
You can specify how dictionaries should be treated (by items, keys, or values) 
and which non-iterable data types should be considered for individual element flattening.

### Level-wise flattening: 

It performs flattening in a level-wise manner, allowing you to control the depth of 
flattening. You can specify the maximum number of levels to flatten, which provides 
flexibility in handling complex nested structures.

### Support for various data types: 

It handles various data types such as lists, tuples, sets, dictionaries, strings, bytes, 
frozensets, bytearrays, NumPy arrays, and more. 
This makes it suitable for working with diverse data structures.

### Maintains order: 

The function preserves the order of elements while flattening, 
ensuring that the relative order of elements is maintained in the flattened list.

```python
Args:
 iterable (iterable): The input iterable to be flattened.
 n (int, optional): The maximum number of levels to flatten. Defaults to sys.maxsize.
 dict_treatment (str, optional): Treatment for dictionaries. Can be one of "items",
	 "keys", or "values". Defaults to "items".
 consider_non_iter (tuple or list, optional): Tuple or list of data types to be
	 considered non-iterable and flattened as individual elements. Defaults to
	 (str, bytes).

Returns:
 list: A flattened list containing the elements from the input iterable.
 
 
 from levelflatten import level_flatten
 import numpy as np
 from collections import defaultdict

 d = defaultdict(list)
 d[1].append((1, 2, 3, [44, 5, 5]))
 iti = [
	 {(1, 2), (3, 4)},
	 ["a", 111, 3],
	 (1, 2),
	 d,
	 "stristristri",
	 range(100, 110),
	 455j,
	 b"xxxxaaa",
	 frozenset({1, 2, 3, 4}),
	 [
		 11,
		 [
			 222,
			 33,
			 4,
			 (333, 4, {1, 2, 3, 4}, {3333: 333}, [33312, 33323], bytearray(b"xxxxx")),
		 ],
	 ],
	 [
		 {2: 3},
		 (
			 2,
			 3,
			 4,
		 ),
		 ["babab", "dd", {10: 12}, (("bbb", 12), [333, 4, {4: 32}])],
	 ],
	 (1.2, 11, 1232),
	 bytearray(b"xxxxx"),
	 np.array([1, 2, 3, 4, 5]),
	 [None, np.nan],
	 [[True, False, True]],
 ]

 t1 = level_flatten(
	 iti,
	 n=1,
	 dict_treatment="items",
	 consider_non_iter=(str, bytes, frozenset, bytearray),
 )
 print(t1)
 # [(1, 2), (3, 4), 'a', 111, 3, 1, 2, (1, [(1, 2, 3, [44, 5, 5])]), 'stristristri', 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 455j, b'xxxxaaa', frozenset({1, 2, 3, 4}), 11, [222, 33, 4, (333, 4, {1, 2, 3, 4}, {3333: 333}, [33312, 33323], bytearray(b'xxxxx'))], {2: 3}, (2, 3, 4), ['babab', 'dd', {10: 12}, (('bbb', 12), [333, 4, {4: 32}])], 1.2, 11, 1232, bytearray(b'xxxxx'), 1, 2, 3, 4, 5, None, nan, [True, False, True]]

 t1 = level_flatten(
	 iti, n=2, dict_treatment="values", consider_non_iter=(str, bytes, dict)
 )
 print(t1)
 # [1, 2, 3, 4, 'a', 111, 3, 1, 2, (1, 2, 3, [44, 5, 5]), 'stristristri', 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 455j, b'xxxxaaa', 1, 2, 3, 4, 11, 222, 33, 4, (333, 4, {1, 2, 3, 4}, {3333: 333}, [33312, 33323], bytearray(b'xxxxx')), {2: 3}, 2, 3, 4, 'babab', 'dd', {10: 12}, (('bbb', 12), [333, 4, {4: 32}]), 1.2, 11, 1232, 120, 120, 120, 120, 120, 1, 2, 3, 4, 5, None, nan, True, False, True]


 t1 = level_flatten(
	 iti, n=3, dict_treatment="keys", consider_non_iter=(str, bytes, set, tuple)
 )
 print(t1)
 # [{(1, 2), (3, 4)}, 'a', 111, 3, (1, 2), 1, 'stristristri', 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 455j, b'xxxxaaa', 1, 2, 3, 4, 11, 222, 33, 4, (333, 4, {1, 2, 3, 4}, {3333: 333}, [33312, 33323], bytearray(b'xxxxx')), 2, (2, 3, 4), 'babab', 'dd', 10, (('bbb', 12), [333, 4, {4: 32}]), (1.2, 11, 1232), 120, 120, 120, 120, 120, 1, 2, 3, 4, 5, None, nan, True, False, True]


 t1 = level_flatten(
	 iti,
	 n=4,
	 dict_treatment="items",
	 consider_non_iter=(str, bytes, set, tuple, dict, bytearray),
 )
 print(t1)
 # [{(1, 2), (3, 4)}, 'a', 111, 3, (1, 2), (1, [(1, 2, 3, [44, 5, 5])]), 'stristristri', 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 455j, b'xxxxaaa', 1, 2, 3, 4, 11, 222, 33, 4, (333, 4, {1, 2, 3, 4}, {3333: 333}, [33312, 33323], bytearray(b'xxxxx')), {2: 3}, (2, 3, 4), 'babab', 'dd', {10: 12}, (('bbb', 12), [333, 4, {4: 32}]), (1.2, 11, 1232), bytearray(b'xxxxx'), 1, 2, 3, 4, 5, None, nan, True, False, True]


 t1 = level_flatten(
	 iti, n=sys.maxsize, dict_treatment="items", consider_non_iter=(np.array, np.ndarray)
 )
 print(t1)
 # [1, 2, 3, 4, 'a', 111, 3, 1, 2, 1, 1, 2, 3, 44, 5, 5, 's', 't', 'r', 'i', 's', 't', 'r', 'i', 's', 't', 'r', 'i', 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 455j, 120, 120, 120, 120, 97, 97, 97, 1, 2, 3, 4, 11, 222, 33, 4, 333, 4, 1, 2, 3, 4, 3333, 333, 33312, 33323, 120, 120, 120, 120, 120, 2, 3, 2, 3, 4, 'b', 'a', 'b', 'a', 'b', 'd', 'd', 10, 12, 'b', 'b', 'b', 12, 333, 4, 4, 32, 1.2, 11, 1232, 120, 120, 120, 120, 120, array([1, 2, 3, 4, 5]), None, nan, True, False, True]


```

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/hansalemaos/levelflatten",
    "name": "levelflatten",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "nested iterables,flatten",
    "author": "Johannes Fischer",
    "author_email": "aulasparticularesdealemaosp@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/ea/b2/4e0caf9c1941c40b8385cc0f3ff4fc1d79c3131d438cabd0a31d4d922c20/levelflatten-0.10.tar.gz",
    "platform": null,
    "description": "\r\n# performs customizable level-wise flattening of nested iterables.\r\n\r\n## pip install levelflatten \r\n\r\n#### Tested against Windows 10 / Python 3.10 / Anaconda \r\n\r\n\r\n## The level_flatten function provides the following advantages:\r\n\r\n### Customizable flattening: \r\n\r\nIt allows you to control the flattening behavior for different data types. \r\nYou can specify how dictionaries should be treated (by items, keys, or values) \r\nand which non-iterable data types should be considered for individual element flattening.\r\n\r\n### Level-wise flattening: \r\n\r\nIt performs flattening in a level-wise manner, allowing you to control the depth of \r\nflattening. You can specify the maximum number of levels to flatten, which provides \r\nflexibility in handling complex nested structures.\r\n\r\n### Support for various data types: \r\n\r\nIt handles various data types such as lists, tuples, sets, dictionaries, strings, bytes, \r\nfrozensets, bytearrays, NumPy arrays, and more. \r\nThis makes it suitable for working with diverse data structures.\r\n\r\n### Maintains order: \r\n\r\nThe function preserves the order of elements while flattening, \r\nensuring that the relative order of elements is maintained in the flattened list.\r\n\r\n```python\r\nArgs:\r\n iterable (iterable): The input iterable to be flattened.\r\n n (int, optional): The maximum number of levels to flatten. Defaults to sys.maxsize.\r\n dict_treatment (str, optional): Treatment for dictionaries. Can be one of \"items\",\r\n\t \"keys\", or \"values\". Defaults to \"items\".\r\n consider_non_iter (tuple or list, optional): Tuple or list of data types to be\r\n\t considered non-iterable and flattened as individual elements. Defaults to\r\n\t (str, bytes).\r\n\r\nReturns:\r\n list: A flattened list containing the elements from the input iterable.\r\n \r\n \r\n from levelflatten import level_flatten\r\n import numpy as np\r\n from collections import defaultdict\r\n\r\n d = defaultdict(list)\r\n d[1].append((1, 2, 3, [44, 5, 5]))\r\n iti = [\r\n\t {(1, 2), (3, 4)},\r\n\t [\"a\", 111, 3],\r\n\t (1, 2),\r\n\t d,\r\n\t \"stristristri\",\r\n\t range(100, 110),\r\n\t 455j,\r\n\t b\"xxxxaaa\",\r\n\t frozenset({1, 2, 3, 4}),\r\n\t [\r\n\t\t 11,\r\n\t\t [\r\n\t\t\t 222,\r\n\t\t\t 33,\r\n\t\t\t 4,\r\n\t\t\t (333, 4, {1, 2, 3, 4}, {3333: 333}, [33312, 33323], bytearray(b\"xxxxx\")),\r\n\t\t ],\r\n\t ],\r\n\t [\r\n\t\t {2: 3},\r\n\t\t (\r\n\t\t\t 2,\r\n\t\t\t 3,\r\n\t\t\t 4,\r\n\t\t ),\r\n\t\t [\"babab\", \"dd\", {10: 12}, ((\"bbb\", 12), [333, 4, {4: 32}])],\r\n\t ],\r\n\t (1.2, 11, 1232),\r\n\t bytearray(b\"xxxxx\"),\r\n\t np.array([1, 2, 3, 4, 5]),\r\n\t [None, np.nan],\r\n\t [[True, False, True]],\r\n ]\r\n\r\n t1 = level_flatten(\r\n\t iti,\r\n\t n=1,\r\n\t dict_treatment=\"items\",\r\n\t consider_non_iter=(str, bytes, frozenset, bytearray),\r\n )\r\n print(t1)\r\n # [(1, 2), (3, 4), 'a', 111, 3, 1, 2, (1, [(1, 2, 3, [44, 5, 5])]), 'stristristri', 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 455j, b'xxxxaaa', frozenset({1, 2, 3, 4}), 11, [222, 33, 4, (333, 4, {1, 2, 3, 4}, {3333: 333}, [33312, 33323], bytearray(b'xxxxx'))], {2: 3}, (2, 3, 4), ['babab', 'dd', {10: 12}, (('bbb', 12), [333, 4, {4: 32}])], 1.2, 11, 1232, bytearray(b'xxxxx'), 1, 2, 3, 4, 5, None, nan, [True, False, True]]\r\n\r\n t1 = level_flatten(\r\n\t iti, n=2, dict_treatment=\"values\", consider_non_iter=(str, bytes, dict)\r\n )\r\n print(t1)\r\n # [1, 2, 3, 4, 'a', 111, 3, 1, 2, (1, 2, 3, [44, 5, 5]), 'stristristri', 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 455j, b'xxxxaaa', 1, 2, 3, 4, 11, 222, 33, 4, (333, 4, {1, 2, 3, 4}, {3333: 333}, [33312, 33323], bytearray(b'xxxxx')), {2: 3}, 2, 3, 4, 'babab', 'dd', {10: 12}, (('bbb', 12), [333, 4, {4: 32}]), 1.2, 11, 1232, 120, 120, 120, 120, 120, 1, 2, 3, 4, 5, None, nan, True, False, True]\r\n\r\n\r\n t1 = level_flatten(\r\n\t iti, n=3, dict_treatment=\"keys\", consider_non_iter=(str, bytes, set, tuple)\r\n )\r\n print(t1)\r\n # [{(1, 2), (3, 4)}, 'a', 111, 3, (1, 2), 1, 'stristristri', 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 455j, b'xxxxaaa', 1, 2, 3, 4, 11, 222, 33, 4, (333, 4, {1, 2, 3, 4}, {3333: 333}, [33312, 33323], bytearray(b'xxxxx')), 2, (2, 3, 4), 'babab', 'dd', 10, (('bbb', 12), [333, 4, {4: 32}]), (1.2, 11, 1232), 120, 120, 120, 120, 120, 1, 2, 3, 4, 5, None, nan, True, False, True]\r\n\r\n\r\n t1 = level_flatten(\r\n\t iti,\r\n\t n=4,\r\n\t dict_treatment=\"items\",\r\n\t consider_non_iter=(str, bytes, set, tuple, dict, bytearray),\r\n )\r\n print(t1)\r\n # [{(1, 2), (3, 4)}, 'a', 111, 3, (1, 2), (1, [(1, 2, 3, [44, 5, 5])]), 'stristristri', 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 455j, b'xxxxaaa', 1, 2, 3, 4, 11, 222, 33, 4, (333, 4, {1, 2, 3, 4}, {3333: 333}, [33312, 33323], bytearray(b'xxxxx')), {2: 3}, (2, 3, 4), 'babab', 'dd', {10: 12}, (('bbb', 12), [333, 4, {4: 32}]), (1.2, 11, 1232), bytearray(b'xxxxx'), 1, 2, 3, 4, 5, None, nan, True, False, True]\r\n\r\n\r\n t1 = level_flatten(\r\n\t iti, n=sys.maxsize, dict_treatment=\"items\", consider_non_iter=(np.array, np.ndarray)\r\n )\r\n print(t1)\r\n # [1, 2, 3, 4, 'a', 111, 3, 1, 2, 1, 1, 2, 3, 44, 5, 5, 's', 't', 'r', 'i', 's', 't', 'r', 'i', 's', 't', 'r', 'i', 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 455j, 120, 120, 120, 120, 97, 97, 97, 1, 2, 3, 4, 11, 222, 33, 4, 333, 4, 1, 2, 3, 4, 3333, 333, 33312, 33323, 120, 120, 120, 120, 120, 2, 3, 2, 3, 4, 'b', 'a', 'b', 'a', 'b', 'd', 'd', 10, 12, 'b', 'b', 'b', 12, 333, 4, 4, 32, 1.2, 11, 1232, 120, 120, 120, 120, 120, array([1, 2, 3, 4, 5]), None, nan, True, False, True]\r\n\r\n\r\n```\r\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "performs customizable level-wise flattening of nested iterables.",
    "version": "0.10",
    "project_urls": {
        "Homepage": "https://github.com/hansalemaos/levelflatten"
    },
    "split_keywords": [
        "nested iterables",
        "flatten"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "8be4bd80ce6726f36fdd6cfefb7bfef23b0d1386d698b48078af4d6a8cce87c1",
                "md5": "9573ffd271046bff4ff9bb422fb20dc7",
                "sha256": "734d4978caaffb34b889c92cdd0601c3c8821ebf9b1ea7011d1c23da7f3ffc05"
            },
            "downloads": -1,
            "filename": "levelflatten-0.10-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "9573ffd271046bff4ff9bb422fb20dc7",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 8162,
            "upload_time": "2023-07-11T03:55:34",
            "upload_time_iso_8601": "2023-07-11T03:55:34.038695Z",
            "url": "https://files.pythonhosted.org/packages/8b/e4/bd80ce6726f36fdd6cfefb7bfef23b0d1386d698b48078af4d6a8cce87c1/levelflatten-0.10-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "eab24e0caf9c1941c40b8385cc0f3ff4fc1d79c3131d438cabd0a31d4d922c20",
                "md5": "884700de54ce73618f53374ec3e00bf4",
                "sha256": "a7f5876e3f3b95be4eed0f6e4d4314e8ff7fbdb6720451e6fb0a1bd017a3a100"
            },
            "downloads": -1,
            "filename": "levelflatten-0.10.tar.gz",
            "has_sig": false,
            "md5_digest": "884700de54ce73618f53374ec3e00bf4",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 6028,
            "upload_time": "2023-07-11T03:55:35",
            "upload_time_iso_8601": "2023-07-11T03:55:35.835548Z",
            "url": "https://files.pythonhosted.org/packages/ea/b2/4e0caf9c1941c40b8385cc0f3ff4fc1d79c3131d438cabd0a31d4d922c20/levelflatten-0.10.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-07-11 03:55:35",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "hansalemaos",
    "github_project": "levelflatten",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "requirements": [],
    "lcname": "levelflatten"
}
        
Elapsed time: 0.24644s