Skip to content
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

Add unit and compound unit formatting #369

Merged
merged 4 commits into from
Apr 7, 2016
Merged

Conversation

akx
Copy link
Member

@akx akx commented Mar 19, 2016

No description provided.

@codecov-io
Copy link

Current coverage is 89.90%

Merging #369 into master will increase coverage by +0.15% as of 0a25c9d

@@            master   #369   diff @@
=====================================
  Files           23     24     +1
  Stmts         3854   3921    +67
  Branches         0      0       
  Methods          0      0       
=====================================
+ Hit           3459   3525    +66
  Partial          0      0       
- Missed         395    396     +1

Review entire Coverage Diff as of 0a25c9d

Powered by Codecov. Updated on successful CI builds.


for unit in elem.findall('compoundUnit'):
unit_type = unit.attrib['type']
box = "%s:%s" % (unit_type, unit_length_type)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder why the original implementation uses string concatenation instead of nested dicts (eg date formats)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Commit 5204709 introduced the colons in the first place, but for alternative forms.
Then 9327e08 reused colons/string concat for lengths (misguidedly, perhaps).

I guess "for no particular reason" is the answer :)

@@ -0,0 +1,239 @@
# -- encoding: UTF-8 --

from babel._compat import Decimal, string_types

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file contains unused source code.

PyUnusedCodeBear, severity NORMAL, section default.

The issue can be fixed by applying the following patch:

--- a/babel/units.py
+++ b/babel/units.py
@@ -1,6 +1,7 @@
 # -- encoding: UTF-8 --

-from babel._compat import Decimal, string_types
+from babel._compat import Decimal
+from babel._compat import string_types
 from babel.core import Locale
 from babel.numbers import format_decimal, LC_NUMERIC

akx added 3 commits April 6, 2016 00:22
(Instead of colon-separated flat dicts.)

This should not be a breaking change, unless someone has used the private
API `l._data["unit_patterns"]`.
* babel.units.format_unit
* babel.units.format_compound_unit
* babel.units.get_unit_name
@akx akx force-pushed the unit_format branch 2 times, most recently from c9dc208 to ca9dede Compare April 5, 2016 21:54
unit_patterns = locale._data["unit_patterns"][q_unit].get(length, {})
formatted_value = format_decimal(value, format, locale)
plural_form = locale.plural_form(value)
if plural_form in unit_patterns:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CLDR here casually mentions length fallbacks (at least that's how I interpreted it here). It'd be awesome to see it here, I don't think it would be that difficult to add an extra iteration through a hardcoded, ordered list of length strings. If it's gross, feel free to punt.

http://www.unicode.org/reports/tr35/tr35-general.html#perUnitPatterns

@jtwang
Copy link
Contributor

jtwang commented Apr 5, 2016

Ow, my head. Yours must hurt more. ;)

@jtwang
Copy link
Contributor

jtwang commented Apr 7, 2016

Looks good, push!

@akx
Copy link
Member Author

akx commented Apr 7, 2016

Ignoring the single Travis failure; it's one of those split-second unluckinesses.

@akx akx merged commit 3b6a2e2 into python-babel:master Apr 7, 2016
@akx akx deleted the unit_format branch April 7, 2016 19:28
@pyup-bot pyup-bot mentioned this pull request Apr 11, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants