Skip to main content

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

  1. All models must have ConfigDict(extra="ignore") set
  2. Field aliases are not supported
  3. 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pydantic_to_flat_dict_and_back-1.0.0.tar.gz (3.9 kB view details)

Uploaded Source

Built Distribution

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

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

Hashes for pydantic_to_flat_dict_and_back-1.0.0.tar.gz
Algorithm Hash digest
SHA256 bc6d66dfb6f4e86a0cde3377374dfd5f7c1ea16c0990f5c52ac8177791a2f64a
MD5 55f7d9317a6bdcd34af17b6ed33cdd6b
BLAKE2b-256 681330661583794a25f074ff1361eeb609fc9374a738d43e4e76d57a1c9649ba

See more details on using hashes here.

File details

Details for the file pydantic_to_flat_dict_and_back-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for pydantic_to_flat_dict_and_back-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 06320f292a78746ec6643f54d552c4e55cd58bae8dab1d8a7143281482428842
MD5 6d5a5fb86f3205e765fbb2cc0ef3ed9d
BLAKE2b-256 b4cca62b0c1212b965bcc031923f2530ba5bc083554367090d480d3c7d4eee1d

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