diff options
author | Ned Batchelder <ned@nedbatchelder.com> | 2021-11-13 12:20:54 -0500 |
---|---|---|
committer | Ned Batchelder <ned@nedbatchelder.com> | 2021-11-13 12:33:11 -0500 |
commit | ec48227a1f00cf2b00298d8189f1265daaa447d6 (patch) | |
tree | 5c37c3f7bead1aa8c114a1645516d6453745556a | |
parent | 5ca980dabd71aa74d90351a6d4e88cf101d65ded (diff) | |
download | python-coveragepy-git-ec48227a1f00cf2b00298d8189f1265daaa447d6.tar.gz |
refactor: a plural helper
-rw-r--r-- | coverage/cmdline.py | 6 | ||||
-rw-r--r-- | coverage/html.py | 16 | ||||
-rw-r--r-- | coverage/misc.py | 11 |
3 files changed, 19 insertions, 14 deletions
diff --git a/coverage/cmdline.py b/coverage/cmdline.py index f3a466e3..070683b8 100644 --- a/coverage/cmdline.py +++ b/coverage/cmdline.py @@ -21,7 +21,7 @@ from coverage.data import CoverageData, combinable_files, line_counts from coverage.debug import info_formatter, info_header, short_stack from coverage.exceptions import BaseCoverageException, ExceptionDuringRun, NoSource from coverage.execfile import PyRunner -from coverage.misc import human_sorted +from coverage.misc import human_sorted, plural from coverage.results import Numbers, should_fail_under @@ -819,9 +819,9 @@ class CoverageScript: summary = line_counts(data, fullpath=True) filenames = human_sorted(summary.keys()) nfiles = len(filenames) - print(f"{nfiles} file{'' if nfiles == 1 else 's'}:") + print(f"{nfiles} file{plural(nfiles)}:") for f in filenames: - line = f"{f}: {summary[f]} line{'' if summary[f] == 1 else 's'}" + line = f"{f}: {summary[f]} line{plural(summary[f])}" plugin = data.file_tracer(f) if plugin: line += f" [{plugin}]" diff --git a/coverage/html.py b/coverage/html.py index d018d0f0..2acc8aba 100644 --- a/coverage/html.py +++ b/coverage/html.py @@ -15,7 +15,7 @@ from coverage.data import add_data_to_hash from coverage.exceptions import CoverageException from coverage.files import flat_rootname from coverage.misc import ensure_dir, file_be_gone, Hasher, isolate_module, format_local_datetime -from coverage.misc import human_sorted +from coverage.misc import human_sorted, plural from coverage.report import get_analysis_to_report from coverage.results import Numbers from coverage.templite import Templite @@ -331,17 +331,11 @@ class HtmlReporter: skipped_covered_msg = skipped_empty_msg = "" if self.skipped_covered_count: - msg = "{} {} skipped due to complete coverage." - skipped_covered_msg = msg.format( - self.skipped_covered_count, - "file" if self.skipped_covered_count == 1 else "files", - ) + n = self.skipped_covered_count + skipped_covered_msg = f"{n} file{plural(n)} skipped due to complete coverage." if self.skipped_empty_count: - msg = "{} empty {} skipped." - skipped_empty_msg = msg.format( - self.skipped_empty_count, - "file" if self.skipped_empty_count == 1 else "files", - ) + n = self.skipped_empty_count + skipped_empty_msg = f"{n} empty file{plural(n)} skipped." html = index_tmpl.render({ 'files': self.file_summaries, diff --git a/coverage/misc.py b/coverage/misc.py index fa09f890..5b62fb4e 100644 --- a/coverage/misc.py +++ b/coverage/misc.py @@ -415,3 +415,14 @@ def human_sorted_items(items, reverse=False): Returns the sorted list of items. """ return sorted(items, key=lambda pair: (human_key(pair[0]), pair[1]), reverse=reverse) + + +def plural(n, thing="", things=""): + """Pluralize a word. + + If n is 1, return thing. Otherwise return things, or thing+s. + """ + if n == 1: + return thing + else: + return things or (thing + "s") |