Skip to content

Instantly share code, notes, and snippets.

@tfiers
Created September 28, 2020 13:59
Show Gist options
  • Save tfiers/5535d91f3b965a0eec406613ebb0b08b to your computer and use it in GitHub Desktop.
Save tfiers/5535d91f3b965a0eec406613ebb0b08b to your computer and use it in GitHub Desktop.
`Quantity[pF]` MVP, via typish (via nptyping NDArrayMeta)
from dataclasses import dataclass, fields, Field
from typish import SubscriptableType
import yunit
class QuantityMeta(SubscriptableType):
@property
def typed_unit(cls):
return cls.__args__
def __hash__(cls):
return hash(cls.typed_unit)
class Quantity(metaclass=QuantityMeta):
...
pF = yunit.Unit('pF')
mV = yunit.Unit('mV')
QpF = Quantity[pF]
@dataclass
class Collection:
k: Quantity[pF]
def __post_init__(self):
for field in fields(self):
field: Field
value: yunit.Quantity = getattr(self, field.name)
specified_unit = field.type.typed_unit
actual_unit = value.display_unit
assert specified_unit == actual_unit
Collection(k = 8 * mV)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment