Skip to content

Commit

Permalink
Merge pull request #42 from alphatwirl/dev
Browse files Browse the repository at this point in the history
Fix type, linter, and spell checker errors
  • Loading branch information
TaiSakuma authored May 26, 2024
2 parents 1e8e9db + 944f098 commit e8c8d69
Show file tree
Hide file tree
Showing 12 changed files with 161 additions and 158 deletions.
2 changes: 2 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@
],
"cSpell.words": [
"autouse",
"capsys",
"funcs",
"initargs",
"inlinehilite",
"ipywidgets",
"linenums",
"pbar",
"pygments",
Expand Down
11 changes: 6 additions & 5 deletions atpbar/presentation/barjupyter.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def __init__(self) -> None:
def __repr__(self) -> str:
return "{}()".format(self.__class__.__name__)

def _present(self) -> None:
def _present(self, report: Report) -> None:
self._create_widgets()
self._update_widgets()

Expand All @@ -30,7 +30,7 @@ def _create_widgets(self) -> None:
self.container_widget = widgets.VBox()
display(self.container_widget)

for taskid in self._new_taskids:
for taskid in self._new_task_ids:
report = self._report_dict[taskid]
self._create_widget(report)

Expand All @@ -54,14 +54,14 @@ def _create_widget(self, report: Report) -> None:

def _update_widgets(self) -> None:
for taskid in (
self._finishing_taskids + self._active_taskids + self._new_taskids
self._finishing_task_ids + self._active_task_ids + self._new_task_ids
):
report = self._report_dict[taskid]
self._update_widget(report)

self._reorder_widgets(report)

if not self._new_taskids and not self._active_taskids:
if not self._new_task_ids and not self._active_task_ids:
self.container_widget = None
self.active_box_list[:] = []
self.complete_box_list[:] = []
Expand All @@ -74,6 +74,7 @@ def _update_widget(self, report: Report) -> None:
percent_fmt = "{:6.2f}%".format(percent)

box = self.widget_dict[report["taskid"]][0]
box # to silence not-used warning

bar = self.widget_dict[report["taskid"]][1]
bar.value = report["done"]
Expand All @@ -93,7 +94,7 @@ def _update_widget(self, report: Report) -> None:
)

def _reorder_widgets(self, report: Report) -> None:
for taskid in self._finishing_taskids:
for taskid in self._finishing_task_ids:
box, bar, label = self.widget_dict[taskid]
if box in self.active_box_list:
self.active_box_list.remove(box)
Expand Down
12 changes: 6 additions & 6 deletions atpbar/presentation/bartty.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def _get_width(self) -> int:
except AttributeError:
return MINIMUM_TERMINAL_WIDTH

def _present(self) -> None:
def _present(self, report: Report) -> None:
self._erase_active_bars()
self._compose_just_finished_bars()
self._compose_active_bars()
Expand All @@ -45,13 +45,13 @@ def _write(self, s: str, out: TextIO) -> None:
self._draw_active_bars()

def _erase_active_bars(self) -> None:
nlines = len(self._active_taskids) + len(self._finishing_taskids)
n_lines = len(self._active_task_ids) + len(self._finishing_task_ids)
# must be the same as len(self.active_bars)

if nlines == 0:
if n_lines == 0:
return

code = "\033[1G" + "\033[A" * (nlines - 1) + "\033[0J"
code = "\033[1G" + "\033[A" * (n_lines - 1) + "\033[0J"
# '\033[1G' move the cursor to the beginning of the line
# '\033[A' move the cursor up
# '\033[0J' clear from cursor to end of screen
Expand All @@ -61,13 +61,13 @@ def _erase_active_bars(self) -> None:

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

def _compose_active_bars(self) -> None:
self.active_bars = [
self._compose_bar_from_taskid(i)
for i in self._active_taskids + self._new_taskids
for i in self._active_task_ids + self._new_task_ids
]

def _compose_bar_from_taskid(self, taskid: UUID) -> str:
Expand Down
42 changes: 21 additions & 21 deletions atpbar/presentation/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,17 @@ def __init__(self) -> None:

self.lock = threading.Lock()

