Skip to content

Commit

Permalink
new-style docstrings
Browse files Browse the repository at this point in the history
  • Loading branch information
casperdcl committed Jan 29, 2024
1 parent 5d6c537 commit 6e44e46
Showing 1 changed file with 38 additions and 13 deletions.
51 changes: 38 additions & 13 deletions Wrappers/Python/cil/utilities/callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,14 @@


class Callback(ABC):
'''Base Callback to inherit from for use in :code:`Algorithm.run(callbacks: list[Callback])`.'''
'''Base Callback to inherit from for use in :code:`Algorithm.run(callbacks: list[Callback])`.
Parameters
----------
verbose: int, choice of 0,1,2, default 1
0=quiet, 1=info, 2=debug.
'''
def __init__(self, verbose=1):
''':param verbose: int, 0=quiet, 1=info, 2=debug'''
self.verbose = verbose

@abstractmethod
Expand All @@ -30,11 +35,15 @@ def __call__(self, algorithm):


class ProgressCallback(Callback):
''':code:`tqdm`-based progress bar.'''
''':code:`tqdm`-based progress bar.
Parameters
----------
tqdm_class: default :code:`tqdm.auto.tqdm`
**tqdm_kwargs:
Passed to :code:`tqdm_class`.
'''
def __init__(self, verbose=1, tqdm_class=tqdm_auto, **tqdm_kwargs):
'''
:param tqdm_kwargs: passed to :code:`tqdm_class`.
'''
super().__init__(verbose=verbose)
self.tqdm_class = tqdm_class
self.tqdm_kwargs = tqdm_kwargs
Expand All @@ -50,6 +59,13 @@ def __call__(self, algorithm):


class _TqdmText(tqdm_std):
''':code:`tqdm`-based progress but text-only updates on separate lines.
Parameters
----------
miniterval: float, default 5
Approximate number of seconds between updates.
'''
def __init__(self, *args, mininterval=5, bar_format="{l_bar}{r_bar}", position=0, **kwargs):
super().__init__(*args, mininterval=mininterval, bar_format=bar_format, position=position, **kwargs)
self._instances.remove(self) # don't interfere with external progress bars
Expand All @@ -66,18 +82,27 @@ def fp_write(s):


class TextProgressCallback(ProgressCallback):
''':code:`ProgressCallback` but printed on separate lines to screen.'''
''':code:`ProgressCallback` but printed on separate lines to screen.
Parameters
----------
miniterval: float, default 5
Approximate number of seconds between updates.
'''
def __init__(self, tqdm_class=_TqdmText, **kwargs):
""":param miniterval: approximate number of seconds between updates."""
super().__init__(tqdm_class=tqdm_class, **kwargs)


class LogfileCallback(TextProgressCallback):
''':code:`TextProgressCallback` but to a file instead of screen.'''
''':code:`TextProgressCallback` but to a file instead of screen.
Parameters
----------
log_file: FileDescriptorOrPath
Passed to :code:`open()`.
mode: str
Passed to :code:`open()`.
'''
def __init__(self, log_file, mode='a', **kwargs):
"""
:param log_file: passed to :code:`open()`.
:param mode: passed to :code:`open()`.
"""
self.fd = open(log_file, mode=mode)
super().__init__(file=self.fd, **kwargs)

0 comments on commit 6e44e46

Please sign in to comment.