From fcfde43b503414ed718d274bbc7d3ce78cbbcd9f Mon Sep 17 00:00:00 2001 From: Garrett Michael Flynn Date: Wed, 15 May 2024 11:42:48 -0700 Subject: [PATCH 1/7] Allow manual updates --- src/tqdm_publisher/_subscriber.py | 4 ++-- src/tqdm_publisher/testing.py | 1 - tests/test_subscriber.py | 35 ++++++++++++++++++++++++++++++- 3 files changed, 36 insertions(+), 4 deletions(-) diff --git a/src/tqdm_publisher/_subscriber.py b/src/tqdm_publisher/_subscriber.py index e5715b3..69f10a7 100644 --- a/src/tqdm_publisher/_subscriber.py +++ b/src/tqdm_publisher/_subscriber.py @@ -1,10 +1,10 @@ -from typing import Any, Dict, Iterable +from typing import Any, Dict, Iterable, Optional from ._publisher import TQDMProgressPublisher class TQDMProgressSubscriber(TQDMProgressPublisher): - def __init__(self, iterable: Iterable[Any], on_progress_update: callable, **tqdm_kwargs): + def __init__(self, on_progress_update: callable, iterable: Optional[Iterable[Any]]=None, **tqdm_kwargs): super().__init__(iterable=iterable, **tqdm_kwargs) def run_on_progress_update(format_dict: Dict[str, Any]): diff --git a/src/tqdm_publisher/testing.py b/src/tqdm_publisher/testing.py index 1b499e2..4baae0b 100644 --- a/src/tqdm_publisher/testing.py +++ b/src/tqdm_publisher/testing.py @@ -7,7 +7,6 @@ async def sleep_func(sleep_duration: float = 1) -> float: def create_tasks(number_of_tasks: int = 10**5): - number_of_tasks = 10**5 sleep_durations = [random.uniform(0, 5.0) for _ in range(number_of_tasks)] tasks = list() diff --git a/tests/test_subscriber.py b/tests/test_subscriber.py index 7ac3a0d..e187ce9 100644 --- a/tests/test_subscriber.py +++ b/tests/test_subscriber.py @@ -30,10 +30,43 @@ def test_callback(data): assert "n" in format and "total" in format tasks = create_tasks() - subscriber = TQDMProgressSubscriber(asyncio.as_completed(tasks), test_callback, total=len(tasks)) + subscriber = TQDMProgressSubscriber(test_callback, asyncio.as_completed(tasks), total=len(tasks)) # Simulate an update to trigger the callback for f in subscriber: await f assert n_callback_executions > 1 + + +# Test manual update management +@pytest.mark.asyncio +async def test_manual_updates(): + n_callback_executions = 0 + + def test_callback(data): + nonlocal n_callback_executions + n_callback_executions += 1 + + print(data) + + assert "progress_bar_id" in data + identifier = data["progress_bar_id"] + assert str(UUID(identifier, version=4)) == identifier + + assert "format_dict" in data + format = data["format_dict"] + assert "n" in format and "total" in format + + tasks = create_tasks(10) + total = len(tasks) + subscriber = TQDMProgressSubscriber(test_callback, total=total) + + # Simulate updatse to trigger the callback + for task in asyncio.as_completed(tasks): + await task + subscriber.update(1) # Update by 1 iteration + + assert n_callback_executions > 1 + + subscriber.close() \ No newline at end of file From 45a8e53fd034cb534de8a1d16c04f493add182ec Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 15 May 2024 18:43:57 +0000 Subject: [PATCH 2/7] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- src/tqdm_publisher/_subscriber.py | 2 +- tests/test_subscriber.py | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/tqdm_publisher/_subscriber.py b/src/tqdm_publisher/_subscriber.py index 69f10a7..083b7b4 100644 --- a/src/tqdm_publisher/_subscriber.py +++ b/src/tqdm_publisher/_subscriber.py @@ -4,7 +4,7 @@ class TQDMProgressSubscriber(TQDMProgressPublisher): - def __init__(self, on_progress_update: callable, iterable: Optional[Iterable[Any]]=None, **tqdm_kwargs): + def __init__(self, on_progress_update: callable, iterable: Optional[Iterable[Any]] = None, **tqdm_kwargs): super().__init__(iterable=iterable, **tqdm_kwargs) def run_on_progress_update(format_dict: Dict[str, Any]): diff --git a/tests/test_subscriber.py b/tests/test_subscriber.py index e187ce9..a91e2a0 100644 --- a/tests/test_subscriber.py +++ b/tests/test_subscriber.py @@ -65,8 +65,8 @@ def test_callback(data): # Simulate updatse to trigger the callback for task in asyncio.as_completed(tasks): await task - subscriber.update(1) # Update by 1 iteration + subscriber.update(1) # Update by 1 iteration - assert n_callback_executions > 1 + assert n_callback_executions > 1 - subscriber.close() \ No newline at end of file + subscriber.close() From c5f91541c081837d58d01c21d8f52ab78fec6b3b Mon Sep 17 00:00:00 2001 From: Garrett Michael Flynn Date: Wed, 15 May 2024 12:03:48 -0700 Subject: [PATCH 3/7] Update test_subscriber.py --- tests/test_subscriber.py | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/tests/test_subscriber.py b/tests/test_subscriber.py index e187ce9..a44a623 100644 --- a/tests/test_subscriber.py +++ b/tests/test_subscriber.py @@ -30,13 +30,19 @@ def test_callback(data): assert "n" in format and "total" in format tasks = create_tasks() - subscriber = TQDMProgressSubscriber(test_callback, asyncio.as_completed(tasks), total=len(tasks)) + total = len(tasks) + subscriber = TQDMProgressSubscriber( + test_callback, + asyncio.as_completed(tasks), + total=total, + mininterval=0, + ) # Simulate an update to trigger the callback for f in subscriber: await f - assert n_callback_executions > 1 + assert n_callback_executions == total + 1 # Test manual update management @@ -60,13 +66,17 @@ def test_callback(data): tasks = create_tasks(10) total = len(tasks) - subscriber = TQDMProgressSubscriber(test_callback, total=total) + subscriber = TQDMProgressSubscriber( + test_callback, + total=total, + mininterval=0 + ) # Simulate updatse to trigger the callback for task in asyncio.as_completed(tasks): await task subscriber.update(1) # Update by 1 iteration - assert n_callback_executions > 1 + assert n_callback_executions == total + 1 subscriber.close() \ No newline at end of file From 647f1ae3a82d71350f71689b78037a1e0b482042 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 15 May 2024 19:04:27 +0000 Subject: [PATCH 4/7] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tests/test_subscriber.py | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/tests/test_subscriber.py b/tests/test_subscriber.py index 1853b60..622619f 100644 --- a/tests/test_subscriber.py +++ b/tests/test_subscriber.py @@ -32,8 +32,8 @@ def test_callback(data): tasks = create_tasks() total = len(tasks) subscriber = TQDMProgressSubscriber( - test_callback, - asyncio.as_completed(tasks), + test_callback, + asyncio.as_completed(tasks), total=total, mininterval=0, ) @@ -66,11 +66,7 @@ def test_callback(data): tasks = create_tasks(10) total = len(tasks) - subscriber = TQDMProgressSubscriber( - test_callback, - total=total, - mininterval=0 - ) + subscriber = TQDMProgressSubscriber(test_callback, total=total, mininterval=0) # Simulate updatse to trigger the callback for task in asyncio.as_completed(tasks): From 3c85e514245fb3642b99266654b179e18a42fff9 Mon Sep 17 00:00:00 2001 From: Garrett Michael Flynn Date: Mon, 20 May 2024 07:55:53 -0700 Subject: [PATCH 5/7] Update arguments and changelog --- CHANGELOG.md | 3 +++ src/tqdm_publisher/_handler.py | 7 +++++-- src/tqdm_publisher/_publisher.py | 4 ++-- src/tqdm_publisher/_subscriber.py | 10 ++++++++-- tests/test_handler.py | 3 ++- tests/test_subscriber.py | 4 ++-- 6 files changed, 22 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5d9fa00..e55ec92 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +## Unreleased +* Update the arguments for all `tqdm_publisher` classes to mirror the `tqdm` constructor, adding additional parameters as required keyword arguments. + ## v0.1.0 (April 26th, 2024) * The first alpha release of `tqdm_publisher`. diff --git a/src/tqdm_publisher/_handler.py b/src/tqdm_publisher/_handler.py index d39a29c..b9f5d3d 100644 --- a/src/tqdm_publisher/_handler.py +++ b/src/tqdm_publisher/_handler.py @@ -17,7 +17,10 @@ def listen(self) -> queue.Queue: return new_queue def create_progress_subscriber( - self, iterable: Iterable[Any], additional_metadata: dict = dict(), **tqdm_kwargs + self, + *tqdm_args, + additional_metadata: dict = dict(), + **tqdm_kwargs ) -> TQDMProgressSubscriber: def on_progress_update(progress_update: dict): @@ -31,7 +34,7 @@ def on_progress_update(progress_update: dict): """ self.announce(message=dict(**progress_update, **additional_metadata)) - return TQDMProgressSubscriber(iterable=iterable, on_progress_update=on_progress_update, **tqdm_kwargs) + return TQDMProgressSubscriber(*tqdm_args, on_progress_update=on_progress_update, **tqdm_kwargs) def announce(self, message: Dict[Any, Any]): """ diff --git a/src/tqdm_publisher/_publisher.py b/src/tqdm_publisher/_publisher.py index 7ff34c2..e9fdb4f 100644 --- a/src/tqdm_publisher/_publisher.py +++ b/src/tqdm_publisher/_publisher.py @@ -5,8 +5,8 @@ class TQDMProgressPublisher(base_tqdm): - def __init__(self, *args, **kwargs): - super().__init__(*args, **kwargs) + def __init__(self, *tqdm_args, **tqdm_kwargs): + super().__init__(*tqdm_args, **tqdm_kwargs) self.progress_bar_id = str(uuid4()) self.callbacks = dict() diff --git a/src/tqdm_publisher/_subscriber.py b/src/tqdm_publisher/_subscriber.py index 083b7b4..a3266a4 100644 --- a/src/tqdm_publisher/_subscriber.py +++ b/src/tqdm_publisher/_subscriber.py @@ -4,8 +4,14 @@ class TQDMProgressSubscriber(TQDMProgressPublisher): - def __init__(self, on_progress_update: callable, iterable: Optional[Iterable[Any]] = None, **tqdm_kwargs): - super().__init__(iterable=iterable, **tqdm_kwargs) + def __init__( + self, + *tqdm_args, + on_progress_update: callable, + **tqdm_kwargs + ): + + super().__init__(*tqdm_args, **tqdm_kwargs) def run_on_progress_update(format_dict: Dict[str, Any]): """ diff --git a/tests/test_handler.py b/tests/test_handler.py index ff868d1..58f97a0 100644 --- a/tests/test_handler.py +++ b/tests/test_handler.py @@ -40,7 +40,8 @@ def test_callback(data): for _ in range(N_SUBSCRIBERS): subscriber = handler.create_progress_subscriber( - asyncio.as_completed(create_tasks(number_of_tasks=N_TASKS_PER_SUBSCRIBER)), total=N_TASKS_PER_SUBSCRIBER + asyncio.as_completed(create_tasks(number_of_tasks=N_TASKS_PER_SUBSCRIBER)), + total=N_TASKS_PER_SUBSCRIBER ) for f in subscriber: diff --git a/tests/test_subscriber.py b/tests/test_subscriber.py index 622619f..f40bb19 100644 --- a/tests/test_subscriber.py +++ b/tests/test_subscriber.py @@ -32,10 +32,10 @@ def test_callback(data): tasks = create_tasks() total = len(tasks) subscriber = TQDMProgressSubscriber( - test_callback, asyncio.as_completed(tasks), total=total, mininterval=0, + on_progress_update=test_callback, ) # Simulate an update to trigger the callback @@ -66,7 +66,7 @@ def test_callback(data): tasks = create_tasks(10) total = len(tasks) - subscriber = TQDMProgressSubscriber(test_callback, total=total, mininterval=0) + subscriber = TQDMProgressSubscriber(on_progress_update=test_callback, total=total, mininterval=0) # Simulate updatse to trigger the callback for task in asyncio.as_completed(tasks): From 08ac3e87b37cdeb6b81baea3f99480f5b11fdc0d Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 20 May 2024 14:56:02 +0000 Subject: [PATCH 6/7] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- src/tqdm_publisher/_handler.py | 5 +---- src/tqdm_publisher/_subscriber.py | 9 ++------- tests/test_handler.py | 3 +-- 3 files changed, 4 insertions(+), 13 deletions(-) diff --git a/src/tqdm_publisher/_handler.py b/src/tqdm_publisher/_handler.py index b9f5d3d..d8bee4e 100644 --- a/src/tqdm_publisher/_handler.py +++ b/src/tqdm_publisher/_handler.py @@ -17,10 +17,7 @@ def listen(self) -> queue.Queue: return new_queue def create_progress_subscriber( - self, - *tqdm_args, - additional_metadata: dict = dict(), - **tqdm_kwargs + self, *tqdm_args, additional_metadata: dict = dict(), **tqdm_kwargs ) -> TQDMProgressSubscriber: def on_progress_update(progress_update: dict): diff --git a/src/tqdm_publisher/_subscriber.py b/src/tqdm_publisher/_subscriber.py index a3266a4..1b3eb53 100644 --- a/src/tqdm_publisher/_subscriber.py +++ b/src/tqdm_publisher/_subscriber.py @@ -4,13 +4,8 @@ class TQDMProgressSubscriber(TQDMProgressPublisher): - def __init__( - self, - *tqdm_args, - on_progress_update: callable, - **tqdm_kwargs - ): - + def __init__(self, *tqdm_args, on_progress_update: callable, **tqdm_kwargs): + super().__init__(*tqdm_args, **tqdm_kwargs) def run_on_progress_update(format_dict: Dict[str, Any]): diff --git a/tests/test_handler.py b/tests/test_handler.py index 58f97a0..ff868d1 100644 --- a/tests/test_handler.py +++ b/tests/test_handler.py @@ -40,8 +40,7 @@ def test_callback(data): for _ in range(N_SUBSCRIBERS): subscriber = handler.create_progress_subscriber( - asyncio.as_completed(create_tasks(number_of_tasks=N_TASKS_PER_SUBSCRIBER)), - total=N_TASKS_PER_SUBSCRIBER + asyncio.as_completed(create_tasks(number_of_tasks=N_TASKS_PER_SUBSCRIBER)), total=N_TASKS_PER_SUBSCRIBER ) for f in subscriber: From f6e9a8fcb83e42c7d7ac64e64ef7f021bf8bcea0 Mon Sep 17 00:00:00 2001 From: Cody Baker <51133164+CodyCBakerPhD@users.noreply.github.com> Date: Mon, 20 May 2024 11:03:44 -0400 Subject: [PATCH 7/7] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e55ec92..651a446 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,5 @@ ## Unreleased -* Update the arguments for all `tqdm_publisher` classes to mirror the `tqdm` constructor, adding additional parameters as required keyword arguments. +* Update the arguments for all `tqdm_publisher` classes to mirror the `tqdm` constructor, adding additional parameters as required keyword arguments. [PR #65](https://github.com/catalystneuro/tqdm_publisher/pull/65) ## v0.1.0 (April 26th, 2024) * The first alpha release of `tqdm_publisher`.