self._new_taskids = list[UUID]()
self._active_taskids = list[UUID]() # in order of arrival
self._finishing_taskids = list[UUID]()
self._complete_taskids = list[UUID]() # in order of completion
self._new_task_ids = list[UUID]()
self._active_task_ids = list[UUID]() # in order of arrival
self._finishing_task_ids = list[UUID]()
self._complete_task_ids = list[UUID]() # in order of completion
self._report_dict = dict[UUID, Report]()

self.interval = 1.0 # [second]
self.last_time = time.time()

def active(self) -> bool:
if self._active_taskids:
if self._active_task_ids:
return True
return False

Expand All @@ -43,63 +43,63 @@ def present(self, report: Report) -> None:
return
if not self._need_to_present():
return
self._present()
self._present(report)
self._update_registry()
self.last_time = time.time()

@abstractmethod
def _present(self) -> None:
def _present(self, report: Report) -> None:
pass

def _register_report(self, report: Report) -> bool:

taskid = report["taskid"]

if taskid in self._complete_taskids:
if taskid in self._complete_task_ids:
return False

self._report_dict[taskid] = report

if taskid in self._finishing_taskids:
if taskid in self._finishing_task_ids:
return True

if report["last"]:
try:
self._active_taskids.remove(taskid)
self._active_task_ids.remove(taskid)
except ValueError:
pass

try:
self._new_taskids.remove(taskid)
self._new_task_ids.remove(taskid)
except ValueError:
pass

self._finishing_taskids.append(taskid)
self._finishing_task_ids.append(taskid)

return True

if taskid in self._active_taskids:
if taskid in self._active_task_ids:
return True

if taskid in self._new_taskids:
if taskid in self._new_task_ids:
return True

self._new_taskids.append(taskid)
self._new_task_ids.append(taskid)
return True

def _update_registry(self) -> None:
self._active_taskids.extend(self._new_taskids)
del self._new_taskids[:]
self._active_task_ids.extend(self._new_task_ids)
del self._new_task_ids[:]

self._complete_taskids.extend(self._finishing_taskids)
del self._finishing_taskids[:]
self._complete_task_ids.extend(self._finishing_task_ids)
del self._finishing_task_ids[:]

def _need_to_present(self) -> bool:

if self._new_taskids:
if self._new_task_ids:
return True

if self._finishing_taskids:
if self._finishing_task_ids:
return True

if time.time() - self.last_time > self.interval:
Expand Down
7 changes: 2 additions & 5 deletions atpbar/presentation/txtprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,11 @@ def present(self, report: Report) -> None:
if not self._need_to_present_(report):
return

self._present_(report)
self._present(report)

self.last_time_map[report["taskid"]] = self._time()

def _present(self) -> None:
pass

def _present_(self, report: Report) -> None:
def _present(self, report: Report) -> None:
time_ = time.strftime("%m/%d %H:%M", time.localtime(time.time()))
percent = float(report["done"]) / report["total"] if report["total"] > 0 else 1
percent = round(percent * 100, 2)
Expand Down
93 changes: 47 additions & 46 deletions tests/presentation/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
import pytest

from atpbar.presentation.base import Presentation
from atpbar.progress_report import Report


class MockProgressBar(Presentation):
def _present(self) -> None:
def _present(self, report: Report) -> None:
pass


Expand Down Expand Up @@ -63,10 +64,10 @@ def test_present(obj: Presentation) -> None:
]
param_names = (
"report, "
"initial_new_taskids, initial_active_taskids, "
"initial_finishing_taskids, initial_complete_taskids, "
"expected_new_taskids, expected_active_taskids, "
"expected_finishing_taskids, expected_complete_taskids, "
"initial_new_task_ids, initial_active_task_ids, "
"initial_finishing_task_ids, initial_complete_task_ids, "
"expected_new_task_ids, expected_active_task_ids, "
"expected_finishing_task_ids, expected_complete_task_ids, "
"expected_return"
)

Expand All @@ -75,28 +76,28 @@ def test_present(obj: Presentation) -> None:
def test_register_report( # type: ignore
obj,
report,
initial_new_taskids,
initial_active_taskids,
initial_finishing_taskids,
initial_complete_taskids,
expected_new_taskids,
expected_active_taskids,
expected_finishing_taskids,
expected_complete_taskids,
initial_new_task_ids,
initial_active_task_ids,
initial_finishing_task_ids,
initial_complete_task_ids,
expected_new_task_ids,
expected_active_task_ids,
expected_finishing_task_ids,
expected_complete_task_ids,
expected_return,
):

