Convert tqdm function to subclass

This commit is contained in:
Joakim Soderlund 2023-11-22 17:25:28 +01:00
parent 129a3440e6
commit f28b10c63a
2 changed files with 32 additions and 11 deletions

View file

@ -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)

View file

@ -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)