feat: add Dimension and unit module

This commit is contained in:
Shunsuke Shibayama 2024-09-20 16:56:22 +09:00
parent 82848c10d6
commit 4651a383ae
16 changed files with 392 additions and 10 deletions

View file

@ -24,3 +24,37 @@ Record = tuple
class Never:
pass
from typing import Generic, TypeVar
Ty = TypeVar('Ty')
M = TypeVar('M', bound=int)
L = TypeVar("L", bound=int)
T = TypeVar("T", bound=int)
I = TypeVar("I", bound=int)
Θ = TypeVar("Θ", bound=int)
N = TypeVar("N", bound=int)
J = TypeVar("J", bound=int)
class Dimension(float, Generic[Ty, M, L, T, I, Θ, N, J]):
def value(self) -> float:
return float(self)
def __str__(self):
return f"Dimension({float(self)})"
def __add__(self, other):
return Dimension(float(self) + other)
def __sub__(self, other):
return Dimension(float(self) - other)
def __mul__(self, other):
return Dimension(float(self) * other)
def __rmul__(self, other):
return Dimension(other * float(self))
def __truediv__(self, other):
return Dimension(float(self) / other)
def __floordiv__(self, other):
return Dimension(float(self) // other)
def __rtruediv__(self, other):
return Dimension(other / float(self))
def __rfloordiv__(self, other):
return Dimension(other // float(self))
def type_check(self, t: type) -> bool:
return t.__name__ == "Dimension"

View file

@ -25,10 +25,12 @@ class UnionType:
class FakeGenericAlias:
__name__: str
__origin__: type
__args__: list # list[type]
def __init__(self, origin, *args):
self.__name__ = origin.__name__
self.__origin__ = origin
self.__args__ = args