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
from flatten_iterables import flatten, mappables, iterables

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

print(json.dumps(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
}

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

Uploaded Source

Built Distribution

flatten_iterables-0.0.1-py3-none-any.whl (3.6 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for flatten_iterables-0.0.1.tar.gz
Algorithm Hash digest
SHA256 b4dbd6a45f9397d85d23251464abe6802e1094a4e5c2b02a3547416b24c2b338
MD5 39319e865b25f16d97eb8c376cc3adb1
BLAKE2b-256 c279f73f4a72103d7119efc28a26db83f9e15bb4f849a57f07c3c542057c2b86

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for flatten_iterables-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 6f4a799ac03081c22bc5c3cecc7124871bb6a939aae3f15b9c8688f0be01c89d
MD5 ad324bc5628451f71f30f2237d5c9a2e
BLAKE2b-256 62aa980968af3f79575bb2fee107ec2e724a4a61da0883e02efbbb3dcb8feb42

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