Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add child class for single callable (restored) #47

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ dependencies = [
[project.optional-dependencies]
test = [
"pytest",
"pytest-asyncio",
"pytest-cov"
]

Expand Down
3 changes: 2 additions & 1 deletion src/tqdm_publisher/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from ._publisher import TQDMPublisher
from ._subscriber import TQDMProgressSubscriber

__all__ = ["TQDMPublisher"]
__all__ = ["TQDMPublisher", "TQDMProgressSubscriber"]
7 changes: 7 additions & 0 deletions src/tqdm_publisher/_subscriber.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from ._publisher import TQDMPublisher


class TQDMProgressSubscriber(TQDMPublisher):
def __init__(self, iterable, on_progress_update: callable, **tqdm_kwargs):
super().__init__(iterable, **tqdm_kwargs)
self.subscribe(lambda format_dict: on_progress_update(dict(progress_bar_id=self.id, format_dict=format_dict)))
18 changes: 0 additions & 18 deletions src/tqdm_publisher/testing.py

This file was deleted.

19 changes: 8 additions & 11 deletions tests/test_basic.py → tests/test_publisher.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import asyncio

import pytest
import time

from tqdm_publisher import TQDMPublisher
from tqdm_publisher.testing import create_tasks


def test_initialization():
Expand All @@ -12,8 +9,7 @@ def test_initialization():


# Test concurrent callback execution
@pytest.mark.asyncio
async def test_subscription_and_callback_execution():
def test_subscription_and_callback_execution():
n_callback_executions = dict()

def test_callback(identifier, data):
Expand All @@ -26,8 +22,9 @@ def test_callback(identifier, data):

assert "n" in data and "total" in data

tasks = create_tasks()
publisher = TQDMPublisher(asyncio.as_completed(tasks), total=len(tasks))
all_task_durations_in_seconds = [0.1 for _ in range(10)]

publisher = TQDMPublisher(all_task_durations_in_seconds)

n_subscriptions = 10
for i in range(n_subscriptions):
Expand All @@ -37,8 +34,8 @@ def test_callback(identifier, data):
assert callback_id in publisher.callbacks

# Simulate an update to trigger the callback
for f in publisher:
await f
for duration in publisher:
time.sleep(duration)

assert len(n_callback_executions) == n_subscriptions

Expand All @@ -51,7 +48,7 @@ def dummy_callback(data):
pass

tasks = []
publisher = TQDMPublisher(asyncio.as_completed(tasks), total=len(tasks))
publisher = TQDMPublisher(tasks)
callback_id = publisher.subscribe(dummy_callback)
result = publisher.unsubscribe(callback_id)
assert result == True
Expand Down
32 changes: 32 additions & 0 deletions tests/test_subscriber.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import time

from tqdm_publisher import TQDMProgressSubscriber


def test_initialization():
publisher = TQDMProgressSubscriber([], on_progress_update=lambda data: print(data))
assert len(publisher.callbacks) == 1


# Test single callback execution
def test_single_callback_execution():
n_tasks = 100
n_callback_executions = 0

def test_callback(data):
nonlocal n_callback_executions
n_callback_executions += 1
print("CAllin")
assert "progress_bar_id" in data and "format_dict" in data
assert "n" in data["format_dict"] and "total" in data["format_dict"]

all_task_durations_in_seconds = [0.1 for _ in range(10)]
publisher = TQDMProgressSubscriber(
all_task_durations_in_seconds, on_progress_update=lambda data: test_callback(data)
)

# Simulate an update to trigger the callback
for duration in publisher:
time.sleep(duration)

assert n_callback_executions == len(all_task_durations_in_seconds)
Loading