Skip to content

Commit

Permalink
Add print_interval
Browse files Browse the repository at this point in the history
  • Loading branch information
doruirimescu committed Jun 10, 2024
1 parent 02e08c5 commit 7d29d01
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
5 changes: 4 additions & 1 deletion src/stateful_data_processor/processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ def __init__(
file_rw: FileRW,
logger: Optional[Logger] = None,
should_read: Optional[bool] = True,
print_interval: Optional[int] = 1,
):
self.file_rw = file_rw
self.print_interval = print_interval
if logger is None:
self.logger = getLogger("StatefulDataProcessor")
else:
Expand Down Expand Up @@ -77,7 +79,8 @@ def _iterate_items(self, items: Collection[Any], *args, **kwargs):
continue

self.process_item(item, iteration_index, *args, **kwargs)
self.logger.info(f"Processed item {item} {len(self.data)} / {items_len}")
if (iteration_index) % self.print_interval == 0:
self.logger.info(f"Processed item {item} {len(self.data)} / {items_len}")
self.logger.info("Finished processing all items.")

@abstractmethod
Expand Down
15 changes: 7 additions & 8 deletions tests/test_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def process_item(


class NumberProcessor(StatefulDataProcessor):
LOOKUP = ["a", "b", "c"]
LOOKUP = ["a", "b", "c", "d"]

def process_item(
self, item: int, iteration_index: int, delay=0.0, *args: Any, **kwargs: Any
Expand Down Expand Up @@ -172,17 +172,16 @@ def test_resumes_after_termination_with_saved_state(self):
]
self.mock_logger.info.assert_has_calls(calls, any_order=True)

def test_number_processor(self):
def test_number_processor_print_interval(self):
processor = NumberProcessor(
self.file_rw, should_read=False, logger=self.mock_logger
self.file_rw, should_read=False, logger=self.mock_logger, print_interval=2
)
processor.run(items=[1, 2, 3], delay=0)
self.assertEqual(processor.data, {1: "a1", 2: "b4", 3: "c9"})
processor.run(items=[1, 2, 3, 4], delay=0)
self.assertEqual(processor.data, {1: "a1", 2: "b4", 3: "c9", 4: "d16"})

calls = [
call("Processed item 1 1 / 3"),
call("Processed item 2 2 / 3"),
call("Processed item 3 3 / 3"),
call("Processed item 1 1 / 4"),
call("Processed item 3 3 / 4"),
call("Finished processing all items."),
]
self.mock_logger.info.assert_has_calls(calls, any_order=True)

0 comments on commit 7d29d01

Please sign in to comment.