Skip to main content

pydantify-common

A Python library providing common base classes and utilities for building Pydantic-based models with XML serialization support. This package serves as the foundation for the pydantify project, enabling seamless conversion between Pydantic models and XML representations.

Features

  • Base Model Classes: PydantifyModel and XMLPydantifyModel for creating structured data models
  • XML Serialization: Convert Pydantic models to XML with namespace support
  • NETCONF Compatibility: Built-in support for NETCONF data root elements
  • Type Safety: Full type hints and Pydantic v2 support
  • Flexible Output: Pretty-print and custom root element options

Installation

pip install pydantify-common

Requirements

  • Python >= 3.10
  • pydantic >= 2
  • lxml >= 6.0.2

Quick Start

Basic Model Definition

Make sure the pydantic model inherits from XMLPydantifyModel and defines namespace and prefix as class variables.

from pydantify_common.model import XMLPydantifyModel

class MyModel(XMLPydantifyModel):
    namespace = "http://example.com/schema"
    prefix = "ex"
    
    name: str
    value: int

XML Serialization

from pydantify_common.helper import model_dump_xml_string

model = MyModel(name="test", value=42)
xml_str = model_dump_xml_string(model, pretty_print=True)
print(xml_str)

NETCONF Data Root

# Add NETCONF-compatible data root element
xml_str = model_dump_xml_string(model, pretty_print=True, data_root=True)

API Reference

PydantifyModel

Base class for all pydantify models. Inherits from Pydantic's BaseModel.

class PydantifyModel(BaseModel):
    pass

XMLPydantifyModel

Extended base class with XML serialization capabilities.

Class Variables:

  • namespace (str): XML namespace URI
  • prefix (str): XML namespace prefix

Methods:

  • model_dump_xml() -> Element: Returns an lxml Element representation
  • fields_to_elements(container_name: str | None = None) -> Element: Converts model fields to XML elements

model_dump_xml_string()

Helper function to serialize XMLPydantifyModel instances to XML strings.

def model_dump_xml_string(
    model: XMLPydantifyModel,
    *,
    pretty_print: bool = False,
    data_root: bool = False
) -> str

Parameters:

  • model: The XMLPydantifyModel instance to serialize
  • pretty_print: Enable formatted XML output (default: False)
  • data_root: Add NETCONF-compatible <data> root element (default: False)

Returns: XML string representation of the model

Examples

The project includes comprehensive examples in the tests/examples/ directory:

  • with_augment: Demonstrates augmented YANG models with namespace support
  • with_import_uses: Shows handling of YANG imports and type reuse

Run tests to see examples in action:

uv run pytest tests/test_examples.py -vvv

Development

Setup Development Environment

uv sync

Running Tests

make tests

Code Quality

# Type checking
make mypy

# Linting and formatting
make ruff

Contributing

Contributions are welcome! Please ensure all tests pass and code follows the project's style guidelines before submitting a pull request.

Metadata

Release files for pydantify-common 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pydantify-common 0.1.0
File Size Uploaded
pydantify_common-0.1.0.tar.gz 50.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pydantify-common 0.1.0
File Interpreter ABI Platform
pydantify_common-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 54.1 kB

Release files / pydantify_common-0.1.0.tar.gz

Download URL pydantify_common-0.1.0.tar.gz
Size 50.2 kB
Tags Source
SHA-256 checksum
How to use checksums
28760cccd9d3b8f0026aa00e4ece8a102f52d91f3b71f58a331174ed54c20e96
BLAKE2b-256 checksum
How to use checksums
1f201106a3bb93fd9a6591b9bb24e018ad698f5a0d122ea0986b403b74566708
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.7

Release files / pydantify_common-0.1.0-py3-none-any.whl

Download URL pydantify_common-0.1.0-py3-none-any.whl
Size 3.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
fae25526f58fcc975d05c2f23e7f4fd8b9262c0a0c930a8a0cb2f9c4d10e6c2a
BLAKE2b-256 checksum
How to use checksums
586490416fc8f24960b864394fd53937b2fbc01d8f1c8793e612d0b7702ede8b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.7

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page