Skip to content

Progress

Warning

This module is deprecated and will be removed in a future release. It implements a wrapper for the tqdm progress bar iterator for easy toggling, but this functionality is already provided by the disable argument of tqdm.

MockProgress(iterator)

Bases: Iterator

A Naive mock class to use with maybe_progress and tqdm. Mocked methods don't support return values. Mocked properties don't do anything

Source code in src/pydvl/utils/progress.py
def __init__(self, iterator: Union[Iterator, Iterable]):
    # Since there is no _it in __dict__ at this point, doing here
    # self._it = iterator
    # results in a call to __getattr__() and the assignment fails, so we
    # use __dict__ instead
    self.__dict__["_it"] = iterator

maybe_progress(it, display=False, **kwargs)

Returns either a tqdm progress bar or a mock object which wraps the iterator as well, but ignores any accesses to methods or properties.

PARAMETER DESCRIPTION
it

the iterator to wrap

TYPE: Union[int, Iterable, Iterator]

display

set to True to return a tqdm bar

TYPE: bool DEFAULT: False

kwargs

Keyword arguments that will be forwarded to tqdm

DEFAULT: {}

Source code in src/pydvl/utils/progress.py
def maybe_progress(
    it: Union[int, Iterable, Iterator], display: bool = False, **kwargs
) -> Union[tqdm, MockProgress]:
    """Returns either a tqdm progress bar or a mock object which wraps the
    iterator as well, but ignores any accesses to methods or properties.

    Args:
        it: the iterator to wrap
        display: set to True to return a tqdm bar
        kwargs: Keyword arguments that will be forwarded to tqdm
    """
    if isinstance(it, int):
        it = range(it)  # type: ignore
    return tqdm(it, **kwargs) if display else MockProgress(it)

Last update: 2023-09-02
Created: 2023-09-02