Pydantic bindings for Pint
Project description
pintdantic
Pydantic bindings for Pint
Installation
pip install pintdantic
Quick Start
from pintdantic import QuantityModel, QuantityField
from pint import Quantity
class MyModel(QuantityModel):
length: QuantityField
width: QuantityField
# Create a model with Quantity objects
model = MyModel(
length=Quantity(10.0, "m"),
width=Quantity(5.0, "m")
)
# Serialize to dict (condensed format by default)
data = model.to_dict()
# {"length": [10.0, "meter"], "width": [5.0, "meter"]}
# Load from dict
loaded_model = MyModel.from_dict(data)
Serialization Formats
Pintdantic supports two serialization formats for Quantity values:
Condensed Format (Default)
By default, Quantity values are serialized to a compact array format:
model = MyModel(length=Quantity(100.0, "watt"))
data = model.to_dict()
# {"length": [100.0, "watt"]}
This format is more compact and produces smaller JSON payloads.
Verbose Format
You can use the verbose format (dict-based) by setting verbose=True:
# Serialize with verbose format
data = model.to_dict(verbose=True)
# {"length": {"magnitude": 100.0, "units": "watt"}}
# Save to file with verbose format
model.save("output.json", verbose=True)
# Using Pydantic's model_dump with context
data = model.model_dump(context={'verbose': True})
Backwards Compatibility
Pintdantic can load data in either format, providing full backwards compatibility:
# Both formats work for loading
condensed_data = {"length": [10.0, "meter"], "width": [5.0, "meter"]}
verbose_data = {
"length": {"magnitude": 10.0, "units": "meter"},
"width": {"magnitude": 5.0, "units": "meter"}
}
model1 = MyModel.from_dict(condensed_data) # Works!
model2 = MyModel.from_dict(verbose_data) # Also works!
Input Formats
Pintdantic accepts multiple input formats for Quantity fields:
# Quantity objects
MyModel(length=Quantity(10.0, "m"))
# Tuples (magnitude, units)
MyModel(length=(10.0, "m"))
# Lists [magnitude, units] - JSON-compatible
MyModel(length=[10.0, "m"])
# Dicts (verbose format)
MyModel(length={"magnitude": 10.0, "units": "m"})
# Bare numbers (if defaults are set)
class ModelWithDefaults(QuantityModel):
length: QuantityField = (10.0, "m")
ModelWithDefaults(length=5.0) # Uses default units "m"
Nested Models
The verbose flag works with nested models and propagates through the entire serialization tree:
class Inner(QuantityModel):
measurement: QuantityField
class Outer(QuantityModel):
name: str
inner: Inner
height: QuantityField
outer = Outer(
name="test",
inner=Inner(measurement=Quantity(5.0, "cm")),
height=Quantity(10.0, "m")
)
# Condensed (default)
data = outer.to_dict()
# {
# "name": "test",
# "inner": {"measurement": [5.0, "centimeter"]},
# "height": [10.0, "meter"]
# }
# Verbose
data = outer.to_dict(verbose=True)
# {
# "name": "test",
# "inner": {"measurement": {"magnitude": 5.0, "units": "centimeter"}},
# "height": {"magnitude": 10.0, "units": "meter"}
# }
Usage with Pydantic BaseModel
When using QuantityModel within a regular Pydantic BaseModel, you can control the serialization format using Pydantic's context:
from pydantic import BaseModel
class RegularModel(BaseModel):
id: int
quantities: MyQuantityModel
model = RegularModel(id=1, quantities=MyQuantityModel(...))
# Condensed format (default)
data = model.model_dump()
# Verbose format using context
data = model.model_dump(context={'verbose': True})
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
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 pintdantic-0.0.4.tar.gz.
File metadata
- Download URL: pintdantic-0.0.4.tar.gz
- Upload date:
- Size: 8.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.8.22
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
21e56c73a06c0727010aef486e2eb5261c389d9977592f9aee3842c5371a4525
|
|
| MD5 |
71a1b25820057e6d70bfc5a128d4fb33
|
|
| BLAKE2b-256 |
46efa658620002b73534ebdfc22169fb0011f744bfa89b737c54367165eca263
|
File details
Details for the file pintdantic-0.0.4-py3-none-any.whl.
File metadata
- Download URL: pintdantic-0.0.4-py3-none-any.whl
- Upload date:
- Size: 8.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.8.22
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4ea6b8bb52a61bdd6cdb0124ad411ebd40c6844794958ff6e740f16711ea58b5
|
|
| MD5 |
6db8ad6e99c3dddc64bd998e6eb26520
|
|
| BLAKE2b-256 |
af58e3a3e3fa4234b5779dd9ea17e71a218527fdd577a3d3b709ff3d40d85be9
|