From 0eaeb99f2de1330a562752d30d02d1898f681cf8 Mon Sep 17 00:00:00 2001 From: Ned Batchelder Date: Tue, 12 Oct 2021 08:46:25 -0400 Subject: fix: use human sorting on human-readable things --- coverage/misc.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) (limited to 'coverage/misc.py') diff --git a/coverage/misc.py b/coverage/misc.py index 29397537..40f00930 100644 --- a/coverage/misc.py +++ b/coverage/misc.py @@ -389,3 +389,34 @@ def import_local_file(modname, modfile=None): spec.loader.exec_module(mod) return mod + + +def human_key(s): + """Turn a string into a list of string and number chunks. + "z23a" -> ["z", 23, "a"] + """ + def tryint(s): + """If `s` is a number, return an int, else `s` unchanged.""" + try: + return int(s) + except ValueError: + return s + + return [tryint(c) for c in re.split(r"(\d+)", s)] + +def human_sorted(strings): + """Sort the given iterable of strings the way that humans expect. + + Numeric components in the strings are sorted as numbers. + + Returns the sorted list. + + """ + return sorted(strings, key=human_key) + +def human_sorted_items(items, reverse=False): + """Sort the (string, value) items the way humans expect. + + Returns the sorted list of items. + """ + return sorted(items, key=lambda pair: (human_key(pair[0]), pair[1]), reverse=reverse) -- cgit v1.2.1