Skip to main content

Pydantify common classes and functions

Project description

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.

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

pydantify_common-0.1.0.tar.gz (50.2 kB view details)

Uploaded Source

Built Distribution

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

pydantify_common-0.1.0-py3-none-any.whl (3.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pydantify_common-0.1.0.tar.gz
  • Upload date:
  • Size: 50.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pydantify_common-0.1.0.tar.gz
Algorithm Hash digest
SHA256 28760cccd9d3b8f0026aa00e4ece8a102f52d91f3b71f58a331174ed54c20e96
MD5 ea939659e3e3838eae7d815b4e5f7baa
BLAKE2b-256 1f201106a3bb93fd9a6591b9bb24e018ad698f5a0d122ea0986b403b74566708

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pydantify_common-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 fae25526f58fcc975d05c2f23e7f4fd8b9262c0a0c930a8a0cb2f9c4d10e6c2a
MD5 f85320d95f73561e17cdac8d02ccdb35
BLAKE2b-256 586490416fc8f24960b864394fd53937b2fbc01d8f1c8793e612d0b7702ede8b

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