obj._new_taskids[:] = initial_new_taskids
obj._active_taskids[:] = initial_active_taskids
obj._finishing_taskids[:] = initial_finishing_taskids
obj._complete_taskids[:] = initial_complete_taskids
obj._new_task_ids[:] = initial_new_task_ids
obj._active_task_ids[:] = initial_active_task_ids
obj._finishing_task_ids[:] = initial_finishing_task_ids
obj._complete_task_ids[:] = initial_complete_task_ids

assert expected_return == obj._register_report(report)

assert expected_new_taskids == obj._new_taskids
assert expected_active_taskids == obj._active_taskids
assert expected_finishing_taskids == obj._finishing_taskids
assert expected_complete_taskids == obj._complete_taskids
assert expected_new_task_ids == obj._new_task_ids
assert expected_active_task_ids == obj._active_task_ids
assert expected_finishing_task_ids == obj._finishing_task_ids
assert expected_complete_task_ids == obj._complete_task_ids


params = [
Expand Down Expand Up @@ -132,37 +133,37 @@ def test_register_report( # type: ignore
),
]
param_names = (
"initial_new_taskids, initial_active_taskids, "
"initial_finishing_taskids, initial_complete_taskids, "
"expected_new_taskids, expected_active_taskids, "
"expected_finishing_taskids, expected_complete_taskids, "
"initial_new_task_ids, initial_active_task_ids, "
"initial_finishing_task_ids, initial_complete_task_ids, "
"expected_new_task_ids, expected_active_task_ids, "
"expected_finishing_task_ids, expected_complete_task_ids, "
)


@pytest.mark.parametrize(param_names, params)
def test_update_registry( # type: ignore
obj,
initial_new_taskids,
initial_active_taskids,
initial_finishing_taskids,
initial_complete_taskids,
expected_new_taskids,
expected_active_taskids,
expected_finishing_taskids,
expected_complete_taskids,
initial_new_task_ids,
initial_active_task_ids,
initial_finishing_task_ids,
initial_complete_task_ids,
expected_new_task_ids,
expected_active_task_ids,
expected_finishing_task_ids,
expected_complete_task_ids,
):

obj._new_taskids[:] = initial_new_taskids
obj._active_taskids[:] = initial_active_taskids
obj._finishing_taskids[:] = initial_finishing_taskids
obj._complete_taskids[:] = initial_complete_taskids
obj._new_task_ids[:] = initial_new_task_ids
obj._active_task_ids[:] = initial_active_task_ids
obj._finishing_task_ids[:] = initial_finishing_task_ids
obj._complete_task_ids[:] = initial_complete_task_ids

obj._update_registry()

assert expected_new_taskids == obj._new_taskids
assert expected_active_taskids == obj._active_taskids
assert expected_finishing_taskids == obj._finishing_taskids
assert expected_complete_taskids == obj._complete_taskids
assert expected_new_task_ids == obj._new_task_ids
assert expected_active_task_ids == obj._active_task_ids
assert expected_finishing_task_ids == obj._finishing_task_ids
assert expected_complete_task_ids == obj._complete_task_ids


params = [
Expand All @@ -172,24 +173,24 @@ def test_update_registry( # type: ignore
pytest.param([], [], 4.0, 2.0, 3.0, False),
]
param_names = (
"new_taskids, finishing_taskids, " "current_time, last_time, interval, expected"
"new_task_ids, finishing_task_ids, " "current_time, last_time, interval, expected"
)


@pytest.mark.parametrize(param_names, params)
def test_need_to_present( # type: ignore
obj,
mock_time,
new_taskids,
finishing_taskids,
new_task_ids,
finishing_task_ids,
current_time,
last_time,
interval,
expected,
):

obj._new_taskids[:] = new_taskids
obj._finishing_taskids[:] = finishing_taskids
obj._new_task_ids[:] = new_task_ids
obj._finishing_task_ids[:] = finishing_task_ids

mock_time.time.return_value = current_time
obj.last_time = last_time
Expand Down
Loading

0 comments on commit e8c8d69

Please sign in to comment.