Skip to content

Commit

Permalink
MAINT: 'Refactored by Sourcery' (#1)
Browse files Browse the repository at this point in the history
Co-authored-by: Sourcery AI <>
  • Loading branch information
sourcery-ai[bot] authored Jul 8, 2023
1 parent f4994bb commit f6a2e9b
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 116 deletions.
4 changes: 2 additions & 2 deletions asv_runner/benchmarks/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
adding a new submodule with an "export_as_benchmark" attribute.
"""


import importlib
import pkgutil

Expand All @@ -47,8 +48,7 @@
try:
module = importlib.import_module(f"{pkgname}.{module_name}")
if "export_as_benchmark" in dir(module):
for bench in getattr(module, "export_as_benchmark"):
benchmark_types.append(bench)
benchmark_types.extend(iter(getattr(module, "export_as_benchmark")))
except NotRequired:
# Ignored.
pass
62 changes: 28 additions & 34 deletions asv_runner/benchmarks/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,18 @@ def _get_attr(source, name, ignore_case=False):
**ValueError**
: If more than one attribute with the given name exists and `ignore_case` is `True`.
"""
if ignore_case:
attrs = [
getattr(source, key) for key in dir(source) if key.lower() == name.lower()
]

if len(attrs) > 1:
raise ValueError(f"{source.__name__} contains multiple {name} functions.")
elif len(attrs) == 1:
return attrs[0]
else:
return None
else:
if not ignore_case:
return getattr(source, name, None)
attrs = [
getattr(source, key) for key in dir(source) if key.lower() == name.lower()
]

if len(attrs) > 1:
raise ValueError(f"{source.__name__} contains multiple {name} functions.")
elif len(attrs) == 1:
return attrs[0]
else:
return None


def _get_all_attrs(sources, name, ignore_case=False):
Expand Down Expand Up @@ -273,20 +272,13 @@ def check_num_args(root, benchmark_name, func, min_num_args, max_num_args=None):
if inspect.ismethod(func):
max_args -= 1

if info.defaults is not None:
min_args = max_args - len(info.defaults)
else:
min_args = max_args

min_args = max_args if info.defaults is None else max_args - len(info.defaults)
if info.varargs is not None:
max_args = math.inf

ok = (min_args <= max_num_args) and (min_num_args <= max_args)
if not ok:
if min_args == max_args:
args_str = min_args
else:
args_str = f"{min_args}-{max_args}"
args_str = min_args if min_args == max_args else f"{min_args}-{max_args}"
if min_num_args == max_num_args:
num_args_str = min_num_args
else:
Expand Down Expand Up @@ -332,16 +324,14 @@ def _repr_no_address(obj):
"""
result = repr(obj)
address_regex = re.compile(r"^(<.*) at (0x[\da-fA-F]*)(>)$")
match = address_regex.match(result)
if match:
suspected_address = match.group(2)
if match := address_regex.match(result):
suspected_address = match[2]
# Double check this is the actual address
default_result = object.__repr__(obj)
match2 = address_regex.match(default_result)
if match2:
known_address = match2.group(2)
if match2 := address_regex.match(default_result):
known_address = match2[2]
if known_address == suspected_address:
result = match.group(1) + match.group(3)
result = match[1] + match[3]

return result

Expand Down Expand Up @@ -464,7 +454,7 @@ def __init__(self, name, func, attr_sources):
# Accept a single list for one parameter only
self._params = [self._params]
else:
self._params = [[item for item in entry] for entry in self._params]
self._params = [list(entry) for entry in self._params]

if len(self.param_names) != len(self._params):
self.param_names = self.param_names[: len(self._params)]
Expand All @@ -484,7 +474,7 @@ def __init__(self, name, func, attr_sources):
for j in range(len(param)):
name = param[j]
if name in dupe_dict:
param[j] = name + f" ({dupe_dict[name]})"
param[j] = f"{name} ({dupe_dict[name]})"
dupe_dict[name] += 1
self.params[i] = param

Expand Down Expand Up @@ -561,22 +551,26 @@ def check(self, root):

if self.setup_cache_key is not None:
ok = ok and check_num_args(
root, self.name + ": setup_cache", self._setup_cache, 0
root, f"{self.name}: setup_cache", self._setup_cache, 0
)
max_num_args += 1

for setup in self._setups:
ok = ok and check_num_args(
root, self.name + ": setup", setup, min_num_args, max_num_args
root, f"{self.name}: setup", setup, min_num_args, max_num_args
)

ok = ok and check_num_args(
root, self.name + ": call", self.func, min_num_args, max_num_args
root, f"{self.name}: call", self.func, min_num_args, max_num_args
)

for teardown in self._teardowns:
ok = ok and check_num_args(
root, self.name + ": teardown", teardown, min_num_args, max_num_args
root,
f"{self.name}: teardown",
teardown,
min_num_args,
max_num_args,
)

return ok
Expand Down
36 changes: 9 additions & 27 deletions asv_runner/console.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@ def isatty(file):
but some user-defined types may not. In such cases, this function
assumes those are not ttys.
"""
if hasattr(file, "isatty"):
return file.isatty()
return False
return file.isatty() if hasattr(file, "isatty") else False


def _color_text(text, color):
Expand Down Expand Up @@ -133,12 +131,9 @@ def _write_with_fallback(s, fileobj):
if not isinstance(s, str):
raise ValueError("Input string is not a Unicode string")

try:
with contextlib.suppress(UnicodeError):
fileobj.write(s)
return
except UnicodeError:
pass

# Fall back to writing bytes
enc = locale.getpreferredencoding()
try:
Expand Down Expand Up @@ -192,21 +187,17 @@ def color_print(*args, **kwargs):
if isatty(file) and not WIN:
for i in range(0, len(args), 2):
msg = args[i]
if i + 1 == len(args):
color = ""
else:
color = args[i + 1]

color = "" if i + 1 == len(args) else args[i + 1]
if color:
msg = _color_text(msg, color)
_write_with_fallback(msg, file)

_write_with_fallback(end, file)
else:
for i in range(0, len(args), 2):
msg = args[i]
_write_with_fallback(msg, file)
_write_with_fallback(end, file)

_write_with_fallback(end, file)


def get_answer_default(prompt, default, use_defaults=False):
Expand Down Expand Up @@ -238,16 +229,11 @@ def get_answer_default(prompt, default, use_defaults=False):
return default

x = input()
if x.strip() == "":
return default
return x
return default if x.strip() == "" else x


def truncate_left(s, l):
if len(s) > l:
return "..." + s[-(l - 3) :]
else:
return s
return f"...{s[-(l - 3):]}" if len(s) > l else s


class Log:
Expand Down Expand Up @@ -275,11 +261,7 @@ def _stream_formatter(self, record):
color_print("")
parts = record.msg.split("\n", 1)
first_line = parts[0]
if len(parts) == 1:
rest = None
else:
rest = parts[1]

rest = None if len(parts) == 1 else parts[1]
indent = self._indent + 1
continued = getattr(record, "continued", False)

Expand Down Expand Up @@ -313,11 +295,11 @@ def _stream_formatter(self, record):
else:
color = "red"

spaces = " " * indent
color_print(first_line, color, end="")
if rest is not None:
color_print("")
detail = textwrap.dedent(rest)
spaces = " " * indent
for line in detail.split("\n"):
color_print(spaces, end="")
color_print(line)
Expand Down
13 changes: 6 additions & 7 deletions asv_runner/discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,8 @@ def disc_modules(module_name, ignore_import_errors=False):
yield module

if getattr(module, "__path__", None):
for _, name, _ in pkgutil.iter_modules(module.__path__, module_name + "."):
for item in disc_modules(name, ignore_import_errors):
yield item
for _, name, _ in pkgutil.iter_modules(module.__path__, f"{module_name}."):
yield from disc_modules(name, ignore_import_errors)


def disc_benchmarks(root, ignore_import_errors=False):
Expand Down Expand Up @@ -204,7 +203,7 @@ def get_benchmark_from_name(root, name, extra_params=None):
# try to directly import benchmark function by guessing its import module name
parts = name.split(".")
for i in [1, 2]:
path = os.path.join(root, *parts[:-i]) + ".py"
path = f"{os.path.join(root, *parts[:-i])}.py"
if not os.path.isfile(path):
continue
modname = ".".join([os.path.basename(root)] + parts[:-i])
Expand Down Expand Up @@ -274,12 +273,12 @@ def list_benchmarks(root, fp):
for benchmark in disc_benchmarks(root):
if not first:
fp.write(", ")
clean = dict(
(k, v)
clean = {
k: v
for (k, v) in benchmark.__dict__.items()
if isinstance(v, (str, int, float, list, dict, bool))
and not k.startswith("_")
)
}
json.dump(clean, fp, skipkeys=True)
first = False
fp.write("]")
Expand Down
2 changes: 1 addition & 1 deletion asv_runner/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def _run_server(args):
# Import benchmark suite before forking.
# Capture I/O to a file during import.
with posix_redirect_output(stdout_file, permanent=False):
for benchmark in disc_benchmarks(
for _ in disc_benchmarks(
benchmark_dir, ignore_import_errors=True
):
pass
Expand Down
46 changes: 8 additions & 38 deletions asv_runner/statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,7 @@ def quantile(x, q):
j = int(math.floor(z))
z -= j

if j == n - 1:
m = y[-1]
else:
m = (1 - z) * y[j] + z * y[j + 1]

return m
return y[-1] if j == n - 1 else (1 - z) * y[j] + z * y[j + 1]


def quantile_ci(x, q, alpha_min=0.01):
Expand Down Expand Up @@ -230,11 +225,7 @@ def __init__(self, y, nu=None):
if len(y) == 0:
raise ValueError("empty input")

if nu is None:
self.nu = len(y) - 1
else:
self.nu = nu

self.nu = len(y) - 1 if nu is None else nu
# Sort input
y = sorted(y)

Expand All @@ -251,7 +242,7 @@ def __init__(self, y, nu=None):
if self._y_scale != 0:
self.y = [(yp - self.mle) / self._y_scale for yp in y]
else:
self.y = [0 for yp in y]
self.y = [0 for _ in y]

self._cdf_norm = None
self._cdf_memo = {}
Expand Down Expand Up @@ -283,13 +274,7 @@ def _cdf_unnorm(self, beta):
if beta != beta:
return beta

for k, y in enumerate(self.y):
if y > beta:
k0 = k
break
else:
k0 = len(self.y)

k0 = next((k for k, y in enumerate(self.y) if y > beta), len(self.y))
cdf = 0

nu = self.nu
Expand All @@ -307,16 +292,8 @@ def _cdf_unnorm(self, beta):
c = 2 * k - len(self.y)
y = sum(self.y[k:]) - sum(self.y[:k])

if k == 0:
a = -math.inf
else:
a = self.y[k - 1]

if k == k0:
b = beta
else:
b = self.y[k]

a = -math.inf if k == 0 else self.y[k - 1]
b = beta if k == k0 else self.y[k]
if c == 0:
term = (b - a) / y ** (nu + 1)
else:
Expand Down Expand Up @@ -371,19 +348,12 @@ def _ppf_unnorm(self, cdfx):

if k == 0:
z = -nu * c * term
if z > 0:
beta = (z ** (-1 / nu) - y) / c
else:
beta = -math.inf
beta = (z ** (-1 / nu) - y) / c if z > 0 else -math.inf
elif c == 0:
beta = a + term * y ** (nu + 1)
else:
z = (a * c + y) ** (-nu) - nu * c * term
if z > 0:
beta = (z ** (-1 / nu) - y) / c
else:
beta = math.inf

beta = (z ** (-1 / nu) - y) / c if z > 0 else math.inf
if k < len(self.y):
beta = min(beta, self.y[k])

Expand Down
10 changes: 3 additions & 7 deletions asv_runner/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,7 @@ def human_float(value, significant=3, truncate_small=None, significant_zeros=Fal
if magnitude <= -5 or magnitude >= 9:
# Too many digits, use scientific notation
fmt = f"{{0:.{significant}e}}"
elif value == int(value):
value = int(round(value, num_digits))
fmt = "{0:d}"
elif num_digits <= 0:
elif value == int(value) or num_digits <= 0:
value = int(round(value, num_digits))
fmt = "{0:d}"
else:
Expand Down Expand Up @@ -162,7 +159,6 @@ def human_time(seconds, err=None):
str_time = human_float(seconds / units[i][1], 3, significant_zeros=True)
if err is None:
return f"{str_time:s}{units[i][0]}"
else:
str_err = human_float(err / units[i][1], 1, truncate_small=2)
return f"{str_time:s}±{str_err:s}{units[i][0]}"
str_err = human_float(err / units[i][1], 1, truncate_small=2)
return f"{str_time:s}±{str_err:s}{units[i][0]}"
return "~0"

0 comments on commit f6a2e9b

Please sign in to comment.