Created
February 17, 2024 08:57
-
-
Save SofaVc/65d910f007769fd733b451466a6c2588 to your computer and use it in GitHub Desktop.
class ReactNumber
This file contains hidden or 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
| class ReactNumber(Observerble, Observer): | |
| def __init__(self, number: Union[int, float, Strategy]): | |
| super().__init__(number) | |
| self.value = number | |
| @property | |
| def value(self) -> Union[int, float]: | |
| if self._number is None and self._strategy is not None: | |
| self._number = self._strategy.eval() | |
| return self._number | |
| @value.setter | |
| def value(self, new_value: Union[int, float, Strategy]) -> None: | |
| self._number = None | |
| self._strategy = None | |
| if isinstance(new_value, Strategy): | |
| self._strategy = new_value | |
| else: | |
| self._number = new_value | |
| self.notify() | |
| def __str__(self) -> str: | |
| return str(self.value) | |
| def __repr__(self) -> str: | |
| return str(self) | |
| def update(self, *args, **kwargs) -> None: | |
| if self._strategy is not None: | |
| self._number = self._strategy.eval() | |
| self.notify() | |
| def notify(self) -> None: | |
| for observer in self._observers_list: | |
| if hasattr(observer, 'update'): | |
| observer.update(self) | |
| def __mul__(self, other) -> 'ReactNumber': | |
| """ | |
| Функция умножения | |
| :param other: другой объект | |
| :return: возвращает новое число | |
| """ | |
| new_number = ReactNumber(MultiplicationStrategy(self, other)) | |
| self.attach(new_number) | |
| if isinstance(other, ReactNumber): | |
| other.attach(new_number) | |
| return new_number | |
| def __rmul__(self, other) -> 'ReactNumber': | |
| """ | |
| Функция отражённое умножение | |
| :param other: другой объект | |
| :return: возвращает новое число | |
| """ | |
| new_number = ReactNumber(MultiplicationStrategy(other, self)) | |
| self.attach(new_number) | |
| if isinstance(other, ReactNumber): | |
| other.attach(new_number) | |
| return new_number | |
| def __add__(self, other) -> 'ReactNumber': | |
| """ | |
| Функция сложения | |
| :param other: другой объект | |
| :return: возвращает новое число | |
| """ | |
| new_number = ReactNumber(SummatoryStrategy(self, other)) | |
| self.attach(new_number) | |
| if isinstance(other, ReactNumber): | |
| other.attach(new_number) | |
| return new_number | |
| def __radd__(self, other) -> 'ReactNumber': | |
| """ | |
| Функция отражённое сложение | |
| :param other: другой объект | |
| :return: возвращает новое число | |
| """ | |
| new_number = ReactNumber(SummatoryStrategy(other, self)) | |
| self.attach(new_number) | |
| if isinstance(other, ReactNumber): | |
| other.attach(new_number) | |
| return new_number | |
| def __sub__(self, other) -> 'ReactNumber': | |
| """ | |
| Функция вычитания | |
| :param other: другой объект | |
| :return: возвращает новое число | |
| """ | |
| new_number = ReactNumber(DifferenceStrategy(self, other)) | |
| self.attach(new_number) | |
| if isinstance(other, ReactNumber): | |
| other.attach(new_number) | |
| return new_number | |
| def __rsub__(self, other) -> 'ReactNumber': | |
| """ | |
| Функция отражённое вычитание | |
| :param other: другой объект | |
| :return: возвращает новое число | |
| """ | |
| new_number = ReactNumber(DifferenceStrategy(self, other)) | |
| self.attach(new_number) | |
| if isinstance(other, ReactNumber): | |
| other.attach(new_number) | |
| return new_number * -1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment