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
from flatten_iterables import fi

fi.key_style = "python"

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

print(json.dumps(fi.flatten(data), indent=2))
{
  "['dict']['a']": 42,
  "['nested_dicts']['a0']['b0']": 42,
  "['nested_dicts']['a0']['b1']": 23,
  "['empty_dict']": {},
  "['list'][0]": 42,
  "['nested_lists'][0][0]": 42,
  "['empty_list']": [],
  "[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
from flatten_iterables import 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

How to run the 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.3.tar.gz (5.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

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

Uploaded Python 3

File details

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

File metadata

  • Download URL: flatten_iterables-1.0.3.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.3.tar.gz
Algorithm Hash digest
SHA256 a5ad243010d64701a56198cce6d57eac75c9ac56c95a4c6764c003f38288e935
MD5 30a7fe27b333c8b725271adf64004b4e
BLAKE2b-256 96aa71360a75a549e08f0dfcb3050ece252e140893bc6e64d7c4430532f06399

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for flatten_iterables-1.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 7dea830a52b88048b25e72eb7bb515fdc1bad900005b94f72e48d4f6af6d6f27
MD5 83a031354b2d2abea7c275b525ae5189
BLAKE2b-256 e0cde50edf5c2890277c51b9b330b109b1f9b3b072f0261d6cd8be9081649989

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page