Skip to main content

Flatten iterables and mappings into a dictionary of reference paths and values.

Project description

Flatten It

Introduction

Flatten It uses iteration (no recursion) to flatten iterables into a dictionary where each key is a reference path and each value is the value at the respective path.

Features

  • Produces valid Python or JS reference paths for string and numeric keys.
  • Raises ValueError on circular references.
  • Iterative algorithm; flatten deeply nested structures without causing a call stack overflow.

Table of Contents

Installation

pip install flatten_iterables

Usage

In this example, an object named data will be flattened into a dictionary of reference paths and their respective values. The resulting dictionary will be dumped to a JSON string.

import json
import flatten_iterables as fi

data = {
    "dict": {"a": 42},
    "list": [42],
    "nested_dicts": {"a0": {"b0": 42, "b1": 23}},
    "nested_lists": [
        [
            42,
        ],
    ],
    ...: 42,
}

print(json.dumps(fi.flatten(data), indent=2))
{
  "['dict']['a']": 42,
  "['list'][0]": 42,
  "['nested_dicts']['a0']['b0']": 42,
  "['nested_dicts']['a0']['b1']": 23,
  "['nested_lists'][0][0]": 42,
  "[Ellipsis]": 42
}

By default Flatten It will flatten structures that contain instances of list and dict. However, you can flatten stuctures containing other types of iterables and mappings by adding their respective types to the iterables and mappables sets.

In this example, a structure containing the types set and OrderedDict will be flattened. The type set is added to the iterables set and the type OrderedDict is added to the mappables set.

import json
import flatten_iterables as fi
from collections import OrderedDict

fi.iterables.add(set)
fi.mappables.add(OrderedDict)

data = {
    "set": {23, 42, 57},
    "ordered_dict": OrderedDict(a=23, b=42, c=57),
}

print(json.dumps(fi.flatten(data), indent=2))
{
  "['set'][0]": 57,
  "['set'][1]": 42,
  "['set'][2]": 23,
  "['ordered_dict']['a']": 23,
  "['ordered_dict']['b']": 42,
  "['ordered_dict']['c']": 57
}

API

fi.flatten(it)

  • it Union[Iterable, Mapping] The iterable or mapping to be flattened.

fi.key_style Literal["python", "js"] Specify a key style. Default: python

fi.iterables Set[Iterables] Add iterable candidates to this set. Default: (list)

fi.mappables Set[Mapping] Add mappable candidates to this set. Default: {dict}

Test

Clone the repository.

git clone https://github.com/faranalytics/flatten_iterables.git

Change directory into the root of the repository.

cd flatten_iterables

Install the package in editable mode.

pip install -e .

Run the tests.

python -m unittest -v

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

flatten_iterables-1.0.0.tar.gz (5.4 kB view details)

Uploaded Source

Built Distribution

flatten_iterables-1.0.0-py3-none-any.whl (4.2 kB view details)

Uploaded Python 3

File details

Details for the file flatten_iterables-1.0.0.tar.gz.

File metadata

  • Download URL: flatten_iterables-1.0.0.tar.gz
  • Upload date:
  • Size: 5.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.27.2

File hashes

Hashes for flatten_iterables-1.0.0.tar.gz
Algorithm Hash digest
SHA256 1e27afbc1dba1c0b4257f091f3fac724a40c2ed7da918ce6c0d1ae36fdaf2076
MD5 667b68682d7b0227934f84f253c72fea
BLAKE2b-256 9033155065e87053d8912369b5c1e601aa25a69a7d5837aded03292a5594dc95

See more details on using hashes here.

File details

Details for the file flatten_iterables-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for flatten_iterables-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 bf7e5421bf729bbfa4d89870ed5d7dee5f7a32a0144203d7ea8abd44b15a9d95
MD5 c7a9cbd6fae712ee8c4f42fe13b4e665
BLAKE2b-256 5653617a056af0aa7ad4a0e5b36961fc35d32204f59ed2360687af73ec305d1a

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