From 98e7be7b34675203ad1eb7e538b3055148f38244 Mon Sep 17 00:00:00 2001 From: Garrett Michael Flynn Date: Fri, 19 Jan 2024 11:00:45 -0800 Subject: [PATCH 1/6] Add numpy-style docstrings --- src/tqdm_publisher/publisher.py | 91 +++++++++++++++++++++++++++++---- 1 file changed, 82 insertions(+), 9 deletions(-) diff --git a/src/tqdm_publisher/publisher.py b/src/tqdm_publisher/publisher.py index d810e34..16b4000 100644 --- a/src/tqdm_publisher/publisher.py +++ b/src/tqdm_publisher/publisher.py @@ -9,22 +9,95 @@ def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.callbacks = {} - # Override the update method to call callbacks - def update(self, n=1, always_callback=False): - if super().update(n) or always_callback: + # Override the update method to call callbacks + def update(self, n=1): + if super().update(n): for id in list(self.callbacks): callback = self.callbacks.get(id) if callback: callback(self.format_dict) - # Subscribe to updates - def subscribe(self, callback): - callback_id = uuid4() + def subscribe(self, callback: callable): + """ + Subscribe to updates from the progress bar. + + This method assigns a unique ID to the given callback and stores it in an internal + dictionary. It allows the callback to be referenced and activated in the overriden + update function for TQDM. The unique callback ID is returned, which can be used + for future operations such as deregistering the callback. + + + Parameters + ---------- + callback : callable + A callable object (like a function) that will be called back by this object. + The callback should be able to be invoked with a single argument, the progress + bar's format_dict. + + Returns + ------- + str + A unique identifier for the callback. This ID is a hexadecimal UUID string + and can be used to reference the registered callback in future operations. + + Examples + -------- + >>> def my_callback(format_dict): + >>> print("Progress update", format_dict) + >>> + >>> publisher = TQDMPublisher() + >>> callback_id = publisher.subscribe(my_callback) + >>> print(callback_id) # Prints the unique callback ID + """ + + callback_id = uuid4().hex self.callbacks[callback_id] = callback return callback_id - # Unsubscribe from updates - def unsubscribe(self, callback_id): - del self.callbacks[callback_id] \ No newline at end of file + def unsubscribe(self, callback_id: str): + """ + Unsubscribe a previously registered callback from the progress bar updates. + + This method removes the callback associated with the given unique ID from the internal + dictionary. It is used to deregister callbacks that were previously added via the + `subscribe` method. Once a callback is removed, it will no longer be called during + the progress bar's update events. + + Parameters + ---------- + callback_id : str + The unique identifier of the callback to be unsubscribed. This is the same hexadecimal + UUID string that was returned by the `subscribe` method when the callback was registered. + + Returns + ------- + bool + Returns True if the callback was successfully removed, or False if no callback was + found with the given ID. + + Examples + -------- + >>> publisher = TQDMPublisher() + >>> callback_id = publisher.subscribe(my_callback) + >>> print(callback_id) # Prints the unique callback ID + >>> unsubscribed = publisher.unsubscribe(callback_id) + >>> print(unsubscribed) # True if successful, False otherwise + + Raises + ------ + KeyError + If the provided `callback_id` does not correspond to any registered callback. + + Notes + ----- + It's important to manage the lifecycle of callbacks, especially in cases where the + number of subscribers might grow large or change frequently. Unsubscribing callbacks + when they are no longer needed can help prevent memory leaks and other performance issues. + """ + try: + del self.callbacks[callback_id] + return True + except KeyError: + return False \ No newline at end of file From 6ccff8f661fee47eed13cdac527a215a647ca2c8 Mon Sep 17 00:00:00 2001 From: Garrett Michael Flynn Date: Fri, 19 Jan 2024 11:25:26 -0800 Subject: [PATCH 2/6] Update publisher.py --- src/tqdm_publisher/publisher.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/tqdm_publisher/publisher.py b/src/tqdm_publisher/publisher.py index 16b4000..0c57a7c 100644 --- a/src/tqdm_publisher/publisher.py +++ b/src/tqdm_publisher/publisher.py @@ -39,8 +39,8 @@ def subscribe(self, callback: callable): Returns ------- str - A unique identifier for the callback. This ID is a hexadecimal UUID string - and can be used to reference the registered callback in future operations. + A unique identifier for the callback. This ID is a UUID string and can be used + to reference the registered callback in future operations. Examples -------- @@ -52,7 +52,7 @@ def subscribe(self, callback: callable): >>> print(callback_id) # Prints the unique callback ID """ - callback_id = uuid4().hex + callback_id = str(uuid4()) self.callbacks[callback_id] = callback return callback_id @@ -68,8 +68,8 @@ def unsubscribe(self, callback_id: str): Parameters ---------- callback_id : str - The unique identifier of the callback to be unsubscribed. This is the same hexadecimal - UUID string that was returned by the `subscribe` method when the callback was registered. + The unique identifier of the callback to be unsubscribed. This is the same UUID string + that was returned by the `subscribe` method when the callback was registered. Returns ------- From 0629ac5780204dfa21f6e28b6e47617707172c7a Mon Sep 17 00:00:00 2001 From: Garrett Michael Flynn Date: Fri, 19 Jan 2024 13:31:36 -0800 Subject: [PATCH 3/6] Apply suggestions from code review Co-authored-by: Cody Baker <51133164+CodyCBakerPhD@users.noreply.github.com> --- src/tqdm_publisher/publisher.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/tqdm_publisher/publisher.py b/src/tqdm_publisher/publisher.py index 0c57a7c..f9adc02 100644 --- a/src/tqdm_publisher/publisher.py +++ b/src/tqdm_publisher/publisher.py @@ -24,11 +24,10 @@ def subscribe(self, callback: callable): Subscribe to updates from the progress bar. This method assigns a unique ID to the given callback and stores it in an internal - dictionary. It allows the callback to be referenced and activated in the overriden + dictionary. It allows the callback to be referenced and activated in the overridden update function for TQDM. The unique callback ID is returned, which can be used for future operations such as deregistering the callback. - Parameters ---------- callback : callable @@ -96,8 +95,8 @@ def unsubscribe(self, callback_id: str): number of subscribers might grow large or change frequently. Unsubscribing callbacks when they are no longer needed can help prevent memory leaks and other performance issues. """ - try: - del self.callbacks[callback_id] - return True - except KeyError: - return False \ No newline at end of file + if callback_id not in self.callbacks: + return False + + del self.callbacks[callback_id] + return True \ No newline at end of file From 8929876482139aa3ce8af27d757bffbdedef9f9b Mon Sep 17 00:00:00 2001 From: Garrett Michael Flynn Date: Fri, 19 Jan 2024 13:39:24 -0800 Subject: [PATCH 4/6] Update publisher.py --- src/tqdm_publisher/publisher.py | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/tqdm_publisher/publisher.py b/src/tqdm_publisher/publisher.py index f9adc02..695a8fd 100644 --- a/src/tqdm_publisher/publisher.py +++ b/src/tqdm_publisher/publisher.py @@ -84,11 +84,6 @@ def unsubscribe(self, callback_id: str): >>> unsubscribed = publisher.unsubscribe(callback_id) >>> print(unsubscribed) # True if successful, False otherwise - Raises - ------ - KeyError - If the provided `callback_id` does not correspond to any registered callback. - Notes ----- It's important to manage the lifecycle of callbacks, especially in cases where the From 82b7280ef8e007210823d6caae8e8961349acf55 Mon Sep 17 00:00:00 2001 From: Garrett Michael Flynn Date: Fri, 19 Jan 2024 13:40:28 -0800 Subject: [PATCH 5/6] Remove display check --- src/tqdm_publisher/publisher.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/tqdm_publisher/publisher.py b/src/tqdm_publisher/publisher.py index 695a8fd..12c934f 100644 --- a/src/tqdm_publisher/publisher.py +++ b/src/tqdm_publisher/publisher.py @@ -12,11 +12,11 @@ def __init__(self, *args, **kwargs): # Override the update method to call callbacks def update(self, n=1): - if super().update(n): - for id in list(self.callbacks): - callback = self.callbacks.get(id) - if callback: - callback(self.format_dict) + super().update(n) + for id in list(self.callbacks): + callback = self.callbacks.get(id) + if callback: + callback(self.format_dict) def subscribe(self, callback: callable): From 423b0fca512fe95a5c4c1f91d598f4fa20bbc8dc Mon Sep 17 00:00:00 2001 From: Garrett Michael Flynn Date: Fri, 19 Jan 2024 13:57:34 -0800 Subject: [PATCH 6/6] Update publisher.py --- src/tqdm_publisher/publisher.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/tqdm_publisher/publisher.py b/src/tqdm_publisher/publisher.py index 12c934f..a5830d5 100644 --- a/src/tqdm_publisher/publisher.py +++ b/src/tqdm_publisher/publisher.py @@ -12,11 +12,12 @@ def __init__(self, *args, **kwargs): # Override the update method to call callbacks def update(self, n=1): - super().update(n) + displayed = super().update(n) for id in list(self.callbacks): callback = self.callbacks.get(id) if callback: callback(self.format_dict) + return displayed def subscribe(self, callback: callable):