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 structures 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.1.tar.gz (5.4 kB view details)

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

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

File metadata

  • Download URL: flatten_iterables-1.0.1.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.1.tar.gz
Algorithm Hash digest
SHA256 46c976b29905b96940684deac9b98d8af76ac1038993cf711e72a89dc6a00907
MD5 4e63063b9e849e317c5068d4c45fd474
BLAKE2b-256 c5586786a6a841ba6273937a4047ad1e4cccee073c75dcb65a7d203a9f93716f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for flatten_iterables-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 bfea8b1ecbeaa7e8dc92ad2bf53618fd4f7eda32248a98ff8287a2cc24fd0c01
MD5 5dda3e2aed176966027b960a1f3babea
BLAKE2b-256 08e9fb1db8e0214f624b6ae0e7564bfbbec2334c2deb4d72b5539078712a23e0

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