Skip to content

Commit

Permalink
Improve documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
jayqi committed Mar 6, 2024
1 parent 3793c20 commit 7d7ce80
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
[![tests](https://github.com/jayqi/sortedcontainers-pydantic/actions/workflows/tests.yml/badge.svg?branch=main)](https://github.com/jayqi/sortedcontainers-pydantic/actions/workflows/tests.yml?query=branch%3Amain)
[![codecov](https://codecov.io/gh/jayqi/sortedcontainers-pydantic/branch/main/graph/badge.svg)](https://codecov.io/gh/jayqi/sortedcontainers-pydantic)

This package adds [Pydantic](https://docs.pydantic.dev/latest/) support to [sortedcontainers](https://github.com/grantjenks/python-sortedcontainers/), a fast, pure-Python sorted collections library.
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 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 from `sortedcontainers_pydantic` instead of `sortedcontainers`.
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`.

```python
from pydantic import BaseModel, TypeAdapter
Expand All @@ -19,6 +19,12 @@ class MyModel(BaseModel):
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])

Expand All @@ -28,6 +34,12 @@ TypeAdapter(SortedList).validate_json("[3, 1, 2]")

<sup>Reproducible example created by [reprexlite](https://github.com/jayqi/reprexlite) v0.5.0</sup>

Currently, only Pydantic V2 is supported.

## Installation

...
sortedcontainers-pydantic is not yet available on PyPI. For now, install from GitHub:

```bash
pip install sortedcontainers-pydantic@git+https://github.com/jayqi/sortedcontainers-pydantic.git
```

0 comments on commit 7d7ce80

Please sign in to comment.