pydantic-cereal
Advanced serialization for Pydantic models
Pydantic is the most widely used data validation library for Python. It uses type hints/type annotations to define data models and has quite a nice "feel" to it. Pydantic V2 was released in June 2023 and brings many changes and improvements, including a new Rust-based engine for serializing and validating data.
This package, pydantic-cereal, is a small extension package that enables users to serialize Pydantic
models with "arbitrary" (non-JSON-fiendly) types to "arbitrary" file-system-like locations.
It uses fsspec to support generic file systems.
Writing a custom writer (serializer) and reader (loader) with fsspec URIs is quite straightforward.
You can also use universal-pathlib's
UPath with pydantic-cereal.
📘 See the full documentation here. 📘
Usage Example
See the minimal pure-Python example to learn how to wrap your own type. Below is a preview of this example.
from fsspec import AbstractFileSystem
from pydantic import BaseModel, ConfigDict
from pydantic_cereal import Cereal
cereal = Cereal() # This is a global variable
# Create and "register" a custom type
class MyType(object):
"""My custom type, which isn't a Pydantic model."""
def __init__(self, value: str):
self.value = str(value)
def __repr__(self) -> str:
return f"MyType({self.value})"
def my_reader(fs: AbstractFileSystem, path: str) -> MyType:
"""Read a MyType from an fsspec URI."""
return MyType(value=fs.read_text(path)) # type: ignore
def my_writer(obj: MyType, fs: AbstractFileSystem, path: str) -> None:
"""Write a MyType object to an fsspec URI."""
fs.write_text(path, obj.value)
MyWrappedType = cereal.wrap_type(MyType, reader=my_reader, writer=my_writer)
# Use type within Pydantic model
class MyModel(BaseModel):
"""My custom Pydantic model."""
config = ConfigDict(arbitrary_types_allowed=True) # Pydantic configuration
fld: MyWrappedType
mdl = MyModel(fld=MyType("my_field"))
# We can save the whole model to an fsspec URI, such as this MemoryFileSystem
uri = "memory://my_model"
cereal.write_model(mdl, uri)
# And we can read it back later
obj = cereal.read_model(uri)
assert isinstance(obj, MyModel)
assert isinstance(obj.fld, MyType)
For wrapping 3rd-party libraries, see the Pandas dataframe example.
Metadata
Release files for pydantic-cereal 0.0.6
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 | |
|---|---|---|---|---|
| pydantic-cereal-0.0.6.tar.gz | 25.1 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| pydantic_cereal-0.0.6-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 38.7 kB
Release files / pydantic-cereal-0.0.6.tar.gz
| Download URL | pydantic-cereal-0.0.6.tar.gz |
|---|---|
| Size | 25.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
aac44edc80de142706f765eb550b241e7be16ced713cea2f2fc981d248a8fdca
|
|
BLAKE2b-256 checksum How to use checksums |
33843c67e76d3d77f2603a2598150e0ebabe2fb380c8552d4e727b333fcec3ce
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/4.0.2 CPython/3.11.8
|
Release files / pydantic_cereal-0.0.6-py3-none-any.whl
| Download URL | pydantic_cereal-0.0.6-py3-none-any.whl |
|---|---|
| Size | 13.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
334ecc669687a415e1bb02c943393def6401b32d001fb2c7313f2923c439874b
|
|
BLAKE2b-256 checksum How to use checksums |
33daec4811d5b21ff2b54f126265a2c6cdae65c6faed69b86fa7705e0556a95e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/4.0.2 CPython/3.11.8
|
Security for pydantic-cereal 0.0.6
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.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A 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.