Use Pydantic to build complex types from simple parameters.
Project description
The cyanotype process, which gave birth to the blueprint, uses iron salts and UV light to transform ordinary paper into deep Prussian blue prints.
Features
Cyantic lets you build complex types from simple blueprints, using Pydantic.
- Build complex objects using intermediate Pydantic models.
- Reference other values using
@value:x.y.z - Import objects using
@import:x.y.z - Define custom
@hookhandlers (see tests)
Installation
uv add cyantic
Usage
from cyantic import Blueprint, blueprint, CyanticModel, hook
from torch import Tensor
import torch
import yaml
# 1. Create and register some useful parameterisations
# (or soon install from PyPi, i.e. `rye add cyantic-torch`)
@blueprint(Tensor)
class NormalTensor(Blueprint[Tensor]):
mean: float
std: float
size: tuple[int, ...]
def build(self) -> Tensor:
return torch.normal(self.mean, self.std, size=self.size)
@blueprint(Tensor)
class UniformTensor(Blueprint[Tensor]):
low: float
high: float
size: tuple[int, ...]
def build(self) -> Tensor:
return torch.empty(self.size).uniform_(self.low, self.high)
# 2. Write pydantic models using `CyanticModel` base class
class MyModel(CyanticModel):
normal_tensor: Tensor
uniform_tensor: Tensor
# 3. (optional) define custom hooks
@hook("env")
def env_hook(name: str, _: ValidationContext) -> str:
"""Handle @env:VARIABLE_NAME references."""
try:
return os.environ[name]
except KeyError as e:
raise ValueError(f"Environment variable {name} not found") from e
# 3. Validate from YAML files that specify the parameterisation
some_yaml = """common:
size: [3, 5]
foo: @env:MY_VARIABLE
normal_tensor:
mean: 0.0
std: 0.1
size: @value:common.size
uniform_tensor:
low: -1.0
std: 1.0
size: @value:common.size
"""
# 4. Receive objects built from the parameterisations.
my_model = MyModel.model_validate(yaml.safe_load(some_yaml))
assert isinstance(my_model.normal_tensor, Tensor)
assert isinstance(my_model.uniform_tensor, Tensor)
Development
git clone https://github.com/flywhl/cyantic.gitcd cyanticuv syncjust test
Flywheel
Science needs humble software tools. Flywheel is an open source collective building simple tools to preserve scientific momentum, inspired by devtools and devops culture.
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 cyantic-0.2.0.tar.gz.
File metadata
- Download URL: cyantic-0.2.0.tar.gz
- Upload date:
- Size: 42.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.5.16
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cc1636aedd7ba0da02da2ebf6bd11e56184161cac01242438b24ce8cdafa9dea
|
|
| MD5 |
178ac3e66f92e860bc733187daa94670
|
|
| BLAKE2b-256 |
c9ca5a62741973dcaa71d4acb20aec55b84f47d81615c8ef070073b6048653ce
|
File details
Details for the file cyantic-0.2.0-py3-none-any.whl.
File metadata
- Download URL: cyantic-0.2.0-py3-none-any.whl
- Upload date:
- Size: 10.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.5.16
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
df0683faac999d3f7f9406e3dc84ddaae6d575eecefff525991dbc6dcde98442
|
|
| MD5 |
2934f64cd34f05c8f6e48f0c72880ae8
|
|
| BLAKE2b-256 |
e5d67a3c7567f7d530b456604748086b6691be03cde90fc287f5ec2d85877553
|