pydantic-pint


Namepydantic-pint JSON
Version 0.1 PyPI version JSON
download
home_pageNone
SummaryPydantic validation for Pint Quantities.
upload_time2024-05-06 23:34:15
maintainerNone
docs_urlNone
authorNone
requires_python>=3.8
licenseMIT License Copyright (c) 2024 Tyler Hughes Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
keywords pydantic-pint pydantic pint
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Pydantic Pint

[![_](https://img.shields.io/pypi/v/pydantic-pint)](https://pypi.python.org/pypi/pydantic-pint)
[![_](https://img.shields.io/pypi/pyversions/pydantic-pint)](https://github.com/tylerh111/pydantic-pint)
[![_](https://img.shields.io/pypi/l/pydantic-pint)](https://github.com/tylerh111/pydantic-pint/blob/main/LICENSE.md)
[![_](https://img.shields.io/readthedocs/pydantic-pint)](https://pydantic-pint.readthedocs.io)

---

[Pydantic](https://docs.pydantic.dev) is a Python library for data validation and data serialization.
[Pint](https://pint.readthedocs.io) is a Python library for defining, operating, and manipulating physical quantities.
By default, they do not play well with each other.

Many projects that have a need for data validation may also need to work with physical quantities.
[Pydantic Pint](https://pydantic-pint.readthedocs.io) aims to bridge that gap by providing Pydantic validation for Pint quantities.

```python
from pydantic import BaseModel
from pydantic_pint import PydanticPintQuantity
from pint import Quantity
from typing import Annotations

class Box(BaseModel):
    length: Annotated[Quantity, PydanticPintQuantity("m")]
    width: Annotated[Quantity, PydanticPintQuantity("m")]

box = Box(
    length="4m",
    width="2m",
)
```

## Getting Started

### Installation

Pydantic Pint is available as [`pydantic-pint`](https://pypi.python.org/pypi/pydantic-pint) on PyPI.

Pydantic Pint requires both Pydantic and Pint to be installed.
It also requires [`typing.Annotated`](https://docs.python.org/3/library/typing.html#typing.Annotated) (for older version of python use [`typing-extensions`](https://pypi.org/project/typing-extensions/)).

```shell
pip install pydantic-pint
```

### Usage

Pydantic Pint provides `PydanticPintQuantity` which enabled Pydantic validation for Pint quantities.
For a field of a Pydantic model to have quantity validation, it must be annotated with a `PydanticPintQuantity` for a given unit.

```python
from pydantic import BaseModel
from pydantic_pint import PydanticPintQuantity
from pint import Quantity
from typing import Annotated

class Coordinates(BaseModel):
    latitude: Annotated[Quantity, PydanticPintQuantity("deg")]
    longitude: Annotated[Quantity, PydanticPintQuantity("deg")]
    altitude: Annotated[Quantity, PydanticPintQuantity("km")]
```

Users of the model can input anything to the field with a specified unit that is convertible to the units declared in the annotation.
For instance, the units for `Coordinates.altitude` are kilometers, however users can specify meters instead.
`PydanticPintQuantity` will handle the conversion from meters to kilometers.

```python
coord = Coordinates(
    latitude="39.905705 deg",
    longitude="-75.166519 deg",
    altitude="12 meters",
)

print(coord)
#> latitude=<Quantity(39.905705, 'degree')> longitude=<Quantity(-75.166519, 'degree')> altitude=<Quantity(0.012, 'kilometer')>
print(f"{coord!r}")
#> Coordinates(latitude=<Quantity(39.905705, 'degree')>, longitude=<Quantity(-75.166519, 'degree')>, altitude=<Quantity(0.012, 'kilometer')>)
print(coord.model_dump())
#> {'latitude': <Quantity(39.905705, 'degree')>, 'longitude': <Quantity(-75.166519, 'degree')>, 'altitude': <Quantity(0.012, 'kilometer')>}
print(coord.model_dump(mode="json"))
#> {'latitude': '39.905705 degree', 'longitude': '-75.166519 degree', 'altitude': '0.012 kilometer'}
print(f"{coord.model_dump_json()!r}")
#> '{"latitude":"39.905705 degree","longitude":"-75.166519 degree","altitude":"0.012 kilometer"}'
```

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "pydantic-pint",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": null,
    "keywords": "pydantic-pint, pydantic, pint",
    "author": null,
    "author_email": "Tyler Hughes <tylerxh111+git@proton.me>",
    "download_url": "https://files.pythonhosted.org/packages/5f/bc/c82c85f1be1fa58743281205511323b39b65ff8c1f5a4ead60a31c358136/pydantic_pint-0.1.tar.gz",
    "platform": null,
    "description": "# Pydantic Pint\n\n[![_](https://img.shields.io/pypi/v/pydantic-pint)](https://pypi.python.org/pypi/pydantic-pint)\n[![_](https://img.shields.io/pypi/pyversions/pydantic-pint)](https://github.com/tylerh111/pydantic-pint)\n[![_](https://img.shields.io/pypi/l/pydantic-pint)](https://github.com/tylerh111/pydantic-pint/blob/main/LICENSE.md)\n[![_](https://img.shields.io/readthedocs/pydantic-pint)](https://pydantic-pint.readthedocs.io)\n\n---\n\n[Pydantic](https://docs.pydantic.dev) is a Python library for data validation and data serialization.\n[Pint](https://pint.readthedocs.io) is a Python library for defining, operating, and manipulating physical quantities.\nBy default, they do not play well with each other.\n\nMany projects that have a need for data validation may also need to work with physical quantities.\n[Pydantic Pint](https://pydantic-pint.readthedocs.io) aims to bridge that gap by providing Pydantic validation for Pint quantities.\n\n```python\nfrom pydantic import BaseModel\nfrom pydantic_pint import PydanticPintQuantity\nfrom pint import Quantity\nfrom typing import Annotations\n\nclass Box(BaseModel):\n    length: Annotated[Quantity, PydanticPintQuantity(\"m\")]\n    width: Annotated[Quantity, PydanticPintQuantity(\"m\")]\n\nbox = Box(\n    length=\"4m\",\n    width=\"2m\",\n)\n```\n\n## Getting Started\n\n### Installation\n\nPydantic Pint is available as [`pydantic-pint`](https://pypi.python.org/pypi/pydantic-pint) on PyPI.\n\nPydantic Pint requires both Pydantic and Pint to be installed.\nIt also requires [`typing.Annotated`](https://docs.python.org/3/library/typing.html#typing.Annotated) (for older version of python use [`typing-extensions`](https://pypi.org/project/typing-extensions/)).\n\n```shell\npip install pydantic-pint\n```\n\n### Usage\n\nPydantic Pint provides `PydanticPintQuantity` which enabled Pydantic validation for Pint quantities.\nFor a field of a Pydantic model to have quantity validation, it must be annotated with a `PydanticPintQuantity` for a given unit.\n\n```python\nfrom pydantic import BaseModel\nfrom pydantic_pint import PydanticPintQuantity\nfrom pint import Quantity\nfrom typing import Annotated\n\nclass Coordinates(BaseModel):\n    latitude: Annotated[Quantity, PydanticPintQuantity(\"deg\")]\n    longitude: Annotated[Quantity, PydanticPintQuantity(\"deg\")]\n    altitude: Annotated[Quantity, PydanticPintQuantity(\"km\")]\n```\n\nUsers of the model can input anything to the field with a specified unit that is convertible to the units declared in the annotation.\nFor instance, the units for `Coordinates.altitude` are kilometers, however users can specify meters instead.\n`PydanticPintQuantity` will handle the conversion from meters to kilometers.\n\n```python\ncoord = Coordinates(\n    latitude=\"39.905705 deg\",\n    longitude=\"-75.166519 deg\",\n    altitude=\"12 meters\",\n)\n\nprint(coord)\n#> latitude=<Quantity(39.905705, 'degree')> longitude=<Quantity(-75.166519, 'degree')> altitude=<Quantity(0.012, 'kilometer')>\nprint(f\"{coord!r}\")\n#> Coordinates(latitude=<Quantity(39.905705, 'degree')>, longitude=<Quantity(-75.166519, 'degree')>, altitude=<Quantity(0.012, 'kilometer')>)\nprint(coord.model_dump())\n#> {'latitude': <Quantity(39.905705, 'degree')>, 'longitude': <Quantity(-75.166519, 'degree')>, 'altitude': <Quantity(0.012, 'kilometer')>}\nprint(coord.model_dump(mode=\"json\"))\n#> {'latitude': '39.905705 degree', 'longitude': '-75.166519 degree', 'altitude': '0.012 kilometer'}\nprint(f\"{coord.model_dump_json()!r}\")\n#> '{\"latitude\":\"39.905705 degree\",\"longitude\":\"-75.166519 degree\",\"altitude\":\"0.012 kilometer\"}'\n```\n",
    "bugtrack_url": null,
    "license": "MIT License  Copyright (c) 2024 Tyler Hughes  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.  THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ",
    "summary": "Pydantic validation for Pint Quantities.",
    "version": "0.1",
    "project_urls": {
        "changes": "https://github.com/tylerh111/pydantic-pint/blob/main/CHANGES.md",
        "documentation": "https://pydantic-pint.readthedocs.io",
        "issues": "https://github.com/tylerh111/pydantic-pint/issues",
        "source": "https://github.com/tylerh111/pydantic-pint"
    },
    "split_keywords": [
        "pydantic-pint",
        " pydantic",
        " pint"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "00880ca0ce9d10a8cf2983d24483572cdd4e7ab3ec7d0a2d0f0ae9e3d4a28a74",
                "md5": "b2f8e817f0d8ab26d4525ead0e748b65",
                "sha256": "4d04125df91f080facea67c5bede068ba5f7f8453bf69c3c3ae18ca901a84e76"
            },
            "downloads": -1,
            "filename": "pydantic_pint-0.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "b2f8e817f0d8ab26d4525ead0e748b65",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 7303,
            "upload_time": "2024-05-06T23:34:13",
            "upload_time_iso_8601": "2024-05-06T23:34:13.551001Z",
            "url": "https://files.pythonhosted.org/packages/00/88/0ca0ce9d10a8cf2983d24483572cdd4e7ab3ec7d0a2d0f0ae9e3d4a28a74/pydantic_pint-0.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "5fbcc82c85f1be1fa58743281205511323b39b65ff8c1f5a4ead60a31c358136",
                "md5": "c06582001e7bb86735da33ad8880028c",
                "sha256": "488a256e37724970dbeb736d38a45bc3a9da4112a59edb094d9f87428a2b5467"
            },
            "downloads": -1,
            "filename": "pydantic_pint-0.1.tar.gz",
            "has_sig": false,
            "md5_digest": "c06582001e7bb86735da33ad8880028c",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 19741,
            "upload_time": "2024-05-06T23:34:15",
            "upload_time_iso_8601": "2024-05-06T23:34:15.514646Z",
            "url": "https://files.pythonhosted.org/packages/5f/bc/c82c85f1be1fa58743281205511323b39b65ff8c1f5a4ead60a31c358136/pydantic_pint-0.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-05-06 23:34:15",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "tylerh111",
    "github_project": "pydantic-pint",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "pydantic-pint"
}
        
Elapsed time: 0.23759s