flat-adapter
flat-adapter converts nested mappings into deterministic flat rows suitable
for database imports and ETL pipelines.
Quick start
from flat_adapter import FlatAdapter
class ItemAdapter(FlatAdapter):
item_id: int
quantity: int
class OrderAdapter(FlatAdapter):
order_id: int
items: list[ItemAdapter]
rows = OrderAdapter.adapt(
{
"order_id": "1001",
"items": [
{"item_id": "10", "quantity": "2"},
{"item_id": "20", "quantity": "1"},
],
}
)
assert rows == [
{"order_id": 1001, "item_id": 10, "quantity": 2},
{"order_id": 1001, "item_id": 20, "quantity": 1},
]
The first release supports Mapping[str, object] input, nested mappings,
typed scalar conversion, optional fields, aliases, custom Field paths, and
deterministic Cartesian expansion of nested lists. adapt() returns
list[dict[str, object]]; iter_adapt() returns an iterator over the same
rows.
Field configuration
Use typing.Annotated to attach extraction metadata without a runtime
assignment or a cast:
from typing import Annotated
from flat_adapter import Field, FlatAdapter
class CustomerAdapter(FlatAdapter):
customer_id: Annotated[int, Field(source="payload.customer_id")]
display_name: Annotated[str, Field(source="payload.name", default="Unknown")]
The legacy name: int = Field(...) form remains supported at runtime, but
Annotated is the preferred form for mypy --strict projects.
Performance and row limits
Depth of seven to ten nested adapters is normally safe; the main cost comes
from Cartesian expansion. For list lengths L1, L2, ..., the result count
can grow as product(max(1, len(Li))). adapt() materializes all rows in
memory, while iter_adapt() yields them lazily.
Use max_rows to fail fast before an expansion becomes too large. The limit
works with both eager and lazy APIs:
rows = OrderAdapter.adapt(payload, max_rows=10_000)
For large results, consume rows lazily:
for row in OrderAdapter.iter_adapt(payload, max_rows=10_000):
process(row)
Run the local benchmark scenarios with:
uv run python benchmarks/flatten_benchmark.py
Development
The project uses uv for environments, dependencies, and lockfile management.
uv sync
uv run pre-commit install
uv run pre-commit run --all-files
uv run pytest --cov=flat_adapter --cov-report=term-missing
uv run ruff check src tests
uv run mypy --strict src tests
uv build
uv run twine check dist/*
Package layout
src/flat_adapter/ Library package
tests/unit/ Pure behavior tests
docs/ English/Russian guides and promotion notes
benchmarks/ Manual performance scenarios
See CONTEXT.md for architecture boundaries and TECHDEBT.md for known risks.
Русская версия руководства: docs/README.ru.md. English guide: docs/README.en.md.
Versioning
Releases follow Semantic Versioning. While the package is below 1.0.0, a
minor release may include a documented contract change; patch releases remain
backward-compatible fixes. CHANGELOG.md records user-visible changes.
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file flat_adapter-0.2.0.tar.gz.
File metadata
- Download URL: flat_adapter-0.2.0.tar.gz
- Upload date:
- Size: 90.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2d082ba6c15ee17f544efb253aaefe8f71e0687e7fa1a5fecd73738dc00cffb4
|
|
| MD5 |
d81c7c78ae9a7b3a17026ea7876fd21f
|
|
| BLAKE2b-256 |
1c8f84cf7dd4dfcf51d03de1270e58eea1a7e56ad651471f585181a633a8312c
|
Provenance
The following attestation bundles were made for flat_adapter-0.2.0.tar.gz:
Publisher:
release.yml on groshevpavel/flat_adapter
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
flat_adapter-0.2.0.tar.gz -
Subject digest:
2d082ba6c15ee17f544efb253aaefe8f71e0687e7fa1a5fecd73738dc00cffb4 - Sigstore transparency entry: 2382190530
- Sigstore integration time:
-
Permalink:
groshevpavel/flat_adapter@7bf1dc742c0db30eee839e8e034b5067778598d0 -
Branch / Tag:
refs/heads/master - Owner: https://github.com/groshevpavel
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@7bf1dc742c0db30eee839e8e034b5067778598d0 -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file flat_adapter-0.2.0-py3-none-any.whl.
File metadata
- Download URL: flat_adapter-0.2.0-py3-none-any.whl
- Upload date:
- Size: 11.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b7dadbe0b78bfd28520ce3ee04b4c1f09a3a4635d9201055cbad09f86018a32d
|
|
| MD5 |
5ea40594e54ca93d59b34dfe8d8ecde6
|
|
| BLAKE2b-256 |
9c5ebcb860ae88b059e87b69a59fd817e4dc218cc821e396c1d58ef650ad6f6f
|
Provenance
The following attestation bundles were made for flat_adapter-0.2.0-py3-none-any.whl:
Publisher:
release.yml on groshevpavel/flat_adapter
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
flat_adapter-0.2.0-py3-none-any.whl -
Subject digest:
b7dadbe0b78bfd28520ce3ee04b4c1f09a3a4635d9201055cbad09f86018a32d - Sigstore transparency entry: 2382190671
- Sigstore integration time:
-
Permalink:
groshevpavel/flat_adapter@7bf1dc742c0db30eee839e8e034b5067778598d0 -
Branch / Tag:
refs/heads/master - Owner: https://github.com/groshevpavel
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@7bf1dc742c0db30eee839e8e034b5067778598d0 -
Trigger Event:
workflow_dispatch
-
Statement type: