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

Sourcery refactored master branch #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 52 additions & 62 deletions asv/benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
Run a Unix socket forkserver.
"""


Copy link
Author

Choose a reason for hiding this comment

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

Lines 36-40 refactored with the following changes:

# !!!!!!!!!!!!!!!!!!!! NOTE !!!!!!!!!!!!!!!!!!!!
# This file, unlike most others, must be compatible with as many
# versions of Python as possible and have no dependencies outside of
Expand All @@ -33,11 +34,7 @@
# sys.path[0] on start which can shadow other modules
import sys

if __name__ == "__main__":
_old_sys_path_head = sys.path.pop(0)
else:
_old_sys_path_head = None

_old_sys_path_head = sys.path.pop(0) if __name__ == "__main__" else None
import copy
import cProfile as profile
import ctypes
Expand Down Expand Up @@ -170,18 +167,17 @@ def recvall(sock, size):


def _get_attr(source, name, ignore_case=False):
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 -173 to +180
Copy link
Author

Choose a reason for hiding this comment

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

Function _get_attr refactored with the following changes:



def _get_all_attrs(sources, name, ignore_case=False):
Expand Down Expand Up @@ -282,20 +278,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 -285 to +287
Copy link
Author

Choose a reason for hiding this comment

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

Function check_num_args refactored with the following changes:

if min_num_args == max_num_args:
num_args_str = min_num_args
else:
Expand All @@ -310,16 +299,14 @@ def check_num_args(root, benchmark_name, func, min_num_args, max_num_args=None):
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 -313 to +309
Copy link
Author

Choose a reason for hiding this comment

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

Function _repr_no_address refactored with the following changes:


return result

Expand Down Expand Up @@ -361,18 +348,18 @@ def __init__(self, name, func, attr_sources):
try:
self.param_names = [str(x) for x in list(self.param_names)]
except ValueError:
raise ValueError("%s.param_names is not a list of strings" % (name,))
raise ValueError(f"{name}.param_names is not a list of strings")

try:
self._params = list(self._params)
except ValueError:
raise ValueError("%s.params is not a list" % (name,))
raise ValueError(f"{name}.params is not a list")

if self._params and not isinstance(self._params[0], (tuple, list)):
# 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 -364 to +362
Copy link
Author

Choose a reason for hiding this comment

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

Function Benchmark.__init__ refactored with the following changes:


if len(self.param_names) != len(self._params):
self.param_names = self.param_names[:len(self._params)]
Expand All @@ -388,7 +375,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 @@ -421,20 +408,28 @@ def check(self, root):
max_num_args = min_num_args

if self.setup_cache_key is not None:
ok = ok and check_num_args(root, self.name + ": setup_cache",
self._setup_cache, 0)
ok = ok and check_num_args(
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)
ok = ok and check_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)
ok = ok and check_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)
ok = ok and check_num_args(
root,
f"{self.name}: teardown",
teardown,
min_num_args,
max_num_args,
)
Comment on lines -424 to +432
Copy link
Author

Choose a reason for hiding this comment

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

Function Benchmark.check refactored with the following changes:


return ok

Expand Down Expand Up @@ -533,13 +528,7 @@ def func():
def run(self, *param):
warmup_time = self.warmup_time
if warmup_time < 0:
if '__pypy__' in sys.modules:
warmup_time = 1.0
else:
# Transient effects exist also on CPython, e.g. from
# OS scheduling
warmup_time = 0.1

warmup_time = 1.0 if '__pypy__' in sys.modules else 0.1
Comment on lines -536 to +531
Copy link
Author

Choose a reason for hiding this comment

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

Function TimeBenchmark.run refactored with the following changes:

This removes the following comments ( why? ):

# Transient effects exist also on CPython, e.g. from
# OS scheduling

timer = self._get_timer(*param)

try:
Expand Down Expand Up @@ -840,9 +829,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=ignore_import_errors):
yield item
for _, name, _ in pkgutil.iter_modules(module.__path__, f'{module_name}.'):
yield from disc_modules(name, ignore_import_errors=ignore_import_errors)
Comment on lines -843 to +833
Copy link
Author

Choose a reason for hiding this comment

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

Function disc_modules refactored with the following changes:



def disc_benchmarks(root, ignore_import_errors=False):
Expand Down Expand Up @@ -908,7 +896,7 @@ def get_benchmark_from_name(root, name, extra_params=None):
# 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 -911 to +899
Copy link
Author

Choose a reason for hiding this comment

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

Function get_benchmark_from_name refactored with the following changes:

if not os.path.isfile(path):
continue
modname = '.'.join([os.path.basename(root)] + parts[:-i])
Expand Down Expand Up @@ -965,10 +953,12 @@ def list_benchmarks(root, fp):
for benchmark in disc_benchmarks(root):
if not first:
fp.write(', ')
clean = dict(
(k, v) for (k, v) in benchmark.__dict__.items()
if isinstance(v, (str, int, float, list, dict, bool)) and not
k.startswith('_'))
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 -968 to +961
Copy link
Author

Choose a reason for hiding this comment

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

Function list_benchmarks refactored with the following changes:

json.dump(clean, fp, skipkeys=True)
first = False
fp.write(']')
Expand Down Expand Up @@ -1131,7 +1121,7 @@ def main_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(benchmark_dir, ignore_import_errors=True):
for _ in disc_benchmarks(benchmark_dir, ignore_import_errors=True):
Comment on lines -1134 to +1124
Copy link
Author

Choose a reason for hiding this comment

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

Function main_run_server refactored with the following changes:

pass

# Report result
Expand Down Expand Up @@ -1297,9 +1287,9 @@ def main():
sys.exit(1)

mode = sys.argv[1]
args = sys.argv[2:]

if mode in commands:
Comment on lines -1300 to 1302
Copy link
Author

Choose a reason for hiding this comment

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

Function main refactored with the following changes:

args = sys.argv[2:]

commands[mode](args)
sys.exit(0)
else:
Expand Down
17 changes: 7 additions & 10 deletions asv/benchmarks.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,7 @@ def __init__(self, conf, benchmarks, regex=None):
self._benchmark_selection[benchmark['name']] = []
for idx, param_set in enumerate(
itertools.product(*benchmark['params'])):
name = '%s(%s)' % (
benchmark['name'],
', '.join(param_set))
name = f"{benchmark['name']}({', '.join(param_set)})"
Copy link
Author

Choose a reason for hiding this comment

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

Function Benchmarks.__init__ refactored with the following changes:

if not regex or any(re.search(reg, name) for reg in regex):
self[benchmark['name']] = benchmark
self._benchmark_selection[benchmark['name']].append(idx)
Expand Down Expand Up @@ -153,15 +151,13 @@ def _disc_benchmarks(cls, conf, repo, environments, commit_hashes, check):
#

def iter_hashes():
for h in commit_hashes[:1]:
yield h
yield from commit_hashes[:1]
for branch in conf.branches:
try:
yield repo.get_hash_from_name(branch)
except NoSuchNameError:
continue
for h in commit_hashes[1:]:
yield h
yield from commit_hashes[1:]
Comment on lines -156 to +160
Copy link
Author

Choose a reason for hiding this comment

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

Function Benchmarks._disc_benchmarks refactored with the following changes:

  • Replace yield inside for loop with yield from [×2] (yield-from)


def iter_unique(iter):
seen = set()
Expand Down Expand Up @@ -326,13 +322,14 @@ def load(cls, conf, regex=None):
try:
path = cls.get_benchmark_file_path(conf.results_dir)
if not os.path.isfile(path):
raise util.UserError("Benchmark list file {} missing!".format(path))
raise util.UserError(f"Benchmark list file {path} missing!")
Comment on lines -329 to +325
Copy link
Author

Choose a reason for hiding this comment

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

Function Benchmarks.load refactored with the following changes:

d = util.load_json(path, api_version=cls.api_version)
benchmarks = d.values()
return cls(conf, benchmarks, regex=regex)
except util.UserError as err:
if "asv update" in str(err):
# Don't give conflicting instructions
raise
raise util.UserError("{}\nUse `asv run --bench just-discover` to "
"regenerate benchmarks.json".format(str(err)))
raise util.UserError(
f"{str(err)}\nUse `asv run --bench just-discover` to regenerate benchmarks.json"
)
2 changes: 1 addition & 1 deletion asv/build_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def _get_cache_dir(self, commit_hash):
Get the cache dir and timestamp file corresponding to a given commit hash.
"""
path = os.path.join(self._path, commit_hash)
stamp = path + ".timestamp"
stamp = f"{path}.timestamp"
Copy link
Author

Choose a reason for hiding this comment

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

Function BuildCache._get_cache_dir refactored with the following changes:

return path, stamp

def _remove_cache_dir(self, commit_hash):
Expand Down
20 changes: 12 additions & 8 deletions asv/commands/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def help(args):
"help", help="Display usage information")
help_parser.set_defaults(func=help)

commands = dict((x.__name__, x) for x in util.iter_subclasses(Command))
commands = {x.__name__: x for x in util.iter_subclasses(Command)}

for command in command_order:
subparser = commands[str(command)].setup_arguments(subparsers)
Expand All @@ -102,13 +102,17 @@ def _make_docstring():
lines = []

for p in subparsers.choices.values():
lines.append('.. _cmd-{0}:'.format(p.prog.replace(' ', '-')))
lines.append('')
lines.append(p.prog)
lines.append('-' * len(p.prog))
lines.append('::')
lines.append('')
lines.extend(' ' + x for x in p.format_help().splitlines())
lines.extend(
(
'.. _cmd-{0}:'.format(p.prog.replace(' ', '-')),
'',
p.prog,
'-' * len(p.prog),
'::',
'',
)
)
lines.extend(f' {x}' for x in p.format_help().splitlines())
lines.append('')

return '\n'.join(lines)
Loading