From 9a46fb52bb8bb860a311cce5c035c1a7a86ef7b6 Mon Sep 17 00:00:00 2001 From: eprbell <77937475+eprbell@users.noreply.github.com> Date: Sat, 22 Jun 2024 23:07:09 -0700 Subject: [PATCH] Reorganized class order --- src/rp2/abstract_accounting_method.py | 78 +++++++++++++-------------- 1 file changed, 39 insertions(+), 39 deletions(-) diff --git a/src/rp2/abstract_accounting_method.py b/src/rp2/abstract_accounting_method.py index f2736ac..c20d8ef 100644 --- a/src/rp2/abstract_accounting_method.py +++ b/src/rp2/abstract_accounting_method.py @@ -23,11 +23,6 @@ from rp2.rp2_error import RP2RuntimeError, RP2TypeError -class AbstractAccountingMethodIterator: - def __next__(self) -> InTransaction: - raise NotImplementedError("abstract function") - - class AcquiredLotAndAmount(NamedTuple): acquired_lot: InTransaction amount: RP2Decimal @@ -44,6 +39,45 @@ class AcquiredLotHeapSortKey(NamedTuple): internal_id_int: int +class AbstractAccountingMethodIterator: + def __next__(self) -> InTransaction: + raise NotImplementedError("abstract function") + + +class ListAccountingMethodIterator(AbstractAccountingMethodIterator): + def __init__(self, acquired_lot_list: List[InTransaction], from_index: int, to_index: int, order_type: AcquiredLotCandidatesOrder) -> None: + self.__acquired_lot_list = acquired_lot_list + self.__start_index = from_index if order_type == AcquiredLotCandidatesOrder.OLDER_TO_NEWER else to_index + self.__end_index = to_index if order_type == AcquiredLotCandidatesOrder.OLDER_TO_NEWER else from_index + self.__step = 1 if order_type == AcquiredLotCandidatesOrder.OLDER_TO_NEWER else -1 + self.__index = self.__start_index + self.__order_type = order_type + + def _check_index(self) -> bool: + if self.__order_type == AcquiredLotCandidatesOrder.OLDER_TO_NEWER: + return self.__index <= self.__end_index + return self.__index >= self.__end_index + + def __next__(self) -> InTransaction: + result: Optional[InTransaction] = None + while self._check_index(): + result = self.__acquired_lot_list[self.__index] + self.__index += self.__step + return result + raise StopIteration(self) + + +class HeapAccountingMethodIterator(AbstractAccountingMethodIterator): + def __init__(self, acquired_lot_heap: List[Tuple[AcquiredLotHeapSortKey, InTransaction]]) -> None: + self.__acquired_lot_heap = acquired_lot_heap + + def __next__(self) -> InTransaction: + while len(self.__acquired_lot_heap) > 0: + _, result = heappop(self.__acquired_lot_heap) + return result + raise StopIteration(self) + + class AbstractAcquiredLotCandidates: def __init__( self, @@ -93,40 +127,6 @@ def __iter__(self) -> AbstractAccountingMethodIterator: return self._accounting_method._create_accounting_method_iterator(self) -class ListAccountingMethodIterator(AbstractAccountingMethodIterator): - def __init__(self, acquired_lot_list: List[InTransaction], from_index: int, to_index: int, order_type: AcquiredLotCandidatesOrder) -> None: - self.__acquired_lot_list = acquired_lot_list - self.__start_index = from_index if order_type == AcquiredLotCandidatesOrder.OLDER_TO_NEWER else to_index - self.__end_index = to_index if order_type == AcquiredLotCandidatesOrder.OLDER_TO_NEWER else from_index - self.__step = 1 if order_type == AcquiredLotCandidatesOrder.OLDER_TO_NEWER else -1 - self.__index = self.__start_index - self.__order_type = order_type - - def _check_index(self) -> bool: - if self.__order_type == AcquiredLotCandidatesOrder.OLDER_TO_NEWER: - return self.__index <= self.__end_index - return self.__index >= self.__end_index - - def __next__(self) -> InTransaction: - result: Optional[InTransaction] = None - while self._check_index(): - result = self.__acquired_lot_list[self.__index] - self.__index += self.__step - return result - raise StopIteration(self) - - -class HeapAccountingMethodIterator(AbstractAccountingMethodIterator): - def __init__(self, acquired_lot_heap: List[Tuple[AcquiredLotHeapSortKey, InTransaction]]) -> None: - self.__acquired_lot_heap = acquired_lot_heap - - def __next__(self) -> InTransaction: - while len(self.__acquired_lot_heap) > 0: - _, result = heappop(self.__acquired_lot_heap) - return result - raise StopIteration(self) - - class ListAcquiredLotCandidates(AbstractAcquiredLotCandidates): pass