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
File details
Details for the file sortedcontainers_pydantic-1.0.0.tar.gz
.
File metadata
- Download URL: sortedcontainers_pydantic-1.0.0.tar.gz
- Upload date:
- Size: 8.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.12.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | eb0e4aeb5197d690165f0a7b1a55c490eabe4b21375c3a91726373b2551e25ee |
|
MD5 | 46d58a0027bcbac1ed9c1a2459edbd08 |
|
BLAKE2b-256 | 2539b8dbaf922f4f0fd1d7d033f12fe22b5c8b14a41eac6caab671e9cb59144b |
File details
Details for the file sortedcontainers_pydantic-1.0.0-py3-none-any.whl
.
File metadata
- Download URL: sortedcontainers_pydantic-1.0.0-py3-none-any.whl
- Upload date:
- Size: 4.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.12.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 07e92e9b85dbf9248e0a5b59e0311a095e6fb27e0dc461b1209ead5a550b60b2 |
|
MD5 | 6a8bccbd1504e030b7bdf193be18ab29 |
|
BLAKE2b-256 | 6df44d01fd09a88a6d9707b4609568415cb21585e83f42282dd848c9bc0af95c |