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:
PydantifyModelandXMLPydantifyModelfor 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 URIprefix(str): XML namespace prefix
Methods:
model_dump_xml() -> Element: Returns an lxml Element representationfields_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 serializepretty_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
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 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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
28760cccd9d3b8f0026aa00e4ece8a102f52d91f3b71f58a331174ed54c20e96
|
|
| MD5 |
ea939659e3e3838eae7d815b4e5f7baa
|
|
| BLAKE2b-256 |
1f201106a3bb93fd9a6591b9bb24e018ad698f5a0d122ea0986b403b74566708
|
File details
Details for the file pydantify_common-0.1.0-py3-none-any.whl.
File metadata
- Download URL: pydantify_common-0.1.0-py3-none-any.whl
- Upload date:
- Size: 3.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fae25526f58fcc975d05c2f23e7f4fd8b9262c0a0c930a8a0cb2f9c4d10e6c2a
|
|
| MD5 |
f85320d95f73561e17cdac8d02ccdb35
|
|
| BLAKE2b-256 |
586490416fc8f24960b864394fd53937b2fbc01d8f1c8793e612d0b7702ede8b
|