Skip to main content

Flatten nested python objects.

Project description

pyflattener

One useful utility for making Swimlane Bundles is the Flattener, which helps to simplify complex JSON.

Basic Usage

from pyflattener import Flattener, do_flatten

data = {
    "outer_key": {
        "inner_key1": "inner val 1",
        "inner_key2": "inner val 2"
    },
    "basic_key": "value",
    "basic_list": ["1", "2", "3"],
    "mixed_list": [1, "2", "3"],
    "basic_list2": [1, 2, 3]
}

# Simple flatten
flat_data = do_flatten(data)

# Instance analogue
flat_data = Flattener(prefix=None, stringify_lists=True, shallow_flatten=False,
                      keep_simple_lists=True, autoparse_dt_strs=True).flatten(data)

flat_data will now look like:

{
'outer_key_inner_key1': 'inner val 1',
'basic_list': ['1', '2', '3'],  # Simple list kept as a list
'basic_key': 'value',
'outer_key_inner_key2': 'inner val 2',
'basic_list2': [1, 2, 3],  # Simple list kept as list
'mixed_list': '1,2,3'  # Nonsimple list CVSV'd
}

Here is a description of the params you can pass to a Flattener() object or do_flatten

Prefix

Prefix to add to the data after flattening

do_flatten({"a": 5}, prefix="my_prefix")
# {"my_prefix_a": 5}

Stringify Lists

Turn lists with basic types into CSV, defaults to True. This option is ignored for simple lists if keep_simple_lists is True

stringify = <True or False>
do_flatten({"a": [1,2,3]}, stringify_lists=stringify, keep_simple_lists=False)
# True -> {"a": "1,2,3"}
# False -> {"a": [1,2,3]}

Shallow Flatten

Ignore the first level of nesting, and only flatten each element within it. Used for lists of dictionaries

data = [
    {"a": { "sub_a": 1 }, "b": 5},
    {"a": { "sub_a": 2 }, "b": 6},
]
shallow = <True or False>
do_flatten(data, shallow_flatten=shallow)
# True -> [
#    {"a_sub_a": 1, "b": 5},
#    {"a_sub_a": 2, "b": 6}
# ]

# False -> {"a_sub_a": [1,2], "b": [5,6]}

Keep Simple Lists

If a list in the resulting flattened dict is only integers or only strings, even if stringify_lists is True, keep this list

simple = <True or False>
do_flatten({"a": [1,2,3], "b": ["c", 4]}, keep_simple_lists=simple)
# True -> {"a": [1,2,3], "b": "c,4"}
# False -> {"a": "1,2,3", "b": "c,4"}

Misc Flattening Functions

There are many useful flattening functions for more complicated data

Hoist Key(s)

Grab keys from a list of dicts

hoist_key("a", [{"a": 5}, {"a": 6}])
# -> [5, 6]

hoist_keys(["a", "b"], [{"a": 5, "b": 1}, {"a": 6, "b": 2}])
# -> [[5, 6], [1, 2]]

Replace Dict Prefix

Replace a prefix in a dictionary

replace_dict_prefix("aaa", "bbbb", {"aaa_data": 5})
# -> {'bbbb_data': 5}
# Or more commonly like:

replace_dict_prefix("aaa_", "", {"aaa_data": 5})
# -> {'data': 5}

Merge Dicts

Merge two dictionaries together, regardless if they share keys or not. If they share keys, it uses combine_listdict

merge_dicts({"a": 1}, {"b": 2})
# -> {"a": 1, "b": 2}

merge_dicts({"a": 1}, {"a": 2})
# -> {"a": [1, 2]})

Is SimpleList

Check if a list is purely of integers or purely of strings

is_simplelist([1,2,3])
# -> True
is_simplelist([1,2,"3"])
# -> False

Flatten Single Lists

Flatten all keys in a dict that are lists with a single entry

flatten_single_lists({"a": [1,2,3], "b": [5]})
# -> {"b": 5,"a": [1, 2, 3]}

Combine ListDict

Combine a list of dictionaries into a single dictionary

combine_listdict([{"a": 1},{"a": 2}, {"a": 3}])
# -> {"a": [1, 2, 3]}

complicated_data = [
    {
        "a": "entry 1",
        "b": "v1"
    },
    {
        "b": "v2"
    },
    {
        "a": "entry 2"
    }
]
combine_listdict(complicated_data)
# -> {
#   'a': ['entry 1', None, 'entry 2'],
#   'b': ['v1', 'v2', None]
#}

Note how the missing entries were filled in with None This is to ensure the ordering of elements can be obtained in the flattened dict result.

Also note that attempting to combine a list of dictionaries with nonbasic keys (subdicts or lists) can lead to odd results, or not be possible to combine in that form

Clean XMLToDict Result

XMLToDict returns very ugly data, this helps clean it up. It only cleans top-level keys, so it is most effective after flattening

import xmltodict

ugly_xml = "<xml><key attr=\"5\">val</key></xml>"
xml_dict = xmltodict.parse(ugly_xml)
clean_xmltodict_result(do_flatten(xml_dict))
# -> {u'xml_key_text': u'val', u'xml_key_attr': "5"}

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

pyflattener-1.0.1.tar.gz (12.0 kB view details)

Uploaded Source

Built Distribution

pyflattener-1.0.1-py3-none-any.whl (11.2 kB view details)

Uploaded Python 3

File details

Details for the file pyflattener-1.0.1.tar.gz.

File metadata

  • Download URL: pyflattener-1.0.1.tar.gz
  • Upload date:
  • Size: 12.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.9.6 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/1.0.0 urllib3/1.26.18 tqdm/4.64.1 importlib-metadata/4.8.3 keyring/23.4.1 rfc3986/1.5.0 colorama/0.4.5 CPython/3.6.15

File hashes

Hashes for pyflattener-1.0.1.tar.gz
Algorithm Hash digest
SHA256 1752138a37e55d26f74a392f53ec5ee2b33c99a81827fd2497494364ff8941ed
MD5 67bc404bb9da43954771cc386e8b25f4
BLAKE2b-256 8dd34839442ef3c06ec8e27e1aec480825de481c4d493ad6f9d37eb551efadf0

See more details on using hashes here.

File details

Details for the file pyflattener-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: pyflattener-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 11.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.9.6 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/1.0.0 urllib3/1.26.18 tqdm/4.64.1 importlib-metadata/4.8.3 keyring/23.4.1 rfc3986/1.5.0 colorama/0.4.5 CPython/3.6.15

File hashes

Hashes for pyflattener-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 5993cfd1e484344caa17c14fe32ed57ea3047428d19ff8ee2c385b47d1eae29b
MD5 92e4df1a2378bbaed16ad2906fa92f7a
BLAKE2b-256 6b0846fdb55e965c11e21600d5bf0e8b8919a66c154cfe1dd543b32a2eb4352f

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