-
Notifications
You must be signed in to change notification settings - Fork 469
Rule based number formatting #1232
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
blagasz
wants to merge
16
commits into
python-babel:master
Choose a base branch
from
blagasz:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
79c20e7
initial commit for rework
blagasz 73c2b84
rbnf: light clean up
akx 7f15c5d
rbnf: correct radix reading
akx 78ed1b4
rbnf: make spell_number API less kwargsy
akx a5c65a7
rbnf: store divisor and substitutions in Rule to avoid recomputation
akx 50b87fc
rbnf: eagerly evaluate self.rulesets to avoid alias lookup every time
akx 1f27ca1
rbnf: replace .format & friends with f-strings
akx 802e734
rbnf: correctly dump rulesets/rules to JSON file
akx 9905dd5
Add smoke test for all RBNF-enabled locales and rulesets
akx a4692a9
remove recursion bugs, add dataclass context, add plural tokens parsing
blagasz 7a56053
add extensive smoke testing with generated TOML files for all RBNF lo…
blagasz 0cc8c13
for python versions below 3.11 skip toml based rbnf tests
blagasz 6896cf6
restore conftest
blagasz 2105a53
add tests for rbnf engine
blagasz 3ff947c
add a few negative number test cases and utility functions for test g…
blagasz bb56a15
comment out yet unused fractional parsing code to increase coverage
blagasz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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 |
|---|---|---|
|
|
@@ -28,6 +28,7 @@ | |
|
|
||
| from babel.core import Locale, default_locale, get_global | ||
| from babel.localedata import LocaleDataDict | ||
| from babel.rbnf import RuleBasedNumberFormat | ||
|
|
||
| LC_MONETARY = default_locale(('LC_MONETARY', 'LC_NUMERIC')) | ||
| LC_NUMERIC = default_locale('LC_NUMERIC') | ||
|
|
@@ -1023,6 +1024,26 @@ def __init__(self, message: str, suggestions: list[str] | None = None) -> None: | |
| SPACE_CHARS_RE = re.compile('|'.join(SPACE_CHARS)) | ||
|
|
||
|
|
||
| def spell_number(number, locale=LC_NUMERIC, ruleset=None): | ||
| """Return value spelled out for a specific locale | ||
|
|
||
| :param number: the number to format | ||
| :param locale: the `Locale` object or locale identifier | ||
| :param ruleset: the ruleset to use; defaults to regular numbers. | ||
| """ | ||
| speller = RuleBasedNumberFormat.negotiate(locale) | ||
| return speller.format(number, ruleset=ruleset) | ||
|
|
||
|
|
||
| def get_rbnf_rules(locale=LC_NUMERIC): | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure this needs to be a public API? (If it is, it also locks down the public API for RuleBasedNumberFormat.) Or... WDYT, what sort of app would consume the raw rules? |
||
| """Return all the available public rules for a specific locale | ||
|
|
||
| :param locale: the `Locale` object or locale identifier | ||
| """ | ||
| speller = RuleBasedNumberFormat.negotiate(locale) | ||
| return speller.available_rulesets | ||
|
|
||
|
|
||
| def parse_number( | ||
| string: str, | ||
| locale: Locale | str | None = None, | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you add type annotations here, please?