Skip to main content

No project description provided

Project description

Flatten Iterables

Introduction

Flatten Iterables uses iteration (no recursion) in order 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 reference paths for string and numeric keys.
  • Raises ValueError on circular references.
  • Iterative algorithm; hence no 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
}

You can add iterables and mapables by referencing the iterables and mappables sets.

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
}

Test

Clone the repository.

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

Change directory into the root of the repository.

cd flatten_iterables

Run the tests.

python tests/test.py -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-0.0.2.tar.gz (4.9 kB view details)

Uploaded Source

Built Distribution

flatten_iterables-0.0.2-py3-none-any.whl (3.8 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for flatten_iterables-0.0.2.tar.gz
Algorithm Hash digest
SHA256 6bd022841b2fcf78ca33ad8abf1deec7b7ef37c1106b479ffe4add73eabf42de
MD5 2f6a57b962068c953d8cf098c1ccf3e5
BLAKE2b-256 230c4836242053646cfd4657e14b19d3ef892467986f7f4bd27555d3a58c0cbb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for flatten_iterables-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 5f57b9f88e26b76276d709e4a62154d2cef73de14310cb51fc1b65a4ec257d85
MD5 768463d025453a13bd313c33cdf0105f
BLAKE2b-256 122c2c354fadd565b01710be6aba4a9a79c94515959fc1ba2d606fa4612a4316

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