Skip to main content

Literal flatten(or unflatten) for data structures

Project description

lflatten

Biblioteca Python simples, tipada e reversível para flatten e unflatten de estruturas aninhadas (dict e list).

O foco do projeto é:

  • simplicidade
  • previsibilidade
  • round-trip garantido (unflatten(flatten(x)) == x)
  • suporte a separador customizável
  • suporte a escape de separador nas chaves
  • tipagem estática clara

Instalação

Projeto local / em desenvolvimento:

pip install -e .

Ou, se publicado futuramente:

pip install lflatten

Uso básico

Documentação

Flatten

from py_simple_flatten import flatten

data = {
    "foo": {
        "bar": 10
    }
}

flatten(data)

Resultado:

{
    "foo.bar": 10
}

Unflatten

from py_simple_flatten import unflatten

flat = {
    "foo.bar": 10
}

unflatten(flat)

Resultado:

{
    "foo": {
        "bar": 10
    }
}

Separador customizado (sep)

Exemplo com sep=":"

data = {
    "foo:bar": {
        "baz": 10
    }
}

flatten(data, sep=":")

Resultado:

{
    "foo\\:bar:baz": 10
}

E o round-trip funciona corretamente:

unflatten(
    {"foo\\:bar:baz": 10},
    sep=":"
)

Resultado:

{
    "foo:bar": {
        "baz": 10
    }
}

Escape de separador

  • O separador dentro da chave é automaticamente escapado
  • :\:
  • .\.
  • \\\

Isso garante que o unflatten reconstrua a estrutura original sem ambiguidade.


Listas

Listas são suportadas e reconstruídas corretamente:

data = {
    "a": [1, {"b": 2}]
}

flatten(data)

Resultado:

{
    "a.0": 1,
    "a.1.b": 2
}

ignore_none

Ignora valores None durante o flatten:

flatten(
    {"a": None, "b": 1},
    ignore_none=True
)

Resultado:

{
    "b": 1
}

Tipos

Tipos principais (simplificados):

Flatten = dict[str, Any]
Iter = dict | list

A API é compatível com mypy / pyright.


Garantias do projeto

  • ✅ Flatten profundo (dict + list)
  • ✅ Unflatten reversível
  • ✅ Separador customizável
  • ✅ Escape seguro de chaves
  • ✅ Tipagem estática
  • ✅ Sem dependências externas

Quando usar

  • Serialização de estruturas complexas
  • Logs
  • Armazenamento chave-valor
  • Normalização de dados
  • Conversão para DataFrame / CSV

Licença

MIT


Status

Projeto pequeno, estável e intencionalmente minimalista. Contribuições são bem-vindas se mantiverem o foco em simplicidade e previsibilidade.

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

lflatten-0.1.0.tar.gz (6.6 kB view details)

Uploaded Source

Built Distribution

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

lflatten-0.1.0-py3-none-any.whl (7.0 kB view details)

Uploaded Python 3

File details

Details for the file lflatten-0.1.0.tar.gz.

File metadata

  • Download URL: lflatten-0.1.0.tar.gz
  • Upload date:
  • Size: 6.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.0

File hashes

Hashes for lflatten-0.1.0.tar.gz
Algorithm Hash digest
SHA256 d2d49137a474fd06a1e88120399a95d59f370c29e199e45bbe1b860ae5dc3bb4
MD5 cdc00b72a70a82e2d1b6b0740ed17cda
BLAKE2b-256 fd948c6eb6779bc640f342dc7920f945a43c365427e55a534cc81f34c0ba2bb5

See more details on using hashes here.

File details

Details for the file lflatten-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: lflatten-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 7.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.0

File hashes

Hashes for lflatten-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 347c9eeac386a369a7d9471c38e75ceb772963bf1e8059b13e5c35531de6289f
MD5 609a70dcf4420f20b3aac8eabd6843ff
BLAKE2b-256 74f508639128892c5da74853eb983fad1142abd5363735937769aef828f45986

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