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

Ensure that stderr/stdout has flush attr before calling it (Sourcery refactored) #1249

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
55 changes: 29 additions & 26 deletions tqdm/std.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,8 +336,8 @@ def status_printer(file):
fp = file
fp_flush = getattr(fp, 'flush', lambda: None) # pragma: no cover
if fp in (sys.stderr, sys.stdout):
sys.stderr.flush()
sys.stdout.flush()
getattr(sys.stderr, 'flush', lambda: None)()
getattr(sys.stdout, 'flush', lambda: None)()

def fp_write(s):
fp.write(_unicode(s))
Expand Down Expand Up @@ -457,7 +457,7 @@ def format_meter(n, total, elapsed, ncols=None, prefix='', ascii=False, unit='it
total_fmt = str(total) if total is not None else '?'

try:
postfix = ', ' + postfix if postfix else ''
postfix = f', {postfix}' if postfix else ''
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function tqdm.format_meter refactored with the following changes:

except TypeError:
pass

Expand All @@ -473,7 +473,7 @@ def format_meter(n, total, elapsed, ncols=None, prefix='', ascii=False, unit='it
if prefix:
# old prefix setup work around
bool_prefix_colon_already = (prefix[-2:] == ": ")
l_bar = prefix if bool_prefix_colon_already else prefix + ": "
l_bar = prefix if bool_prefix_colon_already else f'{prefix}: '
else:
l_bar = ''

Expand Down Expand Up @@ -600,12 +600,12 @@ def _decr_instances(cls, instance):
# else:
if not instance.gui:
last = (instance.nrows or 20) - 1
# find unfixed (`pos >= 0`) overflow (`pos >= nrows - 1`)
instances = list(filter(
lambda i: hasattr(i, "pos") and last <= i.pos,
cls._instances))
# set first found to current `pos`
if instances:
if instances := list(
filter(
lambda i: hasattr(i, "pos") and last <= i.pos,
cls._instances,
)
):
Comment on lines -603 to +608
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function tqdm._decr_instances refactored with the following changes:

This removes the following comments ( why? ):

# set first found to current `pos`
# find unfixed (`pos >= 0`) overflow (`pos >= nrows - 1`)

inst = min(instances, key=lambda i: i.pos)
inst.clear(nolock=True)
inst.pos = abs(instance.pos)
Expand Down Expand Up @@ -1020,20 +1020,22 @@ def __init__(self, iterable=None, desc=None, total=None, leave=True, file=None,
# Preprocess the arguments
if (
(ncols is None or nrows is None) and (file in (sys.stderr, sys.stdout))
) or dynamic_ncols: # pragma: no cover
): # pragma: no cover
if dynamic_ncols:
dynamic_ncols = _screen_shape_wrapper()
if dynamic_ncols:
ncols, nrows = dynamic_ncols(file)
else:
_dynamic_ncols = _screen_shape_wrapper()
if _dynamic_ncols:
_ncols, _nrows = _dynamic_ncols(file)
if ncols is None:
ncols = _ncols
if nrows is None:
nrows = _nrows

elif _dynamic_ncols := _screen_shape_wrapper():
_ncols, _nrows = _dynamic_ncols(file)
if ncols is None:
ncols = _ncols
if nrows is None:
nrows = _nrows

elif dynamic_ncols: # pragma: no cover
dynamic_ncols = _screen_shape_wrapper()
if dynamic_ncols:
ncols, nrows = dynamic_ncols(file)
Comment on lines -1023 to +1038
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function tqdm.__init__ refactored with the following changes:

This removes the following comments ( why? ):

# pragma: no cover

if miniters is None:
miniters = 0
dynamic_miniters = True
Expand Down Expand Up @@ -1165,8 +1167,7 @@ def __iter__(self):
# If the bar is disabled, then just walk the iterable
# (note: keep this check outside the loop for performance)
if self.disable:
for obj in iterable:
yield obj
yield from iterable
Comment on lines -1168 to +1170
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function tqdm.__iter__ refactored with the following changes:

  • Replace yield inside for loop with yield from (yield-from)

return

mininterval = self.mininterval
Expand Down Expand Up @@ -1388,7 +1389,7 @@ def set_description(self, desc=None, refresh=True):
refresh : bool, optional
Forces refresh [default: True].
"""
self.desc = desc + ': ' if desc else ''
self.desc = f'{desc}: ' if desc else ''
Comment on lines -1391 to +1392
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function tqdm.set_description refactored with the following changes:

if refresh:
self.refresh()

Expand Down Expand Up @@ -1424,8 +1425,10 @@ def set_postfix(self, ordered_dict=None, refresh=True, **kwargs):
postfix[key] = str(postfix[key])
# Else if it's a string, don't need to preprocess anything
# Stitch together to get the final postfix
self.postfix = ', '.join(key + '=' + postfix[key].strip()
for key in postfix.keys())
self.postfix = ', '.join(
f'{key}=' + postfix[key].strip() for key in postfix.keys()
)

Comment on lines -1427 to +1431
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function tqdm.set_postfix refactored with the following changes:

if refresh:
self.refresh()

Expand All @@ -1440,7 +1443,7 @@ def set_postfix_str(self, s='', refresh=True):
def moveto(self, n):
# TODO: private method
self.fp.write(_unicode('\n' * n + _term_move_up() * -n))
self.fp.flush()
getattr(self.fp, 'flush', lambda: None)()

@property
def format_dict(self):
Expand Down