Convert nested Pydantic models to flat dictionaries and back
Project description
pydantic_to_flat_dict_and_back
A Python package that converts nested Pydantic models to flat dictionaries and back, making it easier to work with nested data structures.
Installation
pip install pydantic_to_flat_dict_and_back
Features
- Convert nested Pydantic models to flat dictionaries
- Convert flat dictionaries back to nested Pydantic models
- Optional prefix handling for nested fields
- Compatibility checking for models
Requirements
- Python >=3.12
- Pydantic ~2.6.1
Usage
Basic Example
import pydantic
from pydantic_to_flat_dict_and_back import pydantic_to_flat_dict, flat_dict_to_pydantic
class A(pydantic.BaseModel):
value: float
name: str
seed: int
model_config = pydantic.ConfigDict(extra="ignore")
class B(pydantic.BaseModel):
value: float
name: str
a: A
model_config = pydantic.ConfigDict(extra="ignore")
# Create a nested model
model = B(
value=2.0,
name="nested",
a=A(value=3.0, name="nested2", seed=42)
)
# Convert to flat dictionary
flat_dict = pydantic_to_flat_dict(model, prefix_nested=True)
print(flat_dict)
# Output: {'value': 2.0, 'name': 'nested', 'a_value': 3.0, 'a_name': 'nested2', 'a_seed': 42}
# Convert back to nested model
reconstructed = flat_dict_to_pydantic(B, flat_dict, prefix_nested=True)
assert model.model_dump() == reconstructed.model_dump()
Important Notes
- All models must have
ConfigDict(extra="ignore")set - Field aliases are not supported
- When
prefix_nested=True, nested fields are prefixed with their parent field name
API Reference
pydantic_to_flat_dict
def pydantic_to_flat_dict(
model: pydantic.BaseModel,
prefix_nested: bool = False
) -> dict[str, Any]
Converts a nested Pydantic model to a flat dictionary.
flat_dict_to_pydantic
def flat_dict_to_pydantic(
model_cls: Type[PydanticType],
flat_dict: dict[str, Any],
prefix_nested: bool = False
) -> PydanticType
Reconstructs a Pydantic model from a flat dictionary.
check_pydantic_model_compatibility
def check_pydantic_model_compatibility(
model_cls: Type[PydanticType]
) -> tuple[bool, list[str]]
Checks if a model is compatible with the flattening/unflattening operations.
License
MIT
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Project details
Release history Release notifications | RSS feed
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 pydantic_to_flat_dict_and_back-1.0.0.tar.gz.
File metadata
- Download URL: pydantic_to_flat_dict_and_back-1.0.0.tar.gz
- Upload date:
- Size: 3.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.5 CPython/3.12.3 Linux/5.15.167.4-microsoft-standard-WSL2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bc6d66dfb6f4e86a0cde3377374dfd5f7c1ea16c0990f5c52ac8177791a2f64a
|
|
| MD5 |
55f7d9317a6bdcd34af17b6ed33cdd6b
|
|
| BLAKE2b-256 |
681330661583794a25f074ff1361eeb609fc9374a738d43e4e76d57a1c9649ba
|
File details
Details for the file pydantic_to_flat_dict_and_back-1.0.0-py3-none-any.whl.
File metadata
- Download URL: pydantic_to_flat_dict_and_back-1.0.0-py3-none-any.whl
- Upload date:
- Size: 4.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.5 CPython/3.12.3 Linux/5.15.167.4-microsoft-standard-WSL2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
06320f292a78746ec6643f54d552c4e55cd58bae8dab1d8a7143281482428842
|
|
| MD5 |
6d5a5fb86f3205e765fbb2cc0ef3ed9d
|
|
| BLAKE2b-256 |
b4cca62b0c1212b965bcc031923f2530ba5bc083554367090d480d3c7d4eee1d
|