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.
Project description
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
pydanticmodels usingnumpyscalar types likefloat32,uint32,int8, etc. - Strict Type Enforcement: Ensures model fields adhere to specific precision and range constraints.
- Simple Integration: Easily import and use
numpytypes in yourpydanticmodels.
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.,
UInt8ranges from 0 to 255). - Memory Efficiency: Helps reduce memory consumption by enforcing smaller data types.
License
MIT License
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file numpy_pydantic_types-0.1.0a0.tar.gz.
File metadata
- Download URL: numpy_pydantic_types-0.1.0a0.tar.gz
- Upload date:
- Size: 4.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e9ad6f1920224d14f1aa074b73fdc3f00c8cd7811a1d0594296494be1bda8095
|
|
| MD5 |
af3fdc3f61be71ef79324cf78efc34a3
|
|
| BLAKE2b-256 |
8de5d474eb95a66960e96b956650a4041c160683549ed3fa25fbb23e6eff80b9
|
Provenance
The following attestation bundles were made for numpy_pydantic_types-0.1.0a0.tar.gz:
Publisher:
ci.yml on eksavazquez/numpy-pydantic-types
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
numpy_pydantic_types-0.1.0a0.tar.gz -
Subject digest:
e9ad6f1920224d14f1aa074b73fdc3f00c8cd7811a1d0594296494be1bda8095 - Sigstore transparency entry: 149347290
- Sigstore integration time:
-
Permalink:
eksavazquez/numpy-pydantic-types@9ca4a21a0625cf40f51ce9a728020f5594938af6 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/eksavazquez
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci.yml@9ca4a21a0625cf40f51ce9a728020f5594938af6 -
Trigger Event:
push
-
Statement type:
File details
Details for the file numpy_pydantic_types-0.1.0a0-py3-none-any.whl.
File metadata
- Download URL: numpy_pydantic_types-0.1.0a0-py3-none-any.whl
- Upload date:
- Size: 6.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
042e1d78fd7aeabe03077938d24fb9e63a12a8be8fc528aef674d8597c90cb85
|
|
| MD5 |
a4a53c5980d5e5b09d51ab84e4dda21f
|
|
| BLAKE2b-256 |
e298d6548ac6510254c7680a361d7834f4c0b755b129d4d3ded241953786f10d
|
Provenance
The following attestation bundles were made for numpy_pydantic_types-0.1.0a0-py3-none-any.whl:
Publisher:
ci.yml on eksavazquez/numpy-pydantic-types
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
numpy_pydantic_types-0.1.0a0-py3-none-any.whl -
Subject digest:
042e1d78fd7aeabe03077938d24fb9e63a12a8be8fc528aef674d8597c90cb85 - Sigstore transparency entry: 149347291
- Sigstore integration time:
-
Permalink:
eksavazquez/numpy-pydantic-types@9ca4a21a0625cf40f51ce9a728020f5594938af6 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/eksavazquez
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci.yml@9ca4a21a0625cf40f51ce9a728020f5594938af6 -
Trigger Event:
push
-
Statement type: