Skip to content

Commit

Permalink
minor progress_bar changes
Browse files Browse the repository at this point in the history
  • Loading branch information
dmichaels-harvard committed Apr 20, 2024
1 parent b0dbda2 commit 8075ca3
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
18 changes: 13 additions & 5 deletions dcicutils/progress_bar.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,10 +279,10 @@ def _define_tidy_output_hack(self) -> None:
# string in the display string where the progress bar should actually go,
# which we do in _format_description. Other minor things too; see below.
sys_stdout_write = sys.stdout.write
last_text = None ; last_captured_output_text = None # noqa
last_text = None ; last_captured_output_text = None ; last_spin_change_time = None # noqa
def tidy_stdout_write(text: str) -> None: # noqa
nonlocal self, sys_stdout_write, sentinel_internal, spina, spini, spinn
nonlocal last_text, last_captured_output_text
nonlocal last_text, last_captured_output_text, last_spin_change_time
def replace_first(value: str, match: str, replacement: str) -> str: # noqa
return value[:i] + replacement + value[i + len(match):] if (i := value.find(match)) >= 0 else value
def remove_extra_trailing_spaces(text: str) -> str: # noqa
Expand All @@ -292,13 +292,17 @@ def remove_extra_trailing_spaces(text: str) -> str: # noqa
if (not text) or (last_text == text):
return
last_text = text
now = time.time()
if (self._disabled or self._done) and sentinel_internal in text:
# Another hack to really disable output on interrupt; in this case we set
# tqdm.disable to True, but output can still dribble out, so if the output
# looks like it is from tqdm and we are disabled/done then do no output.
return
if sentinel_internal in text:
spinc = spina[spini % spinn] if not ("100%|" in text) else "✓" ; spini += 1 # noqa
spinc = spina[spini % spinn] if not ("100%|" in text) else "✓"
if last_spin_change_time is None or ((now - last_spin_change_time) >= 0.05):
spini += 1
last_spin_change_time = now
text = replace_first(text, sentinel_internal, f" {spinc}")
text = replace_first(text, "%|", "% ◀|")
text = remove_extra_trailing_spaces(text) + f"{spinc} "
Expand All @@ -307,7 +311,7 @@ def remove_extra_trailing_spaces(text: str) -> str: # noqa
# the unit we gave, which is empty; idunno; just replace it here.
text = replace_first(text, "s/ ", "/s ")
if self._use_byte_size_for_rate and self._bar:
rate = self._bar.n / (time.time() - self._started)
rate = self._bar.n / (now - self._started)
text = text.replace("[rate]", f"{format_size(rate)}/s")
sys_stdout_write(text)
sys.stdout.flush()
Expand Down Expand Up @@ -348,7 +352,11 @@ def ascii_spinners() -> list: # noqa
spinner_chars_a = "⣾⣽⣻⢿⡿⣟⣯⣷"
spinner_chars_b = "|/—\\"
spinner_chars_c = "◰◳◲◱"
return (list(spinner_chars_a[::-1]) * 9) + (list(spinner_chars_b) * 3) + (list(spinner_chars_c) * 3)
spinner_chars_d = "◐◓◑◒"
spinner_chars_e = "◴◷◶◵"
return ((list(spinner_chars_a[::-1]) * 18) +
(list(spinner_chars_b) * 3) + (list(spinner_chars_c) * 3) +
(list(spinner_chars_d) * 3) + (list(spinner_chars_e) * 3))
sys.stdout.write = tidy_stdout_write
spina = ascii_spinners() ; spini = 0 ; spinn = len(spina) # noqa
sentinel = "[progress]" ; sentinel_internal = f"{sentinel}:" # noqa
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "dcicutils"
version = "8.8.3.1b23" # TODO: To become 8.8.4
version = "8.8.3.1b24" # TODO: To become 8.8.4
description = "Utility package for interacting with the 4DN Data Portal and other 4DN resources"
authors = ["4DN-DCIC Team <[email protected]>"]
license = "MIT"
Expand Down

0 comments on commit 8075ca3

Please sign in to comment.