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

Fix PEP-8 violations reported by flake8 on asv/util.py #1007

Merged
merged 12 commits into from
Feb 8, 2022
5 changes: 4 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,7 @@ jobs:
run: pip install flake8

- name: flake8
run: flake8 . || true # preventing CI failing while flake8 issues exist
run: flake8 . || true # preventing CI failing while flake8 issues exist

- name: flake8 validating correct file(s)
run: flake8 asv/util.py
63 changes: 33 additions & 30 deletions asv/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,28 +81,28 @@ def reraise(self):
raise self


def human_list(l):
def human_list(input_list):
"""
Formats a list of strings in a human-friendly way.
"""
l = ["'{0}'".format(x) for x in l]
input_list = ["'{0}'".format(x) for x in input_list]

if len(l) == 0:
if len(input_list) == 0:
return 'nothing'
elif len(l) == 1:
return l[0]
elif len(l) == 2:
return ' and '.join(l)
elif len(input_list) == 1:
return input_list[0]
elif len(input_list) == 2:
return ' and '.join(input_list)
else:
return ', '.join(l[:-1]) + ' and ' + l[-1]
return ', '.join(input_list[:-1]) + ' and ' + input_list[-1]


def human_float(value, significant=3, truncate_small=None, significant_zeros=False):
"""
Return a string representing a float with human friendly significant digits.
Switches to scientific notation for too large/small numbers.
If `truncate_small`, then leading zeros of numbers < 1 are counted as
significant. If not `significant_zeros`, trailing unnecessary zeros are
If `truncate_small`, then leading zeros of numbers < 1 are counted as
significant. If not `significant_zeros`, trailing unnecessary zeros are
stripped.
"""
if value == 0:
Expand Down Expand Up @@ -144,7 +144,7 @@ def human_float(value, significant=3, truncate_small=None, significant_zeros=Fal

if significant_zeros and '.' not in formatted:
if len(formatted) < significant:
formatted += "." + "0"*(significant - len(formatted))
formatted += "." + "0" * (significant - len(formatted))

return formatted

Expand Down Expand Up @@ -254,7 +254,7 @@ def human_time(seconds, err=None):
return "n/a"

for i in xrange(len(units) - 1):
if scale < units[i+1][1]:
if scale < units[i + 1][1]:
str_time = human_float(seconds / units[i][1], 3, significant_zeros=True)
if err is None:
return "{0:s}{1}".format(str_time, units[i][0])
Expand Down Expand Up @@ -531,7 +531,7 @@ def get_content(header=None):
if env and WIN and sys.version_info < (3,):
# Environment keys and values cannot be unicode
def _fix_env(s):
return s.encode('mbcs') if isinstance(s, unicode) else s
return s.encode('mbcs') if isinstance(s, unicode) else s # noqa F821 'unicode' does not exist in py2
env = {_fix_env(k): _fix_env(v) for k, v in env.items()}

kwargs = dict(shell=shell, env=env, cwd=cwd,
Expand Down Expand Up @@ -566,7 +566,8 @@ def _fix_env(s):
debug_log = DebugLogBuffer(log)
dots = False
else:
debug_log = lambda c: None
def debug_log(c):
return None

if WIN:
start_time = [time.time()]
Expand All @@ -592,7 +593,8 @@ def stream_reader(stream, buf):
all_threads = [stdout_reader]

if not redirect_stderr:
stderr_reader = threading.Thread(target=stream_reader, args=(proc.stderr, stderr_chunks))
stderr_reader = threading.Thread(target=stream_reader,
args=(proc.stderr, stderr_chunks))
stderr_reader.daemon = True
stderr_reader.start()
all_threads.append(stderr_reader)
Expand Down Expand Up @@ -658,9 +660,7 @@ def sig_forward(signum, frame):
signal.signal(signal.SIGTSTP, sig_forward)
signal.signal(signal.SIGCONT, sig_forward)

fds = {
proc.stdout.fileno(): stdout_chunks
}
fds = {proc.stdout.fileno(): stdout_chunks}
if not redirect_stderr:
fds[proc.stderr.fileno()] = stderr_chunks

Expand Down Expand Up @@ -730,7 +730,9 @@ def sig_forward(signum, frame):

# Flush and disconnect debug log, if any
debug_log(None)
debug_log = lambda c: None

def debug_log(c):
return None

stdout = b''.join(stdout_chunks)
stderr = b''.join(stderr_chunks)
Expand Down Expand Up @@ -1000,7 +1002,7 @@ def get_cpu_info():
from win32com.client import GetObject
cimv = GetObject(r"winmgmts:root\cimv2")
return cimv.ExecQuery("Select Name from Win32_Processor")[0].name
except:
except sys.DoesNotExist:
pass
return ''

Expand Down Expand Up @@ -1045,7 +1047,7 @@ def _get_terminal_size_fallback():
if width > 10:
width -= 1
return (lines, width)
except:
except Exception:
# Fall back on environment variables, or if not set, (25, 80)
try:
return (int(os.environ.get('LINES')),
Expand Down Expand Up @@ -1089,7 +1091,7 @@ def format_text_table(rows, num_headers=0,
# Ensure same number of items on all rows
num_items = max(len(row) for row in text_rows)
for row in text_rows:
row.extend(['']*(num_items - len(row)))
row.extend([''] * (num_items - len(row)))

# Determine widths
col_widths = [max(len(row[j]) for row in text_rows) + 2
Expand All @@ -1102,14 +1104,14 @@ def format_text_table(rows, num_headers=0,
# Generate result
headers = [" ".join(row) for row in text_rows[:num_headers]]
content = [" ".join(row) for row in text_rows[num_headers:]]
separator = " ".join("-"*w for w in col_widths)
separator = " ".join("-" * w for w in col_widths)

result = []
if top_header_text is not None:
left_span = "-".join("-"*w for w in col_widths[:top_header_span_start])
right_span = "-".join("-"*w for w in col_widths[top_header_span_start:])
left_span = "-".join("-" * w for w in col_widths[:top_header_span_start])
right_span = "-".join("-" * w for w in col_widths[top_header_span_start:])
if left_span and right_span:
result += ["--" + " " * (len(left_span)-1) + top_header_text.center(len(right_span))]
result += ["--" + " " * (len(left_span) - 1) + top_header_text.center(len(right_span))]
result += [" ".join([left_span, right_span])]
else:
result += [top_header_text.center(len(separator))]
Expand All @@ -1129,7 +1131,7 @@ def _datetime_to_timestamp(dt, divisor):
delta = dt - datetime.datetime(1970, 1, 1)
microseconds = (delta.days * 86400 + delta.seconds) * 10**6 + delta.microseconds
value, remainder = divmod(microseconds, divisor)
if remainder >= divisor//2:
if remainder >= divisor // 2:
value += 1
return value

Expand Down Expand Up @@ -1190,7 +1192,7 @@ def geom_mean_na(values):
"""
values = [x for x in values if not is_na(x)]
if values:
exponent = 1/len(values)
exponent = 1 / len(values)
prod = 1.0
acc = 0
for x in values:
Expand All @@ -1203,12 +1205,13 @@ def geom_mean_na(values):

def ceildiv(numerator, denominator):
"""Ceiling division"""
return -((-numerator)//denominator)
return -((-numerator) // denominator)


if not WIN:
long_path_open = open
long_path_rmtree = shutil.rmtree

def long_path(path):
return path
else:
Expand Down Expand Up @@ -1368,7 +1371,7 @@ def interpolate_command(command, variables):
return_codes_set = True
del result[0]
continue
except ValueError as exc:
except ValueError:
pass

raise UserError("Configuration error: invalid return-code specification "
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[flake8]
max-line-length = 99
ignore = ""
ignore = W504