LogarithmPlotter/runtime-pyside6/tests/plugins/natural/interfaces/int.py

321 lines
8.4 KiB
Python
Raw Normal View History

2024-10-16 20:18:53 +00:00
"""
* LogarithmPlotter - 2D plotter software to make BODE plots, sequences and distribution functions.
* Copyright (C) 2021-2024 Ad5001
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
from math import log10, floor
2024-10-16 20:18:53 +00:00
from typing import Self
2024-10-17 00:08:24 +00:00
from .assertion import Assertion
from .base import AssertionInterface
from .utils import repr_
class NumberComparisonAssertionInterface(AssertionInterface):
def __init__(self, value, parent: AssertionInterface = None):
super().__init__(value, parent)
self._compare_stack = []
def _generate_compare_to(self) -> int:
"""
The number generated by the comparison stack.
E.g. can parse one.hundred.million.and.thirty.three.thousand.and.twelve.hundred.and.seven
as ['one', 'hundred', 'million', 'thirty', 'three', 'thousand', 'twelve', 'hundred', 'seven']
which results 100,034,207
"""
minus = len(self._compare_stack) > 0 and self._compare_stack[0] == -1
if len(self._compare_stack) < (2 if minus else 1):
raise RuntimeError("No number to compare the value to provided.")
if minus:
self._compare_stack.pop(0)
# Compute the number
add_stack = [self._compare_stack.pop(0)]
for element in self._compare_stack:
last_power = floor(log10(abs(add_stack[-1])))
current_power = floor(log10(abs(element)))
if last_power < current_power: # E.g. one hundred
add_stack[-1] *= element
elif last_power == 1 and current_power == 0: # E.g thirty four
add_stack[-1] += element
elif last_power > current_power: # E.g a hundred and five
add_stack.append(element)
else:
raise RuntimeError(f"Cannot chain two numbers with the same power ({add_stack[-1]} => {element}.")
total = sum(add_stack)
return -total if minus else total
2024-10-16 20:18:53 +00:00
def _compare(self) -> Assertion:
raise RuntimeError(f"No comparison method defined in {type(self).__name__}.")
def __bool__(self) -> bool:
return bool(self._compare())
def __call__(self, compare_to: int) -> Self:
2024-10-17 00:08:24 +00:00
if type(compare_to) not in (float, int):
raise RuntimeError(f"Cannot compare number ({self._value}) to non number ({repr_(compare_to)}).")
self._compare_stack.append(compare_to)
2024-10-16 20:18:53 +00:00
return self
2024-10-17 00:08:24 +00:00
"""
Chain self properties
"""
2024-10-16 20:18:53 +00:00
@property
2024-10-17 00:08:24 +00:00
def and_(self) -> Self:
2024-10-16 20:18:53 +00:00
return self
@property
2024-10-17 00:08:24 +00:00
def AND(self) -> Self:
2024-10-16 20:18:53 +00:00
return self
2024-10-17 00:08:24 +00:00
"""
Number shorthands
"""
2024-10-16 20:18:53 +00:00
@property
def once(self) -> Self:
return self(1)
@property
def twice(self) -> Self:
return self(2)
@property
def thrice(self) -> Self:
return self(3)
2024-10-17 00:08:24 +00:00
@property
def minus(self) -> Self:
return self(-1)
2024-10-16 20:18:53 +00:00
@property
def zero(self) -> Self:
return self(0)
@property
def one(self) -> Self:
return self(1)
@property
def two(self) -> Self:
return self(2)
@property
def three(self) -> Self:
return self(3)
@property
def four(self) -> Self:
return self(4)
@property
def five(self) -> Self:
return self(5)
@property
def six(self) -> Self:
return self(6)
@property
def seven(self) -> Self:
return self(7)
@property
def eight(self) -> Self:
return self(8)
@property
def nine(self) -> Self:
return self(9)
@property
def ten(self) -> Self:
return self(10)
2024-10-17 00:08:24 +00:00
@property
def eleven(self) -> Self:
return self(11)
@property
def twelve(self) -> Self:
return self(12)
@property
def thirteen(self) -> Self:
return self(13)
@property
def fourteen(self) -> Self:
return self(14)
@property
def fifteen(self) -> Self:
return self(15)
@property
def sixteen(self) -> Self:
return self(16)
@property
def seventeen(self) -> Self:
return self(17)
@property
def eighteen(self) -> Self:
return self(18)
@property
def nineteen(self) -> Self:
return self(19)
2024-10-16 20:18:53 +00:00
@property
def twenty(self) -> Self:
return self(20)
@property
def thirty(self) -> Self:
return self(30)
@property
def forty(self) -> Self:
return self(40)
@property
def fifty(self) -> Self:
return self(50)
@property
def sixty(self) -> Self:
return self(60)
@property
def seventy(self) -> Self:
return self(70)
@property
def eighty(self) -> Self:
2024-10-17 00:08:24 +00:00
return self(80)
2024-10-16 20:18:53 +00:00
@property
def ninety(self) -> Self:
2024-10-17 00:08:24 +00:00
return self(90)
2024-10-16 20:18:53 +00:00
@property
def hundred(self) -> Self:
return self(100)
@property
def thousand(self) -> Self:
return self(1_000)
@property
def million(self) -> Self:
return self(1_000_000)
@property
def billion(self) -> Self:
return self(1_000_000_000)
2024-10-17 00:08:24 +00:00
class LessThanComparisonInterface(NumberComparisonAssertionInterface):
2024-10-16 20:18:53 +00:00
def _compare(self) -> Assertion:
2024-10-17 00:08:24 +00:00
compare = self._generate_compare_to()
2024-10-16 20:18:53 +00:00
return Assertion(
2024-10-17 00:08:24 +00:00
self._value < compare,
f"The value ({repr_(self._value)}) is not less than to {repr_(compare)}.",
self._not
2024-10-16 20:18:53 +00:00
)
2024-10-17 00:08:24 +00:00
class MoreThanComparisonInterface(NumberComparisonAssertionInterface):
2024-10-16 20:18:53 +00:00
def _compare(self) -> Assertion:
2024-10-17 00:08:24 +00:00
compare = self._generate_compare_to()
2024-10-16 20:18:53 +00:00
return Assertion(
2024-10-17 00:08:24 +00:00
self._value > compare,
f"The value ({repr_(self._value)}) is not more than to {repr_(compare)}.",
self._not
2024-10-16 20:18:53 +00:00
)
2024-10-17 00:08:24 +00:00
class AtLeastComparisonInterface(NumberComparisonAssertionInterface):
2024-10-16 20:18:53 +00:00
def _compare(self) -> Assertion:
2024-10-17 00:08:24 +00:00
compare = self._generate_compare_to()
2024-10-16 20:18:53 +00:00
return Assertion(
2024-10-17 00:08:24 +00:00
self._value >= compare,
f"The value ({repr_(self._value)}) is not at least to {repr_(compare)}.",
self._not
2024-10-16 20:18:53 +00:00
)
2024-10-17 00:08:24 +00:00
class AtMostComparisonInterface(NumberComparisonAssertionInterface):
2024-10-16 20:18:53 +00:00
def _compare(self) -> Assertion:
2024-10-17 00:08:24 +00:00
compare = self._generate_compare_to()
2024-10-16 20:18:53 +00:00
return Assertion(
2024-10-17 00:08:24 +00:00
self._value <= compare,
f"The value ({repr_(self._value)}) is not at least to {repr_(compare)}.",
self._not
2024-10-16 20:18:53 +00:00
)
2024-10-17 00:08:24 +00:00
class EqualComparisonInterface(NumberComparisonAssertionInterface):
2024-10-16 20:18:53 +00:00
def _compare(self) -> Assertion:
2024-10-17 00:08:24 +00:00
compare = self._generate_compare_to()
2024-10-16 20:18:53 +00:00
return Assertion(
2024-10-17 00:08:24 +00:00
self._value == compare,
f"The value ({repr_(self._value)}) is not equal to {repr_(compare)}.",
self._not
2024-10-16 20:18:53 +00:00
)
2024-10-17 00:08:24 +00:00
@property
2024-10-16 20:18:53 +00:00
def to(self) -> Self:
return self
2024-10-17 00:08:24 +00:00
class NumberInterface(AssertionInterface):
def __call__(self, value):
return EqualComparisonInterface(self._value, self)(value)
2024-10-16 20:18:53 +00:00
2024-10-17 00:08:24 +00:00
@property
def equals(self) -> EqualComparisonInterface:
return EqualComparisonInterface(self._value, self)
2024-10-16 20:18:53 +00:00
@property
2024-10-17 00:08:24 +00:00
def equal(self) -> EqualComparisonInterface:
return EqualComparisonInterface(self._value, self)
2024-10-16 20:18:53 +00:00
@property
2024-10-17 00:08:24 +00:00
def exactly(self) -> EqualComparisonInterface:
return EqualComparisonInterface(self._value, self)
2024-10-16 20:18:53 +00:00
@property
2024-10-17 00:08:24 +00:00
def of(self) -> EqualComparisonInterface:
return EqualComparisonInterface(self._value, self)
2024-10-16 20:18:53 +00:00
@property
2024-10-17 00:08:24 +00:00
def less_than(self) -> LessThanComparisonInterface:
return LessThanComparisonInterface(self._value, self)
2024-10-16 20:18:53 +00:00
@property
2024-10-17 00:08:24 +00:00
def more_than(self) -> MoreThanComparisonInterface:
return MoreThanComparisonInterface(self._value, self)
2024-10-16 20:18:53 +00:00
@property
2024-10-17 00:08:24 +00:00
def at_least(self) -> AtLeastComparisonInterface:
return AtLeastComparisonInterface(self._value, self)
2024-10-16 20:18:53 +00:00
2024-10-17 00:08:24 +00:00
@property
def at_most(self) -> AtMostComparisonInterface:
return AtMostComparisonInterface(self._value, self)