Pydantic support for the sortedcontainers package.
Project description
sortedcontainers-pydantic
This package adds Pydantic support to sortedcontainers, a fast pure-Python library for sorted mutable collections.
It implements Pydantic's special methods on subclasses of sortedcontainer's SortedDict
, SortedList
, and SortedSet
classes so that you can use them with Pydantic's models, validation, and serialization. To use, simply import the respective class of the same name from sortedcontainers_pydantic
instead of from sortedcontainers
. Only Pydantic V2 is supported.
from pydantic import BaseModel, TypeAdapter
from sortedcontainers_pydantic import SortedList
class MyModel(BaseModel):
sorted_list: SortedList[int]
MyModel(sorted_list=[3, 1, 2])
#> MyModel(sorted_list=SortedList([1, 2, 3]))
MyModel.model_validate_json('{"sorted_list": [3, 1, 2]}')
#> MyModel(sorted_list=SortedList([1, 2, 3]))
MyModel(sorted_list=[3, 1, 2]).model_dump_json()
#> '{"sorted_list":[1,2,3]}'
TypeAdapter(SortedList).validate_python([3, 1, 2])
#> SortedList([1, 2, 3])
TypeAdapter(SortedList).validate_json("[3, 1, 2]")
#> SortedList([1, 2, 3])
Reproducible example created by reprexlite v0.5.0
Installation
sortedcontainers-pydantic is available on PyPI. You can install it with
pip install sortedcontainers-pydantic
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
Hashes for sortedcontainers_pydantic-1.0.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | eb0e4aeb5197d690165f0a7b1a55c490eabe4b21375c3a91726373b2551e25ee |
|
MD5 | 46d58a0027bcbac1ed9c1a2459edbd08 |
|
BLAKE2b-256 | 2539b8dbaf922f4f0fd1d7d033f12fe22b5c8b14a41eac6caab671e9cb59144b |
Hashes for sortedcontainers_pydantic-1.0.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 07e92e9b85dbf9248e0a5b59e0311a095e6fb27e0dc461b1209ead5a550b60b2 |
|
MD5 | 6a8bccbd1504e030b7bdf193be18ab29 |
|
BLAKE2b-256 | 6df44d01fd09a88a6d9707b4609568415cb21585e83f42282dd848c9bc0af95c |