# numpy-pydantic-types
**numpy-pydantic-types** is a Python library providing `numpy` scalar data types compatible with `pydantic`, allowing for easy validation and enforcement of numerical precision in `pydantic` models.
## Overview
`pydantic` models rely on Python's native types (e.g., `int`, `float`, `str`) for data validation, which can be limiting when working with scientific or numerical applications that require specific `numpy` types, like `float32`, `uint16`, or `int8`. This library bridges that gap by introducing numpy scalar types as valid pydantic field types, allowing you to define precise, controlled models that integrate with scientific workflows.
## Features
- **Numpy Scalar Compatibility**: Define `pydantic` models using `numpy` scalar types like `float32`, `uint32`, `int8`, etc.
- **Strict Type Enforcement**: Ensures model fields adhere to specific precision and range constraints.
- **Simple Integration**: Easily import and use `numpy` types in your `pydantic` models.
## Installation
```
pip install numpy_pydantic_types
```
# Usage
This library enables the use of `numpy` scalar types directly within `pydantic` models. Here’s how to set up a model using `numpy` types:
```
from pydantic import BaseModel, ValidationError
from numpy_pydantic_types import Float32, UInt32, UInt16
class ScientificModel(BaseModel):
precision_value: Float32 # Enforces a 32-bit floating point
sample_count: UInt32 # Enforces a 32-bit unsigned integer
sensor_id: UInt16 # Enforces a 16-bit unsigned integer
# Example data
data = {
"precision_value": 1.234567,
"sample_count": 4294967295, # Max value for UInt32
"sensor_id": 65535 # Max value for UInt16
}
# Instantiate the model with the specific numpy types
try:
model = ScientificModel(**data)
print("Model validated successfully:", model)
except ValidationError as e:
print("Validation error:", e)
```
## Supported Numpy Types
The library provides a range of `numpy` scalar types that can be used in `pydantic` models, including:
- **Float Types**: `Float32`, `Float64`
- **Signed Integer Types**: `Int8`, `Int16`, `Int32`, `Int64`
- **Unsigned Integer Types**: `UInt8`, `UInt16`, `UInt32`, `UInt64`
## Example Model
Using specific `numpy` scalar types can help enforce type constraints in applications where precision or memory footprint is crucial, such as scientific computations, data analysis, or embedded systems.
```
from numpy_pydantic_types import Int8, Float64
class DataProcessingModel(BaseModel):
temperature: Float64
adjustment_factor: Int8
```
## Why Use Numpy Scalar Types?
- **Precision**: Control over numerical precision, essential in scientific or numerical applications.
- **Range Enforcement**: Ensures values conform to specific data type ranges (e.g., `UInt8` ranges from 0 to 255).
- **Memory Efficiency**: Helps reduce memory consumption by enforcing smaller data types.
## License
MIT License
Raw data
{
"_id": null,
"home_page": "https://github.com/eksavazquez/numpy-pydantic-types",
"name": "numpy-pydantic-types",
"maintainer": "\u00c1lvaro V\u00e1zquez Ortiz",
"docs_url": null,
"requires_python": "<3.13,>=3.9",
"maintainer_email": "alvvazort@gmail.com",
"keywords": "pydantic, numpy, typing, validation",
"author": "\u00c1lvaro V\u00e1zquez Ortiz",
"author_email": "alvvazort@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/8d/e5/d474eb95a66960e96b956650a4041c160683549ed3fa25fbb23e6eff80b9/numpy_pydantic_types-0.1.0a0.tar.gz",
"platform": null,
"description": "# numpy-pydantic-types\n**numpy-pydantic-types** is a Python library providing `numpy` scalar data types compatible with `pydantic`, allowing for easy validation and enforcement of numerical precision in `pydantic` models.\n\n## Overview\n`pydantic` models rely on Python's native types (e.g., `int`, `float`, `str`) for data validation, which can be limiting when working with scientific or numerical applications that require specific `numpy` types, like `float32`, `uint16`, or `int8`. This library bridges that gap by introducing numpy scalar types as valid pydantic field types, allowing you to define precise, controlled models that integrate with scientific workflows.\n\n## Features\n- **Numpy Scalar Compatibility**: Define `pydantic` models using `numpy` scalar types like `float32`, `uint32`, `int8`, etc.\n- **Strict Type Enforcement**: Ensures model fields adhere to specific precision and range constraints.\n- **Simple Integration**: Easily import and use `numpy` types in your `pydantic` models.\n## Installation\n```\npip install numpy_pydantic_types\n```\n\n# Usage\nThis library enables the use of `numpy` scalar types directly within `pydantic` models. Here\u2019s how to set up a model using `numpy` types:\n\n```\nfrom pydantic import BaseModel, ValidationError\nfrom numpy_pydantic_types import Float32, UInt32, UInt16\n\nclass ScientificModel(BaseModel):\n precision_value: Float32 # Enforces a 32-bit floating point\n sample_count: UInt32 # Enforces a 32-bit unsigned integer\n sensor_id: UInt16 # Enforces a 16-bit unsigned integer\n\n# Example data\ndata = {\n \"precision_value\": 1.234567,\n \"sample_count\": 4294967295, # Max value for UInt32\n \"sensor_id\": 65535 # Max value for UInt16\n}\n\n# Instantiate the model with the specific numpy types\ntry:\n model = ScientificModel(**data)\n print(\"Model validated successfully:\", model)\nexcept ValidationError as e:\n print(\"Validation error:\", e)\n```\n\n## Supported Numpy Types\nThe library provides a range of `numpy` scalar types that can be used in `pydantic` models, including:\n\n- **Float Types**: `Float32`, `Float64`\n- **Signed Integer Types**: `Int8`, `Int16`, `Int32`, `Int64`\n- **Unsigned Integer Types**: `UInt8`, `UInt16`, `UInt32`, `UInt64`\n## Example Model\nUsing specific `numpy` scalar types can help enforce type constraints in applications where precision or memory footprint is crucial, such as scientific computations, data analysis, or embedded systems.\n\n```\nfrom numpy_pydantic_types import Int8, Float64\n\nclass DataProcessingModel(BaseModel):\n temperature: Float64\n adjustment_factor: Int8\n```\n\n## Why Use Numpy Scalar Types?\n- **Precision**: Control over numerical precision, essential in scientific or numerical applications.\n- **Range Enforcement**: Ensures values conform to specific data type ranges (e.g., `UInt8` ranges from 0 to 255).\n- **Memory Efficiency**: Helps reduce memory consumption by enforcing smaller data types.\n\n## License\nMIT License\n\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "numpy-pydantic-types is a Python library providing numpy scalar data types compatible with pydantic, allowing for easy validation and enforcement of numerical precision in pydantic models.",
"version": "0.1.0a0",
"project_urls": {
"Homepage": "https://github.com/eksavazquez/numpy-pydantic-types"
},
"split_keywords": [
"pydantic",
" numpy",
" typing",
" validation"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "e298d6548ac6510254c7680a361d7834f4c0b755b129d4d3ded241953786f10d",
"md5": "a4a53c5980d5e5b09d51ab84e4dda21f",
"sha256": "042e1d78fd7aeabe03077938d24fb9e63a12a8be8fc528aef674d8597c90cb85"
},
"downloads": -1,
"filename": "numpy_pydantic_types-0.1.0a0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "a4a53c5980d5e5b09d51ab84e4dda21f",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": "<3.13,>=3.9",
"size": 6182,
"upload_time": "2024-11-17T12:18:42",
"upload_time_iso_8601": "2024-11-17T12:18:42.418066Z",
"url": "https://files.pythonhosted.org/packages/e2/98/d6548ac6510254c7680a361d7834f4c0b755b129d4d3ded241953786f10d/numpy_pydantic_types-0.1.0a0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "8de5d474eb95a66960e96b956650a4041c160683549ed3fa25fbb23e6eff80b9",
"md5": "af3fdc3f61be71ef79324cf78efc34a3",
"sha256": "e9ad6f1920224d14f1aa074b73fdc3f00c8cd7811a1d0594296494be1bda8095"
},
"downloads": -1,
"filename": "numpy_pydantic_types-0.1.0a0.tar.gz",
"has_sig": false,
"md5_digest": "af3fdc3f61be71ef79324cf78efc34a3",
"packagetype": "sdist",
"python_version": "source",
"requires_python": "<3.13,>=3.9",
"size": 4655,
"upload_time": "2024-11-17T12:18:44",
"upload_time_iso_8601": "2024-11-17T12:18:44.242995Z",
"url": "https://files.pythonhosted.org/packages/8d/e5/d474eb95a66960e96b956650a4041c160683549ed3fa25fbb23e6eff80b9/numpy_pydantic_types-0.1.0a0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-11-17 12:18:44",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "eksavazquez",
"github_project": "numpy-pydantic-types",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "numpy-pydantic-types"
}