Skip to content

Commit bf05db8

Browse files
authored
Merge pull request #1596: Use ngettext to handle plurality
2 parents 09cb977 + b06ac37 commit bf05db8

File tree

4 files changed

+20
-20
lines changed

4 files changed

+20
-20
lines changed

augur/filter/_run.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from augur.io.file import PANDAS_READ_CSV_OPTIONS, open_file
1919
from augur.io.metadata import InvalidDelimiter, Metadata, read_metadata
2020
from augur.io.sequences import read_sequences, write_sequences
21-
from augur.io.print import print_err
21+
from augur.io.print import print_err, _n
2222
from augur.io.vcf import is_vcf as filename_is_vcf, write_vcf
2323
from augur.types import EmptyOutputReportingMethod
2424
from . import include_exclude_rules
@@ -432,7 +432,7 @@ def run(args):
432432
total_strains_passed = len(valid_strains)
433433
total_strains_filtered = len(metadata_strains) + num_excluded_by_lack_of_metadata - total_strains_passed
434434

435-
print_err(f"{total_strains_filtered} {'strain was' if total_strains_filtered == 1 else 'strains were'} dropped during filtering")
435+
print_err(f"{total_strains_filtered} {_n('strain was', 'strains were', total_strains_filtered)} dropped during filtering")
436436

437437
if num_excluded_by_lack_of_metadata:
438438
print_err(f"\t{num_excluded_by_lack_of_metadata} had no metadata")
@@ -462,13 +462,13 @@ def run(args):
462462
parameters = {}
463463

464464
parameters["count"] = count
465-
parameters["were"] = "was" if count == 1 else "were"
466-
parameters["they"] = "it" if count == 1 else "they"
465+
parameters["were"] = _n("was", "were", count)
466+
parameters["they"] = _n("it", "they", count)
467467
print_err("\t" + report_template_by_filter_name[filter_name].format(**parameters))
468468

469469
if (group_by and args.sequences_per_group) or args.subsample_max_sequences:
470470
seed_txt = ", using seed {}".format(args.subsample_seed) if args.subsample_seed else ""
471-
print_err(f"\t{num_excluded_subsamp} {'was' if num_excluded_subsamp == 1 else 'were'} dropped because of subsampling criteria{seed_txt}")
471+
print_err(f"\t{num_excluded_subsamp} {_n('was', 'were', num_excluded_subsamp)} dropped because of subsampling criteria{seed_txt}")
472472

473473
if total_strains_passed == 0:
474474
empty_results_message = "All samples have been dropped! Check filter rules and metadata file format."
@@ -481,4 +481,4 @@ def run(args):
481481
else:
482482
raise ValueError(f"Encountered unhandled --empty-output-reporting method {args.empty_output_reporting!r}")
483483

484-
print_err(f"{total_strains_passed} {'strain' if total_strains_passed == 1 else 'strains'} passed all filters")
484+
print_err(f"{total_strains_passed} {_n('strain', 'strains', total_strains_passed)} passed all filters")

augur/filter/subsample.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from augur.dates import get_year_month, get_year_week
1111
from augur.errors import AugurError
1212
from augur.io.metadata import METADATA_DATE_COLUMN
13-
from augur.io.print import print_err
13+
from augur.io.print import print_err, _n
1414
from . import constants
1515
from .weights_file import WEIGHTS_COLUMN, COLUMN_VALUE_FOR_DEFAULT_WEIGHT, get_default_weight, get_weighted_columns, read_weights_file
1616

@@ -352,8 +352,8 @@ def get_weighted_group_sizes(
352352
# Warn on any under-sampled groups
353353
for _, row in weights.iterrows():
354354
if row[INPUT_SIZE_COLUMN] < row[TARGET_SIZE_COLUMN]:
355-
sequences = 'sequence' if row[TARGET_SIZE_COLUMN] == 1 else 'sequences'
356-
are = 'is' if row[INPUT_SIZE_COLUMN] == 1 else 'are'
355+
sequences = _n('sequence', 'sequences', row[TARGET_SIZE_COLUMN])
356+
are = _n('is', 'are', row[INPUT_SIZE_COLUMN])
357357
group = list(f'{col}={value!r}' for col, value in row[group_by].items())
358358
print_err(f"WARNING: Targeted {row[TARGET_SIZE_COLUMN]} {sequences} for group {group} but only {row[INPUT_SIZE_COLUMN]} {are} available.")
359359

@@ -411,7 +411,7 @@ def _drop_unused_groups(
411411
extra_groups = set(weights.index) - valid_index
412412
if extra_groups:
413413
count = len(extra_groups)
414-
unit = "group" if count == 1 else "groups"
414+
unit = _n("group", "groups", count)
415415
print_err(f"NOTE: Skipping {count} {unit} due to lack of entries in metadata.")
416416
weights = weights[weights.index.isin(valid_index)]
417417

@@ -427,8 +427,8 @@ def _adjust_weights_for_unweighted_columns(
427427
) -> pd.DataFrame:
428428
"""Adjust weights for unweighted columns to reflect equal weighting within each weighted group.
429429
"""
430-
columns = 'column' if len(unweighted_columns) == 1 else 'columns'
431-
those = 'that' if len(unweighted_columns) == 1 else 'those'
430+
columns = _n('column', 'columns', len(unweighted_columns))
431+
those = _n('that', 'those', len(unweighted_columns))
432432
print_err(f"NOTE: Weights were not provided for the {columns} {', '.join(repr(col) for col in unweighted_columns)}. Using equal weights across values in {those} {columns}.")
433433

434434
weights_grouped = weights.groupby(weighted_columns)

augur/io/print.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import gettext
12
import sys
23

34
from augur.debug import DEBUGGING
@@ -13,3 +14,9 @@ def print_debug(*args):
1314
"""Print to stderr if in debugging mode."""
1415
if DEBUGGING:
1516
print_err(*args)
17+
18+
19+
# Use ngettext() without a message catalog for its singular/plural handling so
20+
# we can make proper error messages. gettext() (no "n") is conventionally
21+
# aliased as "_", so alias ngettext() as "_n".
22+
_n = gettext.NullTranslations().ngettext

augur/merge.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
you want to use a version different from what's on PATH), set the SQLITE3
3636
environment variable to path of the desired sqlite3 executable.
3737
"""
38-
import gettext
3938
import os
4039
import re
4140
import subprocess
@@ -51,19 +50,13 @@
5150
from augur.argparse_ import ExtendOverwriteDefault, SKIP_AUTO_DEFAULT_IN_HELP
5251
from augur.errors import AugurError
5352
from augur.io.metadata import DEFAULT_DELIMITERS, DEFAULT_ID_COLUMNS, Metadata
54-
from augur.io.print import print_err, print_debug
53+
from augur.io.print import print_err, print_debug, _n
5554
from augur.utils import first_line
5655

5756

5857
T = TypeVar('T')
5958

6059

61-
# Use ngettext() without a message catalog for its singular/plural handling so
62-
# we can make proper error messages. gettext() (no "n") is conventionally
63-
# aliased as "_", so alias ngettext() as "_n".
64-
_n = gettext.NullTranslations().ngettext
65-
66-
6760
class NamedMetadata(Metadata):
6861
name: str
6962
"""User-provided descriptive name for this metadata file."""

0 commit comments

Comments
 (0)