pydantic-numpy


Namepydantic-numpy JSON
Version 4.0.0 PyPI version JSON
download
home_pagehttps://github.com/caniko/pydantic-numpy
SummaryPydantic Model integration of the NumPy array
upload_time2023-09-14 07:35:36
maintainerCan H. Tartanoglu
docs_urlNone
authorCan H. Tartanoglu
requires_python>=3.9,<3.12
licenseBSD-4
keywords pydantic numpy typing validation
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # pydantic-numpy

Package that integrates NumPy Arrays into Pydantic!

- `NumpyModel` make it possible to dump and load `np.ndarray` within model fields alongside other fields that are not instances of `np.ndarray`!
- `pydantic_numpy.typing` provides many typings such as `NpNDArrayFp64`, `Np3DArrayFp64` (float64 that must be 3D)!

## Usage

For more examples see [test_ndarray.py](./tests/test_typing.py)

```python
import numpy as np

import pydantic_numpy.typing as pnd
from pydantic_numpy import np_array_pydantic_annotated_typing
from pydantic_numpy.model import NumpyModel, MultiArrayNumpyFile


class MyNumpyModel(NumpyModel):
    any_array_dtype_and_dimension: pnd.NpNDArray

    # Must be numpy float32 as dtype
    k: np_array_pydantic_annotated_typing(data_type=np.float32)
    shorthand_for_k: pnd.NpNDArrayFp32

    must_be_1d_np_array: np_array_pydantic_annotated_typing(dimensions=1)


class MyDemoModel(NumpyModel):
    k: np_array_pydantic_annotated_typing(data_type=np.float32)


# Instantiate from array
cfg = MyDemoModel(k=[1, 2])
# Instantiate from numpy file
cfg = MyDemoModel(k="path_to/array.npy")
# Instantiate from npz file with key
cfg = MyDemoModel(k=MultiArrayNumpyFile(path="path_to/array.npz", key="k"))

cfg.k   # np.ndarray[np.float32]

cfg.dump("path_to_dump_dir", "object_id")
cfg.load("path_to_dump_dir", "object_id")
```

`NumpyModel.load` requires the original model:
```python
MyNumpyModel.load(<path>)
```
Use `model_agnostic_load` when you have several models that may be the correct model:

```python
from pydantic_numpy.model import model_agnostic_load

cfg.dump("path_to_dump_dir", "object_id")
equals_cfg = model_agnostic_load("path_to_dump_dir", "object_id", models=[MyNumpyModel, MyDemoModel])
```

### Install
```shell
pip install pydantic-numpy
```

