-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
24 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
""" Custom types for Pydantic models. """ | ||
from typing import Optional, Union | ||
import numpy as np | ||
|
||
|
||
class RoundedFloat(float): | ||
"""Rounds floats to a specified number of decimal places, defaulting to 3.""" | ||
|
||
def __new__(cls, value: float, n_decimals: Optional[int] = 3) -> "RoundedFloat": | ||
if n_decimals is not None: | ||
cls.n_decimals = n_decimals | ||
return super().__new__(cls, value) | ||
|
||
@classmethod | ||
def __get_validators__(cls): # type: ignore | ||
"""Pydantic validator for RoundedFloat""" | ||
yield cls.validate | ||
|
||
@classmethod | ||
def validate(cls, v: Union[float, np.float32, np.float64]) -> "RoundedFloat": | ||
"""Pydantic validator for RoundedFloat""" | ||
if isinstance(v, (np.float32, np.float64)): | ||
v = float(v) # Convert numpy float to standard Python float | ||
return cls(round(v, cls.n_decimals)) |