diff options
| author | Max Nanis | 2026-02-24 17:26:15 -0500 |
|---|---|---|
| committer | Max Nanis | 2026-02-24 17:26:15 -0500 |
| commit | 8c1940445503fd6678d0961600f2be81622793a2 (patch) | |
| tree | b9173562b8824b5eaa805e446d9d780e1f23fb2a /jb/models/currency.py | |
| parent | 25d8c3c214baf10f6520cc1351f78473150e5d7a (diff) | |
| download | amt-jb-8c1940445503fd6678d0961600f2be81622793a2.tar.gz amt-jb-8c1940445503fd6678d0961600f2be81622793a2.zip | |
Extensive use of type checking. Movement of pytest conf towards handling managers (for db agnostic unittest). Starting to organize pytests.
Diffstat (limited to 'jb/models/currency.py')
| -rw-r--r-- | jb/models/currency.py | 70 |
1 files changed, 0 insertions, 70 deletions
diff --git a/jb/models/currency.py b/jb/models/currency.py deleted file mode 100644 index 3094e2a..0000000 --- a/jb/models/currency.py +++ /dev/null @@ -1,70 +0,0 @@ -import warnings -from decimal import Decimal -from typing import Any - -from pydantic import GetCoreSchemaHandler, NonNegativeInt -from pydantic_core import CoreSchema, core_schema - - -class USDCent(int): - def __new__(cls, value, *args, **kwargs): - - if isinstance(value, float): - warnings.warn( - "USDCent init with a float. Rounding behavior may " "be unexpected" - ) - - if isinstance(value, Decimal): - warnings.warn( - "USDCent init with a Decimal. Rounding behavior may " "be unexpected" - ) - - if value < 0: - raise ValueError("USDCent not be less than zero") - - return super(cls, cls).__new__(cls, value) - - def __add__(self, other): - assert isinstance(other, USDCent) - res = super(USDCent, self).__add__(other) - return self.__class__(res) - - def __sub__(self, other): - assert isinstance(other, USDCent) - res = super(USDCent, self).__sub__(other) - return self.__class__(res) - - def __mul__(self, other): - assert isinstance(other, USDCent) - res = super(USDCent, self).__mul__(other) - return self.__class__(res) - - def __abs__(self): - res = super(USDCent, self).__abs__() - return self.__class__(res) - - def __truediv__(self, other): - raise ValueError("Division not allowed for USDCent") - - def __str__(self): - return "%d" % int(self) - - def __repr__(self): - return "USDCent(%d)" % int(self) - - @classmethod - def __get_pydantic_core_schema__( - cls, source_type: Any, handler: GetCoreSchemaHandler - ) -> CoreSchema: - """ - https://docs.pydantic.dev/latest/concepts/types/#customizing-validation-with-__get_pydantic_core_schema__ - """ - return core_schema.no_info_after_validator_function( - cls, handler(NonNegativeInt) - ) - - def to_usd(self) -> Decimal: - return Decimal(int(self) / 100).quantize(Decimal(".01")) - - def to_usd_str(self) -> str: - return "${:,.2f}".format(float(self.to_usd())) |