## Considerations
You can install from [cheind's](https://github.com/cheind/pydantic-numpy) repository if you want Python `3.8` support, but this version only supports Pydantic V1 and will not work with V2.

### Licensing notice
As of version `3.0.0` the license has moved over to BSD-4. The versions prior are under the MIT license.

### History
The original idea originates from [this discussion](https://gist.github.com/danielhfrank/00e6b8556eed73fb4053450e602d2434), and forked from [cheind's](https://github.com/cheind/pydantic-numpy) repository.

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/caniko/pydantic-numpy",
    "name": "pydantic-numpy",
    "maintainer": "Can H. Tartanoglu",
    "docs_url": null,
    "requires_python": ">=3.9,<3.12",
    "maintainer_email": "python@rotas.mozmail.com",
    "keywords": "pydantic,numpy,typing,validation",
    "author": "Can H. Tartanoglu",
    "author_email": "",
    "download_url": "https://files.pythonhosted.org/packages/98/db/db22cad9f88ebb90bb3fde87caa1dd28f0f267a10b7ebeba62b4c7c71ec9/pydantic_numpy-4.0.0.tar.gz",
    "platform": null,
    "description": "# pydantic-numpy\n\nPackage that integrates NumPy Arrays into Pydantic!\n\n- `NumpyModel` make it possible to dump and load `np.ndarray` within model fields alongside other fields that are not instances of `np.ndarray`!\n- `pydantic_numpy.typing` provides many typings such as `NpNDArrayFp64`, `Np3DArrayFp64` (float64 that must be 3D)!\n\n## Usage\n\nFor more examples see [test_ndarray.py](./tests/test_typing.py)\n\n```python\nimport numpy as np\n\nimport pydantic_numpy.typing as pnd\nfrom pydantic_numpy import np_array_pydantic_annotated_typing\nfrom pydantic_numpy.model import NumpyModel, MultiArrayNumpyFile\n\n\nclass MyNumpyModel(NumpyModel):\n    any_array_dtype_and_dimension: pnd.NpNDArray\n\n    # Must be numpy float32 as dtype\n    k: np_array_pydantic_annotated_typing(data_type=np.float32)\n    shorthand_for_k: pnd.NpNDArrayFp32\n\n    must_be_1d_np_array: np_array_pydantic_annotated_typing(dimensions=1)\n\n\nclass MyDemoModel(NumpyModel):\n    k: np_array_pydantic_annotated_typing(data_type=np.float32)\n\n\n# Instantiate from array\ncfg = MyDemoModel(k=[1, 2])\n# Instantiate from numpy file\ncfg = MyDemoModel(k=\"path_to/array.npy\")\n# Instantiate from npz file with key\ncfg = MyDemoModel(k=MultiArrayNumpyFile(path=\"path_to/array.npz\", key=\"k\"))\n\ncfg.k   # np.ndarray[np.float32]\n\ncfg.dump(\"path_to_dump_dir\", \"object_id\")\ncfg.load(\"path_to_dump_dir\", \"object_id\")\n```\n\n`NumpyModel.load` requires the original model:\n```python\nMyNumpyModel.load(<path>)\n```\nUse `model_agnostic_load` when you have several models that may be the correct model:\n\n```python\nfrom pydantic_numpy.model import model_agnostic_load\n\ncfg.dump(\"path_to_dump_dir\", \"object_id\")\nequals_cfg = model_agnostic_load(\"path_to_dump_dir\", \"object_id\", models=[MyNumpyModel, MyDemoModel])\n```\n\n### Install\n```shell\npip install pydantic-numpy\n```\n\n## Considerations\nYou can install from [cheind's](https://github.com/cheind/pydantic-numpy) repository if you want Python `3.8` support, but this version only supports Pydantic V1 and will not work with V2.\n\n### Licensing notice\nAs of version `3.0.0` the license has moved over to BSD-4. The versions prior are under the MIT license.\n\n### History\nThe original idea originates from [this discussion](https://gist.github.com/danielhfrank/00e6b8556eed73fb4053450e602d2434), and forked from [cheind's](https://github.com/cheind/pydantic-numpy) repository.\n",
    "bugtrack_url": null,
    "license": "BSD-4",
    "summary": "Pydantic Model integration of the NumPy array",
    "version": "4.0.0",
    "project_urls": {
        "Homepage": "https://github.com/caniko/pydantic-numpy"
    },
    "split_keywords": [
        "pydantic",
        "numpy",
        "typing",
        "validation"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "37ff38ea6e64a2a566f80db4795b8208a24347a6eda48168becb63fa8e7f67ab",
                "md5": "9a8d7aabf94bde1efb4d008f2ab8d664",
                "sha256": "9f126346c7c4fe50d7e798e22f65febf452797b45a872fad119ec5fd70e87ddc"
            },
            "downloads": -1,
            "filename": "pydantic_numpy-4.0.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "9a8d7aabf94bde1efb4d008f2ab8d664",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.9,<3.12",
            "size": 15833,
            "upload_time": "2023-09-14T07:35:34",
            "upload_time_iso_8601": "2023-09-14T07:35:34.632773Z",
            "url": "https://files.pythonhosted.org/packages/37/ff/38ea6e64a2a566f80db4795b8208a24347a6eda48168becb63fa8e7f67ab/pydantic_numpy-4.0.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "98dbdb22cad9f88ebb90bb3fde87caa1dd28f0f267a10b7ebeba62b4c7c71ec9",
                "md5": "3bc174a0de2b865f9e54c012e3c1ba46",
                "sha256": "bafb3eb00392aba2eed4abce5f6167c86cc834a9ae9d47cd79ba52c455f75f3b"
            },
            "downloads": -1,
            "filename": "pydantic_numpy-4.0.0.tar.gz",
            "has_sig": false,
            "md5_digest": "3bc174a0de2b865f9e54c012e3c1ba46",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.9,<3.12",
            "size": 11638,
            "upload_time": "2023-09-14T07:35:36",
            "upload_time_iso_8601": "2023-09-14T07:35:36.316866Z",
            "url": "https://files.pythonhosted.org/packages/98/db/db22cad9f88ebb90bb3fde87caa1dd28f0f267a10b7ebeba62b4c7c71ec9/pydantic_numpy-4.0.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-09-14 07:35:36",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "caniko",
    "github_project": "pydantic-numpy",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "pydantic-numpy"
}
        
Elapsed time: 0.24074s