https://github.com/drivendataorg/sortedcontainers-pydantic
Adds Pydantic support to sortedcontainers.
https://github.com/drivendataorg/sortedcontainers-pydantic
pydantic python sorted-lists sorted-map sorted-sets sortedcontainers
Last synced: about 1 month ago
JSON representation
Adds Pydantic support to sortedcontainers.
- Host: GitHub
- URL: https://github.com/drivendataorg/sortedcontainers-pydantic
- Owner: drivendataorg
- License: mit
- Created: 2024-03-05T17:51:49.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2025-04-08T05:05:05.000Z (about 1 month ago)
- Last Synced: 2025-04-08T06:22:06.373Z (about 1 month ago)
- Topics: pydantic, python, sorted-lists, sorted-map, sorted-sets, sortedcontainers
- Language: Python
- Homepage:
- Size: 80.1 KB
- Stars: 5
- Watchers: 6
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# sortedcontainers-pydantic
[](https://pypi.org/project/sortedcontainers-pydantic/)
[](https://pypi.org/project/sortedcontainers-pydantic/)
[](https://github.com/drivendataorg/sortedcontainers-pydantic/actions/workflows/tests.yml?query=branch%3Amain)
[](https://codecov.io/gh/drivendataorg/sortedcontainers-pydantic)This package adds [Pydantic](https://docs.pydantic.dev/latest/) support to [sortedcontainers](https://github.com/grantjenks/python-sortedcontainers/), a fast pure-Python library for sorted mutable collections.
It implements [Pydantic's special methods](https://docs.pydantic.dev/latest/concepts/types/#customizing-validation-with-__get_pydantic_core_schema__) 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.
```python
from pydantic import BaseModel, TypeAdapter
from sortedcontainers_pydantic import SortedListclass 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](https://github.com/jayqi/reprexlite) v0.5.0
## Installation
sortedcontainers-pydantic is available on [PyPI](https://pypi.org/project/sortedcontainers-pydantic/). You can install it with
```bash
pip install sortedcontainers-pydantic
```