-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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)) | ||
|
@@ -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 '' | ||
except TypeError: | ||
pass | ||
|
||
|
@@ -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 = '' | ||
|
||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
This removes the following comments ( why? ):
|
||
inst = min(instances, key=lambda i: i.pos) | ||
inst.clear(nolock=True) | ||
inst.pos = abs(instance.pos) | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
This removes the following comments ( why? ):
|
||
if miniters is None: | ||
miniters = 0 | ||
dynamic_miniters = True | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
return | ||
|
||
mininterval = self.mininterval | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
if refresh: | ||
self.refresh() | ||
|
||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
if refresh: | ||
self.refresh() | ||
|
||
|
@@ -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): | ||
|
There was a problem hiding this comment.
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:use-fstring-for-concatenation
)