Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add type hints to tests #41

Merged
merged 23 commits into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"tag:yaml.org,2002:python/name:pymdownx.superfences.fence_code_format"
],
"cSpell.words": [
"autouse",
"funcs",
"initargs",
"inlinehilite",
Expand Down
20 changes: 10 additions & 10 deletions atpbar/funcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@


def find_reporter() -> ProgressReporter | None:
"""returns the progress reporter
'''returns the progress reporter

This function is to be called in the main process of a
multiprocessing program. The reporter should be registered in
Expand All @@ -21,12 +21,12 @@ def find_reporter() -> ProgressReporter | None:
object
The progress reporter

"""
'''
return _machine.find_reporter()


def register_reporter(reporter: ProgressReporter) -> None:
"""registers a reporter
'''registers a reporter

This function is to be called in sub-processes of a
multiprocessing program.
Expand All @@ -42,12 +42,12 @@ def register_reporter(reporter: ProgressReporter) -> None:
-------
None

"""
'''
_machine.register_reporter(reporter)


def flush() -> None:
"""flushes progress bars
'''flushes progress bars

This function flushes all active progress bars. It returns when
the progress bars finish updating.
Expand All @@ -56,7 +56,7 @@ def flush() -> None:
-------
None

"""
'''
_machine.flush()


Expand All @@ -70,7 +70,7 @@ def flushing() -> Iterator[None]:


def disable() -> None:
"""disables progress bars
'''disables progress bars

This function needs to be called in the main process before
`atpbar()` or `find_reporter()` is used.
Expand All @@ -79,18 +79,18 @@ def disable() -> None:
-------
None

"""
'''
_machine.disable()


def shutdown() -> None:
"""shutdowns the progress bars
'''shutdowns the progress bars

Returns
-------
None

"""
'''
_machine.shutdown()


Expand Down
18 changes: 9 additions & 9 deletions atpbar/machine.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def fetch_reporter(self) -> Iterator[ProgressReporter | None]:


class State:
"""The base class of the states"""
'''The base class of the states'''

def __init__(self) -> None:
self.reporter: ProgressReporter | None = None
Expand All @@ -63,10 +63,10 @@ def shutdown(self) -> 'State':


class Initial(State):
"""Initial state
'''Initial state

The pickup is not running.
"""
'''

def __init__(self) -> None:
self.reporter = None
Expand All @@ -82,10 +82,10 @@ def flush(self) -> State:


class Active(State):
"""Active state
'''Active state

The pickup started and is running, typically, in the main process
"""
'''

def __init__(self) -> None:

Expand Down Expand Up @@ -166,11 +166,11 @@ def shutdown(self) -> State:


class Registered(State):
"""Registered state
'''Registered state

Typically, in a sub-process. The reporter, which has been created
in the main process, is registered in the sub-process
"""
'''

def __init__(self, reporter: ProgressReporter | None) -> None:
self.reporter = reporter
Expand All @@ -189,12 +189,12 @@ def fetch_reporter(self, lock: Lock) -> Iterator[ProgressReporter | None]:


class Disabled(State):
"""Disabled state"""
'''Disabled state'''

def __init__(self) -> None:
self.reporter = None


def in_main_thread() -> bool:
"""test if in the main thread"""
'''test if in the main thread'''
return current_thread() == main_thread()
12 changes: 6 additions & 6 deletions atpbar/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@


def atpbar(iterable: Iterable[T], /, name: Optional[str] = None) -> Iterable[T]:
"""returns an instance of `Atpbar`
'''returns an instance of `Atpbar`

Parameters
----------
Expand All @@ -26,7 +26,7 @@ def atpbar(iterable: Iterable[T], /, name: Optional[str] = None) -> Iterable[T]:
An instance of `Atpbar` if successfully instantiated.
Otherwise, the object received as the parameter `iterable`.

"""
'''
try:
len_ = len(iterable) # type: ignore
except TypeError:
Expand All @@ -42,7 +42,7 @@ def atpbar(iterable: Iterable[T], /, name: Optional[str] = None) -> Iterable[T]:


class Atpbar(Generic[T]):
"""Progress bar
'''Progress bar

An iterable that wraps another iterable and shows the progress
bars for the iterations. The class is usually instantiated by the
Expand All @@ -57,7 +57,7 @@ class Atpbar(Generic[T]):
len_ : int
The length of the iterable

"""
'''

def __init__(self, iterable: Iterable[T], name: str, len_: int):
self.iterable = iterable
Expand Down Expand Up @@ -98,13 +98,13 @@ def _report_progress(self, i: int) -> None:

@contextlib.contextmanager
def report_last(pbar: Atpbar[T]) -> Iterator[None]:
"""send a last report
'''send a last report

This function sends the last report of the task when the loop ends
with `break` or an exception so that the progress bar will be
updated with the last complete iteration.

"""
'''
try:
yield
finally:
Expand Down
18 changes: 9 additions & 9 deletions atpbar/presentation/bartty.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@


class ProgressBar(Presentation):
stdout_stderr_redrection = True
stdout_stderr_redirection = True

def __init__(self) -> None:
super().__init__()
self.interval = 0.1 # [second]
self.width = self._get_width()

self.active_bars = list[str]()
self.just_finised_bars = list[str]()
self.just_finished_bars = list[str]()

def __repr__(self) -> str:
return "{}()".format(self.__class__.__name__)
Expand All @@ -30,9 +30,9 @@ def _get_width(self) -> int:

def _present(self) -> None:
self._erase_active_bars()
self._compose_just_finised_bars()
self._compose_just_finished_bars()
self._compose_active_bars()
self._draw_just_finised_bars()
self._draw_just_finished_bars()
self._draw_active_bars()

def _write(self, s: str, out: TextIO) -> None:
Expand All @@ -59,8 +59,8 @@ def _erase_active_bars(self) -> None:
self.out.write(code)
self.out.flush()

def _compose_just_finised_bars(self) -> None:
self.just_finised_bars = [
def _compose_just_finished_bars(self) -> None:
self.just_finished_bars = [
self._compose_bar_from_taskid(i) for i in self._finishing_taskids
]

Expand Down Expand Up @@ -101,9 +101,9 @@ def _compose_bar_from_report(self, report: Report) -> str:

return ret

def _draw_just_finised_bars(self) -> None:
if self.just_finised_bars:
self.out.write("\n".join(self.just_finised_bars) + "\n")
def _draw_just_finished_bars(self) -> None:
if self.just_finished_bars:
self.out.write("\n".join(self.just_finished_bars) + "\n")
self.out.flush()

def _draw_active_bars(self) -> None:
Expand Down
6 changes: 3 additions & 3 deletions atpbar/presentation/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@


class Presentation(ABC):
"""A base class of the progress presentation.
'''A base class of the progress presentation.

A subclass of this class should implement ``_present()``.
"""
'''

stdout_stderr_redrection = False
stdout_stderr_redirection = False

def __init__(self) -> None:

Expand Down
4 changes: 2 additions & 2 deletions atpbar/presentation/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@


def create_presentation() -> Presentation:
"""Create a presentation of progress report, e.g., progress bars
'''Create a presentation of progress report, e.g., progress bars

Returns
-------
Expand All @@ -21,7 +21,7 @@ def create_presentation() -> Presentation:
an instance of ProgressBarJupyter if on Jupyter Notebook
an instance of ProgressPrint otherwise

"""
'''

if sys.stdout.isatty():
return ProgressBar()
Expand Down
4 changes: 2 additions & 2 deletions atpbar/presentation/detect/jupy.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@


def is_jupyter_notebook() -> bool:
"""Tests if on Jupyter Notebook
'''Tests if on Jupyter Notebook

Returns
-------
bool
True if on Jupyter Notebook

"""
'''

if widgets is None:
return False
Expand Down
4 changes: 2 additions & 2 deletions atpbar/presentation/detect/spy.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@


def is_spyder_ide() -> bool:
"""Tests if on Spyder IDE
'''Tests if on Spyder IDE

Returns
-------
bool
True if on Spyder IDE

"""
'''

if spyder is None:
return False
Expand Down
4 changes: 2 additions & 2 deletions atpbar/progress_report/complement.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@


class ProgressReportComplementer:
"""Complement progress reports
'''Complement progress reports

Complement a progress report with the previous report for the same
task.
Expand All @@ -16,7 +16,7 @@ class ProgressReportComplementer:
include `done`, `total`, and 'name'. The `first` and `last` will be
automatically determined if not given.

"""
'''

def __init__(self) -> None:
self.previous_reports = dict[UUID, Report]()
Expand Down
10 changes: 5 additions & 5 deletions atpbar/progress_report/pickup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@


class ProgressReportPickup(threading.Thread):
"""A pickup of progress reports.
'''A pickup of progress reports.

This class picks up progress reports and presents them.

Expand All @@ -20,7 +20,7 @@ class ProgressReportPickup(threading.Thread):
The queue through which this class receives progress reports
presentation :
The presentation of the reports
"""
'''

def __init__(self, queue: 'Queue[Report]', presentation: 'Presentation') -> None:
super().__init__(daemon=True)
Expand All @@ -34,7 +34,7 @@ def __init__(self, queue: 'Queue[Report]', presentation: 'Presentation') -> None
self.start()

def end(self) -> None:
"""end the thread"""
'''end the thread'''
self.queue.put(None) # type: ignore
self.join()

Expand Down Expand Up @@ -76,10 +76,10 @@ def _process_report(self, report: Report) -> None:
self.presentation.present(report)

def _short_sleep(self) -> None:
"""sleep very briefly
'''sleep very briefly

used to prevent the empty `while` loop from increasing CPU
loads

"""
'''
time.sleep(0.001)
Loading
Loading