Path: blob/master/ invest-robot-contest_investRobot-master/robotlib/money.py
5929 views
from __future__ import annotations12import math34from dataclasses import dataclass5from tinkoff.invest import MoneyValue, Quotation678@dataclass(init=False, order=True)9class Money:10units: int11nano: int12MOD: int = 10 ** 91314def __init__(self, value: int | float | Quotation | MoneyValue, nano: int = None):15if nano:16assert isinstance(value, int), 'if nano is present, value must be int'17assert isinstance(nano, int), 'nano must be int'18self.units = value19self.nano = nano20else:21match value:22case int() as value:23self.units = value24self.nano = 025case float() as value:26self.units = int(math.floor(value))27self.nano = int((value - math.floor(value)) * self.MOD)28case Quotation() | MoneyValue() as value:29self.units = value.units30self.nano = value.nano31case _:32raise ValueError(f'{type(value)} is not supported as initial value for Money')3334def __float__(self):35return self.units + self.nano / self.MOD3637def to_float(self):38return float(self)3940def to_quotation(self):41return Quotation(self.units, self.nano)4243def to_money_value(self, currency: str):44return MoneyValue(currency, self.units, self.nano)4546def __add__(self, other: Money) -> Money:47print(self.units + other.units + (self.nano + other.nano) // self.MOD)48print((self.nano + other.nano) % self.MOD)49return Money(50self.units + other.units + (self.nano + other.nano) // self.MOD,51(self.nano + other.nano) % self.MOD52)5354def __neg__(self) -> Money:55return Money(-self.units, -self.nano)5657def __sub__(self, other: Money) -> Money:58return self + -other5960def __mul__(self, other: int) -> Money:61return Money(self.units * other + (self.nano * other) // self.MOD, (self.nano * other) % self.MOD)6263def __str__(self) -> str:64return f'<Money units={self.units} nano={self.nano}>'656667