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

Update Spinner API, fix #11004 #11772

Merged
merged 3 commits into from
Jun 1, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 26 additions & 10 deletions astropy/utils/console.py
Original file line number Diff line number Diff line change
Expand Up @@ -831,7 +831,7 @@ class Spinner:

with Spinner("Reticulating splines", "green") as s:
for item in enumerate(items):
s.next()
s.update()
"""
_default_unicode_chars = "◓◑◒◐"
_default_ascii_chars = "-/|\\"
Expand Down Expand Up @@ -880,6 +880,11 @@ def __init__(self, msg, color='default', file=None, step=1,

self._silent = not isatty(file)

if self._silent:
self._iter = self._silent_iterator()
else:
self._iter = self._iterator()

def _iterator(self):
chars = self._chars
index = 0
Expand Down Expand Up @@ -912,10 +917,7 @@ def _iterator(self):
index = (index + 1) % len(chars)

def __enter__(self):
if self._silent:
return self._silent_iterator()
else:
return self._iterator()
return self

def __exit__(self, exc_type, exc_value, traceback):
file = self._file
Expand All @@ -931,6 +933,24 @@ def __exit__(self, exc_type, exc_value, traceback):
color_print(' [Failed]', 'red', file=file)
flush()

def __iter__(self):
return self

def __next__(self):
next(self._iter)

def update(self, value=None):
"""Update the spin wheel in the terminal.

Parameters
----------
value : int, optional
Ignored (present just for compatibility with `ProgressBar.update`).

"""

next(self)

def _silent_iterator(self):
color_print(self._msg, self._color, file=self._file, end='')
self._file.flush()
Expand Down Expand Up @@ -997,7 +1017,6 @@ def __init__(self, total, msg, color='default', file=None):
self._obj = ProgressBar(total, file=file)

def __enter__(self):
self._iter = self._obj.__enter__()
return self

def __exit__(self, exc_type, exc_value, traceback):
Expand All @@ -1008,10 +1027,7 @@ def update(self, value):
Update the progress bar to the given value (out of the total
given to the constructor.
"""
if self._is_spinner:
next(self._iter)
else:
self._obj.update(value)
self._obj.update(value)


def print_code_line(line, col=None, file=None, tabwidth=8, width=70):
Expand Down
3 changes: 3 additions & 0 deletions docs/changes/utils/11772.api.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Updated ``utils.console.Spinner`` to better resemble the API of
``utils.console.ProgressBar``, including an ``update()`` method and
iterator support.