-
Notifications
You must be signed in to change notification settings - Fork 0
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
Sourcery refactored main branch #1
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 |
---|---|---|
|
@@ -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 | ||
Comment on lines
-34
to
+45
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
|
||
|
||
|
||
def _get_all_attrs(sources, name, ignore_case=False): | ||
|
@@ -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}" | ||
Comment on lines
-276
to
+281
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 min_num_args == max_num_args: | ||
num_args_str = min_num_args | ||
else: | ||
|
@@ -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] | ||
Comment on lines
-335
to
+334
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 result | ||
|
||
|
@@ -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] | ||
Comment on lines
-467
to
+457
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 len(self.param_names) != len(self._params): | ||
self.param_names = self.param_names[: len(self._params)] | ||
|
@@ -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 | ||
|
||
|
@@ -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, | ||
Comment on lines
-564
to
+573
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 ok | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
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
|
||
|
||
|
||
def _color_text(text, color): | ||
|
@@ -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 | ||
|
||
Comment on lines
-136
to
-141
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
|
||
# Fall back to writing bytes | ||
enc = locale.getpreferredencoding() | ||
try: | ||
|
@@ -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) | ||
Comment on lines
-195
to
+200
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
|
||
|
||
|
||
def get_answer_default(prompt, default, use_defaults=False): | ||
|
@@ -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 | ||
Comment on lines
-241
to
+232
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
|
||
|
||
|
||
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 | ||
Comment on lines
-247
to
+236
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
|
||
|
||
|
||
class Log: | ||
|
@@ -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] | ||
Comment on lines
-278
to
+264
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
|
||
indent = self._indent + 1 | ||
continued = getattr(record, "continued", False) | ||
|
||
|
@@ -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) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
Comment on lines
-103
to
+104
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
|
||
|
||
|
||
def disc_benchmarks(root, ignore_import_errors=False): | ||
|
@@ -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" | ||
Comment on lines
-207
to
+206
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 not os.path.isfile(path): | ||
continue | ||
modname = ".".join([os.path.basename(root)] + parts[:-i]) | ||
|
@@ -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("_") | ||
) | ||
} | ||
Comment on lines
-277
to
+281
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
|
||
json.dump(clean, fp, skipkeys=True) | ||
first = False | ||
fp.write("]") | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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( | ||
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
|
||
benchmark_dir, ignore_import_errors=True | ||
): | ||
pass | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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] | ||
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
|
||
|
||
|
||
def quantile_ci(x, q, alpha_min=0.01): | ||
|
@@ -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 | ||
Comment on lines
-233
to
+228
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
|
||
# Sort input | ||
y = sorted(y) | ||
|
||
|
@@ -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 = {} | ||
|
@@ -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)) | ||
Comment on lines
-286
to
+277
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
|
||
cdf = 0 | ||
|
||
nu = self.nu | ||
|
@@ -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: | ||
|
@@ -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 | ||
Comment on lines
-374
to
+356
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 k < len(self.y): | ||
beta = min(beta, self.y[k]) | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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: | ||
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
|
||
value = int(round(value, num_digits)) | ||
fmt = "{0:d}" | ||
else: | ||
|
@@ -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]}" | ||
Comment on lines
-165
to
+163
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 "~0" |
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.
Lines
50-51
refactored with the following changes:for-append-to-extend
)simplify-generator
)