FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

numbers: Preserve quoted semicolons in patterns by himanshu748 · Pull Request #1382 · python-babel/babel · GitHub

Repository navigation

Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (2) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
12 changes: 9 additions & 3 deletions babel/numbers.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -1306,10 +1306,16 @@ def _match_number(pattern):
return rv.groups()

pos_pattern = pattern
neg_pattern = None

# Do we have a negative subpattern?
if ';' in pattern:
pos_pattern, neg_pattern = pattern.split(';', 1)
# Only an unquoted semicolon separates the negative subpattern.
for match in re.finditer(r"'[^']*'|;", pattern):
if match.group() == ';':
pos_pattern = pattern[:match.start()]
neg_pattern = pattern[match.end():]
break

if neg_pattern is not None:
pos_prefix, number, pos_suffix = _match_number(pos_pattern)
neg_prefix, _, neg_suffix = _match_number(neg_pattern)
else:
Expand Down
18 changes: 18 additions & 0 deletions tests/test_numbers.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,24 @@ def test_single_quotes_in_pattern():
assert numbers.format_decimal(12, "'#'0 o''clock", locale='en') == "#12 o'clock"


@pytest.mark.parametrize(
('pattern', 'positive', 'negative'),
[
("0';'", '12;', '-12;'),
("';'0", ';12', '-;12'),
("0'items; total'", '12items; total', '-12items; total'),
("0';';(0';')", '12;', '(12;)'),
("';'0;';-'0", ';12', ';-12'),
("0''';'", "12';", "-12';"),
("0'';(0'')", "12'", "(12')"),
('0;(0)', '12', '(12)'),
],
)
def test_quoted_semicolons_in_pattern(pattern, positive, negative):
assert numbers.format_decimal(12, pattern, locale='en') == positive
assert numbers.format_decimal(-12, pattern, locale='en') == negative


def test_format_currency_with_none_locale_with_default(monkeypatch):
"""Test that the default locale is used when locale is None."""
monkeypatch.setattr(numbers, "LC_MONETARY", "fi_FI")
Expand Down

Back | FazBrowse Home | New Git URL