From f28b10c63aaa0df662c5a68f7a04d08615b62cfd Mon Sep 17 00:00:00 2001 From: Joakim Soderlund Date: Wed, 22 Nov 2023 17:25:28 +0100 Subject: [PATCH] Convert tqdm function to subclass --- fimfarchive/utils.py | 23 +++++++++++++---------- tests/test_utils.py | 20 +++++++++++++++++++- 2 files changed, 32 insertions(+), 11 deletions(-) diff --git a/fimfarchive/utils.py b/fimfarchive/utils.py index 3138fb9..57f57c0 100644 --- a/fimfarchive/utils.py +++ b/fimfarchive/utils.py @@ -29,7 +29,7 @@ from importlib import import_module from importlib_resources import files from pathlib import Path from typing import ( - cast, Any, Callable, Dict, Iterable, Iterator, + cast, Any, Callable, Dict, Iterator, Optional, Set, Tuple, Type, TypeVar, Union, ) @@ -302,15 +302,18 @@ class ResourceLoader: return resource.read_text() -def tqdm(iterable: Iterable[T], **kwargs) -> Iterable[T]: +class tqdm(_tqdm): """ Adds an ASCII progress bar to the iterable. """ - return _tqdm( - iterable, - ascii=True, - leave=False, - smoothing=0, - ncols=72, - **kwargs, - ) + + defaults = { + 'ascii': True, + 'leave': False, + 'smoothing': 0.0, + 'ncols': 72, + } + + def __init__(self, *args, **kwargs): + kwargs = {**self.defaults, **kwargs} + return super().__init__(*args, **kwargs) diff --git a/tests/test_utils.py b/tests/test_utils.py index 1937726..e9ab258 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -33,7 +33,7 @@ from fimfarchive import utils from fimfarchive.flavors import DataFormat, MetaFormat, MetaPurity from fimfarchive.utils import ( find_flavor, get_path, is_blacklisted, - Empty, JayWalker, PersistedDict, + Empty, JayWalker, PersistedDict, tqdm, ) @@ -366,3 +366,21 @@ class TestIsBlacklisted: story = story.merge(key=key, meta=meta) assert result is is_blacklisted(story) + + +class TestTqdm: + """ + tqdm tests. + """ + + def test_overridden_default(self): + """ + Tests defaults can be overridden. + """ + tqdm((), disable=True, smoothing=1.0) + + def test_without_iterator(self): + """ + Tests iterator is not required. + """ + tqdm(disable=True, total=42)