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:
PydantifyModelandXMLPydantifyModelfor creating structured data models - XML Serialization: Convert Pydantic models to XML with namespace support
- YANG-Compatible: Handles
RootModel,Enum, andserialization_aliasfor YANG-generated models - Wrapper Elements: Add outer YANG path wrappers (e.g.,
System,config) to serialized output - 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 typing import Annotated, ClassVar
from pydantic import Field
from pydantify_common.model import XMLPydantifyModel
class MyModel(XMLPydantifyModel):
model_config = {"populate_by_name": True}
namespace: ClassVar[str] = "http://example.com/schema"
prefix: ClassVar[str] = "ex"
# Use serialization_alias for camelCase/hyphenated XML tags
device_name: Annotated[
str,
Field(serialization_alias="ex:deviceName")
]
value: Annotated[int, Field(serialization_alias="ex:value")]
XML Serialization
from pydantify_common.helper import model_dump_xml_string
model = MyModel(device_name="router1", value=42)
xml_str = model_dump_xml_string(model, pretty_print=True)
# Output: <MyModel><deviceName>router1</deviceName><value>42</value></MyModel>
NETCONF Data Root
# Add NETCONF-compatible data root element
xml_str = model_dump_xml_string(model, pretty_print=True, data_root=True)
Wrapper Elements
from pydantify_common.helper import XmlWrapper
# Wrap model in YANG path elements (e.g., System/config)
xml_str = model_dump_xml_string(
model,
pretty_print=True,
data_root=True,
wrappers=[
XmlWrapper("System", "http://example.com/schema"),
XmlWrapper("config", "http://example.com/schema"),
]
)
# Output: <data><System><config><MyModel>...</MyModel></config></System></data>
NETCONF Operations
Add NETCONF operation attributes (operation="replace", etc.) by modifying the Element after serialization:
from lxml import etree
from pydantify_common import model_dump_xml
# Get Element object
element = model_dump_xml(model, data_root=True)
# Add operation attribute to specific elements
monitor_items = element.find('monitor-items')
if monitor_items is not None:
monitor_items.set('operation', 'replace')
# Serialize to string
xml_str = etree.tostring(element, encoding=str, pretty_print=True)
# Output: <monitor-items operation="replace">...</monitor-items>
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(ClassVar[str]): XML namespace URIprefix(ClassVar[str]): XML namespace prefix
Methods:
model_dump_xml() -> Element: Returns an lxml Element representation
XmlWrapper
Named tuple for specifying wrapper elements.
class XmlWrapper(NamedTuple):
tag: str # Element tag name
namespace: str # XML namespace URI
model_dump_xml()
Serialize model to lxml Element for direct manipulation.
def model_dump_xml(
model: XMLPydantifyModel,
*,
data_root: bool = False,
wrappers: list[XmlWrapper] | None = None
) -> Element
Parameters:
model: The XMLPydantifyModel instance to serializedata_root: Add NETCONF-compatible<data>root element (default: False)wrappers: Optional list of wrapper elements, specified outermost-first (default: None)
Returns: lxml Element object
model_dump_xml_string()
Serialize model to XML string (calls model_dump_xml() internally).
def model_dump_xml_string(
model: XMLPydantifyModel,
*,
pretty_print: bool = False,
data_root: bool = False,
wrappers: list[XmlWrapper] | None = None
) -> str
Parameters:
model: The XMLPydantifyModel instance to serializepretty_print: Enable formatted XML output (default: False)data_root: Add NETCONF-compatible<data>root element (default: False)wrappers: Optional list of wrapper elements, specified outermost-first (default: None)
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
- with_enums_defaults: YANG-style
RootModel,Enum, and default value handling
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.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| pydantify_common-0.2.0.tar.gz | 53.0 kB | With provenance | Details for pydantify_common-0.2.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| pydantify_common-0.2.0-py3-none-any.whl | Python 3 | none | any | Details for pydantify_common-0.2.0-py3-none-any.whl |
Total release size: 59.2 kB
Release files / pydantify_common-0.2.0.tar.gz
| Download URL | pydantify_common-0.2.0.tar.gz |
|---|---|
| Size | 53.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
a9195e311e4a5af26f3dcc4d4b5cd347d58596c44adde4d217d4c2a5b7a1fa86
|
|
BLAKE2b-256 checksum How to use checksums |
65bc445dcaf01ded00fc930d578e7d58300fc8ea341d5c60063e8372a7b07120
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 19, 2026.
Transparency logRelease files / pydantify_common-0.2.0-py3-none-any.whl
| Download URL | pydantify_common-0.2.0-py3-none-any.whl |
|---|---|
| Size | 6.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
6ead44da545aa95946dbfcb4b18d5e4093f404709074244119147b4b054db33e
|
|
BLAKE2b-256 checksum How to use checksums |
bb034faa1556372735817b6c112a438324f5fba899decfc8790249c44fbd112e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 19, 2026.
Transparency logSecurity for pydantify-common 0.2.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for pydantify_common-0.2.0.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 19, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.