diff options
author | Ned Batchelder <ned@nedbatchelder.com> | 2022-11-06 10:59:13 -0500 |
---|---|---|
committer | Ned Batchelder <ned@nedbatchelder.com> | 2022-11-06 10:59:13 -0500 |
commit | 30f1ecf0657fa89b56eab300f10c58852edbbcdd (patch) | |
tree | 930a98a0b25852da7e190142421be2f4eabe97d3 /coverage | |
parent | a4da2c62c45132e1a7d1e1f78e32522be5ad405f (diff) | |
download | python-coveragepy-git-30f1ecf0657fa89b56eab300f10c58852edbbcdd.tar.gz |
refactor: human sorting
Diffstat (limited to 'coverage')
-rw-r--r-- | coverage/collector.py | 6 | ||||
-rw-r--r-- | coverage/misc.py | 11 | ||||
-rw-r--r-- | coverage/summary.py | 6 |
3 files changed, 13 insertions, 10 deletions
diff --git a/coverage/collector.py b/coverage/collector.py index 241de05e..ef1d9b41 100644 --- a/coverage/collector.py +++ b/coverage/collector.py @@ -11,7 +11,7 @@ from coverage.config import CoverageConfig from coverage.debug import short_stack from coverage.disposition import FileDisposition from coverage.exceptions import ConfigError -from coverage.misc import human_sorted, isolate_module +from coverage.misc import human_sorted_items, isolate_module from coverage.pytracer import PyTracer os = isolate_module(os) @@ -367,8 +367,8 @@ class Collector: stats = tracer.get_stats() if stats: print("\nCoverage.py tracer stats:") - for k in human_sorted(stats.keys()): - print(f"{k:>20}: {stats[k]}") + for k, v in human_sorted_items(stats.items()): + print(f"{k:>20}: {v}") if self.threading: self.threading.settrace(None) diff --git a/coverage/misc.py b/coverage/misc.py index e3c67bc6..212790a1 100644 --- a/coverage/misc.py +++ b/coverage/misc.py @@ -368,7 +368,7 @@ def import_local_file(modname, modfile=None): return mod -def human_key(s): +def _human_key(s): """Turn a string into a list of string and number chunks. "z23a" -> ["z", 23, "a"] """ @@ -389,14 +389,17 @@ def human_sorted(strings): Returns the sorted list. """ - return sorted(strings, key=human_key) + return sorted(strings, key=_human_key) def human_sorted_items(items, reverse=False): - """Sort the (string, value) items the way humans expect. + """Sort (string, ...) items the way humans expect. + + The elements of `items` can be any tuple/list. They'll be sorted by the + first element (a string), with ties broken by the remaining elements. Returns the sorted list of items. """ - return sorted(items, key=lambda pair: (human_key(pair[0]), pair[1]), reverse=reverse) + return sorted(items, key=lambda item: (_human_key(item[0]), *item[1:]), reverse=reverse) def plural(n, thing="", things=""): diff --git a/coverage/summary.py b/coverage/summary.py index b27b8a1c..1aa802af 100644 --- a/coverage/summary.py +++ b/coverage/summary.py @@ -6,7 +6,7 @@ import sys from coverage.exceptions import ConfigError, NoDataError -from coverage.misc import human_key +from coverage.misc import human_sorted_items from coverage.report import get_analysis_to_report from coverage.results import Numbers @@ -198,9 +198,9 @@ class SummaryReporter: if sort_idx is None: raise ConfigError(f"Invalid sorting option: {self.config.sort!r}") if sort_option == "name": - lines_values.sort(key=lambda tup: (human_key(tup[0]), tup[1]), reverse=reverse) + lines_values = human_sorted_items(lines_values, reverse=reverse) else: - lines_values.sort(key=lambda tup: (tup[sort_idx], tup[0]), reverse=reverse) + lines_values.sort(key=lambda line: (line[sort_idx], line[0]), reverse=reverse) # Calculate total if we had at least one file. total_line = ["TOTAL", self.total.n_statements, self.total.n_missing] |