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

Remove the option time_track #37

Merged
merged 1 commit into from
May 4, 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
21 changes: 3 additions & 18 deletions atpbar/main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import contextlib
import logging
import time
import uuid
from collections.abc import Iterable, Iterator
from typing import Generic, Optional, TypeVar
Expand All @@ -11,12 +10,7 @@
T = TypeVar('T')


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

Parameters
Expand Down Expand Up @@ -44,7 +38,7 @@ def atpbar(
if name is None:
name = repr(iterable)

return Atpbar(iterable, name=name, len_=len_, time_track=time_track)
return Atpbar(iterable, name=name, len_=len_)


class Atpbar(Generic[T]):
Expand All @@ -65,18 +59,11 @@ class Atpbar(Generic[T]):

"""

def __init__(
self,
iterable: Iterable[T],
name: str,
len_: int,
time_track: Optional[bool] = False,
):
def __init__(self, iterable: Iterable[T], name: str, len_: int):
self.iterable = iterable
self.name = name
self.len_ = len_
self.id_ = uuid.uuid4()
self.time_track = time_track

def __iter__(self) -> Iterator[T]:
with fetch_reporter() as reporter:
Expand All @@ -95,8 +82,6 @@ def _report_start(self) -> None:
return
try:
report = Report(taskid=self.id_, name=self.name, done=0, total=self.len_)
if self.time_track:
report["start_time"] = time.time()
self.reporter.report(report)
except BaseException:
pass
Expand Down
21 changes: 3 additions & 18 deletions atpbar/presentation/barjupyter.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,24 +88,9 @@ def _update_widget(self, report: Report) -> None:
bar = (":" * int(percent * 40)).ljust(40, " ")
percent = round(percent * 100, 2)
name = report["name"][0:name_field_length]
if "start_time" in report.keys():
elapsed_str, remaining_str = self._get_time_track(
report["start_time"], percent
)
label.value = (
"<pre> | {:8d} / {:8d} ({:s} / {:s}) |: {:<{}s}</pre>".format(
report["done"],
report["total"],
elapsed_str,
remaining_str,
name,
name_field_length,
)
)
else:
label.value = "<pre> | {:8d} / {:8d} |: {:<{}s}</pre>".format(
report["done"], report["total"], name, name_field_length
)
label.value = "<pre> | {:8d} / {:8d} |: {:<{}s}</pre>".format(
report["done"], report["total"], name, name_field_length
)

def _reorder_widgets(self, report: Report) -> None:
for taskid in self._finishing_taskids:
Expand Down
6 changes: 0 additions & 6 deletions atpbar/presentation/bartty.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,6 @@ def _compose_bar_from_report(self, report: Report) -> str:

format = " {percent:6.2f}% {bar:s} | {done:8d} / {total:8d} |: {name} "

if "start_time" in report.keys():
elapsed_str, remaining_str = self._get_time_track(
report["start_time"], percent
)
format += " | [{:s} / {:s}]".format(elapsed_str, remaining_str)

ret = format.format(
percent=percent,
bar=bar,
Expand Down
21 changes: 0 additions & 21 deletions atpbar/presentation/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,27 +107,6 @@ def _need_to_present(self) -> bool:

return False

def _get_time_track(self, start_time, percent):
"""Format seconds as hours, minutes and seconds."""
time_elapsed = time.time() - start_time
time_remaining = (
(time_elapsed * (100 / percent)) - time_elapsed if percent > 0 else 0
)

return self._time_to_str(time_elapsed), self._time_to_str(time_remaining)

def _time_to_str(self, t):
mins = t // 60
s = int(t % 60)

h = int(mins // 60)
m = int(mins % 60)

if h:
return "{0:d}:{1:02d}:{2:02d}".format(h, m, s)
else:
return "{0:02d}:{1:02d}".format(m, s)

def stdout_write(self, s: str) -> None:
with self.lock:
self._stdout_write(s)
Expand Down
6 changes: 0 additions & 6 deletions tests/presentation/test_presentation.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import time

import pytest

has_jupyter_notebook = False
Expand Down Expand Up @@ -48,7 +46,6 @@ def test_presentation(Class):

@pytest.mark.parametrize("Class", classes, ids=classe_ids)
def test_time_track(Class):
start_time = time.time()
obj = Class()
repr(obj)
obj.active()
Expand All @@ -60,7 +57,6 @@ def test_time_track(Class):
taskid=1,
first=True,
last=False,
start_time=start_time,
)
)
obj.present(
Expand All @@ -71,7 +67,6 @@ def test_time_track(Class):
taskid=1,
first=False,
last=False,
start_time=start_time,
)
)
obj.present(
Expand All @@ -82,7 +77,6 @@ def test_time_track(Class):
taskid=1,
first=False,
last=True,
start_time=start_time,
)
)
obj.active()
Loading