Skip to main content

A Python library to flatten a dictionary with nested dictionnaries and lists

Project description

dictflat

A Python library to flatten a dictionary with nested dictionnaries and lists

Use cases

Transform a dictionary structure into a new organization ready to be inserted into a relational database.

Installation

poetry add dictflat

Quick start

>>> from dictflat import DictFlat
>>> import json
>>> r = DictFlat(
    root_key="root"
).flat(
    d={
        "name": "John",
        "pers_id": 12,
        "birth": {
            "address": {
                "street": "123 Main St",
                "city": "Anytown",
                "state": "CA"
            },
            "date": "10/06/1976 01:10:35"
        },
        "Phone_Numbers": [
            {"type": "home", "number": "555-1234"},
            {"type": "work", "number": "555-5678"},
        ],
    }
)
>>> print("%s" % json.dumps(r, indent=2))
{
  "root": [
    {
      "__id": "662783f7-b1a0-4e8c-9de9-f3a72a896d4c",
      "name": "John",
      "pers_id": 12
    }
  ],
  "root.birth": [
    {
      "__id": "e72d549a-89f5-4208-99c0-4ce3493cbf9e",
      "__ref__root": "662783f7-b1a0-4e8c-9de9-f3a72a896d4c",
      "date": "10/06/1976 01:10:35"
    }
  ],
  "root.birth.address": [
    {
      "__id": "cc489c03-82ca-4b6e-a620-32c9c4be236c",
      "__ref__root.birth": "e72d549a-89f5-4208-99c0-4ce3493cbf9e",
      "street": "123 Main St",
      "city": "Anytown",
      "state": "CA"
    }
  ],
  "root.Phone_Numbers": [
    {
      "__id": "ba1560de-9c4c-4886-b4ca-684e0a7e5df0",
      "__ref__root": "662783f7-b1a0-4e8c-9de9-f3a72a896d4c",
      "type": "home",
      "number": "555-1234"
    },
    {
      "__id": "f1032025-6c7d-4341-8e6a-f0dce2374388",
      "__ref__root": "662783f7-b1a0-4e8c-9de9-f3a72a896d4c",
      "type": "work",
      "number": "555-5678"
    }
  ]
}

The result is always a dictionary where each key is a reference to the original dictionary.

  • In this example, the original root document is identified by the token “root” (the root key) and the "address" sub-dictionary is identified by “root.address”.

Each dictionary value is always a list. See below for more examples with more than one element in lists.

Each sub-dictionnary have:

  • an unique field named "__id" (like a primary key)
  • except for root, a "__ref__root" who contains the "__id" value of parent dictionnary;
    • the "root" token in "__ref__root" field name is directly a reference to the global result dictionnary.

Documentation

All examples explained in documentation come from module tests.

  1. Basic usages
  2. Nested dictionnaries
  3. Generate IDs
  4. Change values
  5. Drop fields
  6. Rename fields
  7. Lists
  8. Squash dictionnaries
  9. Simple keys

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

dictflat-0.1.6.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.

dictflat-0.1.6-py3-none-any.whl (6.2 kB view details)

Uploaded Python 3

File details

Details for the file dictflat-0.1.6.tar.gz.

File metadata

  • Download URL: dictflat-0.1.6.tar.gz
  • Upload date:
  • Size: 5.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.13.3 Linux/6.14.9-200.fc41.x86_64

File hashes

Hashes for dictflat-0.1.6.tar.gz
Algorithm Hash digest
SHA256 56cabf9419c914c2e6ad1300da5a8700d91b34e0a50024bd863b1270b995041e
MD5 e49fe20b3c51202b7b8385ff0b89b61c
BLAKE2b-256 7e3ed0122b64d989b6ea42ae89c5f5ac173f02f190b25d7859d0c6297c562181

See more details on using hashes here.

File details

Details for the file dictflat-0.1.6-py3-none-any.whl.

File metadata

  • Download URL: dictflat-0.1.6-py3-none-any.whl
  • Upload date:
  • Size: 6.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.13.3 Linux/6.14.9-200.fc41.x86_64

File hashes

Hashes for dictflat-0.1.6-py3-none-any.whl
Algorithm Hash digest
SHA256 14ae99d73d4cf31d4bd78b2d934aa82ae7423afbf1d35eb9af95b18ccdafa110
MD5 0bde57097e180b1c48ee40cd577f7400
BLAKE2b-256 fecbbf5423d761bf847efe486b6738d695c756cb85c676fb4d7dd7defecc035b

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