classes.c22_frame_iterator
1from collections.abc import Callable 2from classes import KBox 3from lib import layout 4from icecream import ic 5 6 7KFrameIteratorFunc = Callable[[KBox, any, int], None | bool] 8"""A callable type for functions used in KFrameIterator. 9 10The function takes three arguments: 11- `box`: An instance of `KBox` representing the current box. 12- `item`: The current item from the items list. 13- `index`: The index of the current item. 14 15The function can return `None` or `False`. If it returns `False`, the iterator will not advance to the next position. 16""" 17 18 19class KFrameIterator: 20 Func = KFrameIteratorFunc 21 22 def __init__( 23 self, 24 frame: layout.Coordinates, 25 advanceY: float, 26 items: list, 27 func: KFrameIteratorFunc, 28 ): 29 """ 30 Initialize a KFrameIterator instance. 31 32 Args: 33 frame: A tuple representing the (x, y, width, height) of the frame 34 advanceY: The vertical step size to move down after each item. 35 items: A list of items to iterate over. 36 func: A function to call for each item, with signature (box: KBox, item: any, index: int) -> None | False. 37 If the function returns False, the iterator will not advance to the next position. 38 """ 39 self.x, self.y, self.w, self.h = frame 40 self.currentY = self.y + self.h - advanceY 41 42 self.advanceY = advanceY 43 44 self.items = items 45 self.func = func 46 self.index = 0 47 48 def __iter__(self): 49 return self 50 51 def __next__(self): 52 if self.currentY >= self.y and self.items: 53 item = self.items.pop(0) 54 box = KBox((self.x, self.currentY, self.w, self.advanceY)) 55 result = self.func(box, item, self.index) 56 # Only advance if the function did not return False 57 if result is not False: 58 self.currentY -= self.advanceY 59 self.index += 1 60 return result 61 else: 62 raise StopIteration 63 64 def iterate(self) -> "KFrameIterator": 65 """Iterate through all items, calling the function for each.""" 66 for _ in self: 67 pass # The function is already called in __next__ 68 return self
KFrameIteratorFunc =
collections.abc.Callable[[classes.c20_box.KBox, <built-in function any>, int], None | bool]
A callable type for functions used in KFrameIterator.
The function takes three arguments:
box: An instance ofKBoxrepresenting the current box.item: The current item from the items list.index: The index of the current item.
The function can return None or False. If it returns False, the iterator will not advance to the next position.
class
KFrameIterator:
20class KFrameIterator: 21 Func = KFrameIteratorFunc 22 23 def __init__( 24 self, 25 frame: layout.Coordinates, 26 advanceY: float, 27 items: list, 28 func: KFrameIteratorFunc, 29 ): 30 """ 31 Initialize a KFrameIterator instance. 32 33 Args: 34 frame: A tuple representing the (x, y, width, height) of the frame 35 advanceY: The vertical step size to move down after each item. 36 items: A list of items to iterate over. 37 func: A function to call for each item, with signature (box: KBox, item: any, index: int) -> None | False. 38 If the function returns False, the iterator will not advance to the next position. 39 """ 40 self.x, self.y, self.w, self.h = frame 41 self.currentY = self.y + self.h - advanceY 42 43 self.advanceY = advanceY 44 45 self.items = items 46 self.func = func 47 self.index = 0 48 49 def __iter__(self): 50 return self 51 52 def __next__(self): 53 if self.currentY >= self.y and self.items: 54 item = self.items.pop(0) 55 box = KBox((self.x, self.currentY, self.w, self.advanceY)) 56 result = self.func(box, item, self.index) 57 # Only advance if the function did not return False 58 if result is not False: 59 self.currentY -= self.advanceY 60 self.index += 1 61 return result 62 else: 63 raise StopIteration 64 65 def iterate(self) -> "KFrameIterator": 66 """Iterate through all items, calling the function for each.""" 67 for _ in self: 68 pass # The function is already called in __next__ 69 return self
KFrameIterator( frame: tuple[float, float, float, float], advanceY: float, items: list, func: Callable[[classes.c20_box.KBox, <built-in function any>, int], None | bool])
23 def __init__( 24 self, 25 frame: layout.Coordinates, 26 advanceY: float, 27 items: list, 28 func: KFrameIteratorFunc, 29 ): 30 """ 31 Initialize a KFrameIterator instance. 32 33 Args: 34 frame: A tuple representing the (x, y, width, height) of the frame 35 advanceY: The vertical step size to move down after each item. 36 items: A list of items to iterate over. 37 func: A function to call for each item, with signature (box: KBox, item: any, index: int) -> None | False. 38 If the function returns False, the iterator will not advance to the next position. 39 """ 40 self.x, self.y, self.w, self.h = frame 41 self.currentY = self.y + self.h - advanceY 42 43 self.advanceY = advanceY 44 45 self.items = items 46 self.func = func 47 self.index = 0
Initialize a KFrameIterator instance.
Arguments:
- frame: A tuple representing the (x, y, width, height) of the frame
- advanceY: The vertical step size to move down after each item.
- items: A list of items to iterate over.
- func: A function to call for each item, with signature (box: KBox, item: any, index: int) -> None | False. If the function returns False, the iterator will not advance to the next position.
Func =
collections.abc.Callable[[classes.c20_box.KBox, <built-in function any>, int], None | bool]