From d3be02bc991c5036f39a12c1d3c7bf3a3bf0c184 Mon Sep 17 00:00:00 2001 From: Casper da Costa-Luis Date: Sun, 12 Jul 2020 01:14:15 +0100 Subject: [PATCH] add `asyncio.tarange` --- tqdm/asyncio.py | 10 +++++++++- tqdm/tests/tests_async.py | 5 +++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/tqdm/asyncio.py b/tqdm/asyncio.py index 1d4e42c7..b132be19 100644 --- a/tqdm/asyncio.py +++ b/tqdm/asyncio.py @@ -1,6 +1,6 @@ from .auto import tqdm as tqdm_auto __author__ = {"github.com/": ["casperdcl"]} -__all__ = ['tqdm_asyncio', 'tqdm'] +__all__ = ['tqdm_asyncio', 'tarange', 'tqdm', 'trange'] class tqdm_asyncio(tqdm_auto): @@ -39,5 +39,13 @@ class tqdm_asyncio(tqdm_auto): raise +def tarange(*args, **kwargs): + """ + A shortcut for `tqdm.asyncio.tqdm(range(*args), **kwargs)`. + """ + return tqdm_asyncio(range(*args), **kwargs) + + # Aliases tqdm = tqdm_asyncio +trange = tarange diff --git a/tqdm/tests/tests_async.py b/tqdm/tests/tests_async.py index 40417c98..af485a5a 100644 --- a/tqdm/tests/tests_async.py +++ b/tqdm/tests/tests_async.py @@ -2,7 +2,7 @@ import asyncio from functools import partial from tests_tqdm import with_setup, pretest, posttest, StringIO, closing -from tqdm.asyncio import tqdm_asyncio +from tqdm.asyncio import tqdm_asyncio, tarange def count(start=0, step=1): @@ -18,6 +18,7 @@ def count(start=0, step=1): async def main(): with closing(StringIO()) as our_file: tqdm = partial(tqdm_asyncio, file=our_file, miniters=0, mininterval=0) + trange = partial(tarange, file=our_file, miniters=0, mininterval=0) async for row in tqdm(count(), desc="counter"): if row >= 8: @@ -32,7 +33,7 @@ async def main(): our_file.seek(0) our_file.truncate() - async for row in tqdm(tqdm(range(9), desc="inner"), desc="outer"): + async for row in tqdm(trange(9, desc="inner"), desc="outer"): pass assert 'inner: 100%' in our_file.getvalue() assert 'outer: 100%' in our_file.getvalue()