Skip to content

Package Utils

Shared progress bar class for SEGY-SAK

Progress

Progress class tracks the shared state of all progress trackers in SEGY-SAK

Source code in segysak/progress.py
Python
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
class Progress:
    """Progress class tracks the shared state of all progress trackers in SEGY-SAK"""

    _segysak_tqdm_func = tqdm_auto
    _segysak_tqdm_kwargs = {
        "disable": False,
        "leave": False,
        "unit_scale": True,
    }

    @classmethod
    def silent(cls):
        return cls._segysak_tqdm_kwargs["disable"]

    def __init__(self, tqdm_func: Union[str, None] = None, **tqdm_kwargs):
        if tqdm_func == "a":
            self.tqdm_func = tqdm_auto
        elif tqdm_func == "t":
            self.tqdm_func = tqdm
        elif tqdm_func == "n":
            self.tqdm_func = tqdm_notebook
        else:
            self.tqdm_func = self._segysak_tqdm_func

        self.tqdm_kwargs = tqdm_kwargs

    def __enter__(self):
        kwargs = self._segysak_tqdm_kwargs.copy()
        kwargs.update(self.tqdm_kwargs)
        self.pbar = self.tqdm_func(**kwargs)
        return self.pbar

    def __exit__(self, type: Any, value: Any, traceback: Any):
        self.pbar.clear()
        self.pbar.close()

    @classmethod
    def set_defaults(cls, **tqdm_kwargs: Dict[str, Any]) -> None:
        """Set the default arguments for tqdm progress reporting in SEGY-SAK.

        The defaults are Global and affect all tqdm reporting in the active Python
        session.

        Args:
            tqdm_kwargs: Any valid [`tqdm.tqdm` argument](https://tqdm.github.io/docs/tqdm/).
        """
        valid_args = inspect.signature(tqdm.__init__).parameters
        for kwarg in tqdm_kwargs:
            try:
                assert kwarg in valid_args
            except AssertionError:
                raise AssertionError(
                    f"{kwarg} is not a valid tqdm argument -> {valid_args.keys()}"
                )
        cls._segysak_tqdm_kwargs.update(tqdm_kwargs)

set_defaults(**tqdm_kwargs) classmethod

Set the default arguments for tqdm progress reporting in SEGY-SAK.

The defaults are Global and affect all tqdm reporting in the active Python session.

Parameters:

Name Type Description Default
tqdm_kwargs Dict[str, Any]

Any valid tqdm.tqdm argument.

{}
Source code in segysak/progress.py
Python
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
@classmethod
def set_defaults(cls, **tqdm_kwargs: Dict[str, Any]) -> None:
    """Set the default arguments for tqdm progress reporting in SEGY-SAK.

    The defaults are Global and affect all tqdm reporting in the active Python
    session.

    Args:
        tqdm_kwargs: Any valid [`tqdm.tqdm` argument](https://tqdm.github.io/docs/tqdm/).
    """
    valid_args = inspect.signature(tqdm.__init__).parameters
    for kwarg in tqdm_kwargs:
        try:
            assert kwarg in valid_args
        except AssertionError:
            raise AssertionError(
                f"{kwarg} is not a valid tqdm argument -> {valid_args.keys()}"
            )
    cls._segysak_tqdm_kwargs.update(tqdm_kwargs)