Opened 10 years ago
Closed 10 years ago
#24382 closed Cleanup/optimization (fixed)
django.utils.numberformat should be UTF-8 safe
Reported by: | Jacob Rief | Owned by: | alainivars |
---|---|---|---|
Component: | Utilities | Version: | 1.8alpha1 |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Ready for checkin | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | yes | UI/UX: | no |
Description
By adding
from __future__ import unicode_literals
to django/utils/numberformat.py
, one could write formatters for numbers in charsets other than ASCII, for instance code block U+2150 through U+218F, which contains the representation of Roman numbers in UTF-8.
Change History (16)
comment:1 by , 10 years ago
Component: | Core (Other) → Utilities |
---|---|
Needs tests: | set |
Triage Stage: | Unreviewed → Accepted |
Type: | Uncategorized → Cleanup/optimization |
comment:2 by , 10 years ago
Most other files in folder django/utils.py are UTF-8 safe. Therefore I assume that this line simply has been forgotten - unless there is another good reason.
Before writing a unit test - I don't know where to start from - I'd first like to ask, if this patch will be accepted.
comment:3 by , 10 years ago
I'm sure it will. As for tests, see tests/utils_tests/test_numberformat.py
.
comment:4 by , 10 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
comment:5 by , 10 years ago
Owner: | removed |
---|---|
Status: | assigned → new |
comment:6 by , 10 years ago
Owner: | set to |
---|---|
Status: | new → assigned |
comment:7 by , 10 years ago
Owner: | removed |
---|---|
Status: | assigned → new |
comment:8 by , 10 years ago
Owner: | set to |
---|---|
Status: | new → assigned |
comment:9 by , 10 years ago
Resolution: | → needsinfo |
---|---|
Status: | assigned → closed |
I try to work on this ticket but I missing information about the actual problem. I try to impose unicode string as number
, decimal_sep
and thousand_sep
but none of them failed. Can you give additional information what you are trying to accomplish.
comment:10 by , 10 years ago
My concrete problem is an implementation of a class derived from Decimal
: https://github.com/jrief/django-shop/blob/0.3.0.dev/shop/money/money_maker.py
This class overrides the __format__
method, so that Django's numberformat
can render instances of that type in the current locale. But my class also uses unicode symbols, for instance the € symbol. Therefore strings in numberformat
shall be mixable with unicode strings from other modules.
You can also see it from another perspective.
Most Python files in Django contain this line:
from __future__ import unicode_literals
without that line, unicode literals only work with Python-3. By not adding this, Django intentionally behaves differently in Py-2 and Py-3.
comment:11 by , 10 years ago
Resolution: | needsinfo |
---|---|
Status: | closed → new |
comment:12 by , 10 years ago
It would still be nice to have some code with an error traceback to be able to reproduce the failure and build a test.
comment:13 by , 10 years ago
Well, without making numberformat.py
UTF-8 safe, I get this error:
str_number = '{:f}'.format(number) UnicodeEncodeError: 'ascii' codec can't encode character u'\u20ac' in position 0: ordinal not in range(128)
in https://github.com/django/django/blob/stable/1.8.x/django/utils/numberformat.py#L28
You can emulate this by implementing a special Decimal class, say
# -*- coding: utf-8 -*- from __future__ import unicode_literals from decimal import Decimal class EuroDecimal(Decimal): """ Wrapper for Decimal which prefixes each amount with the € symbol. """ def __format__(self, specifier, context=None, _localeconv=None): amount = Decimal.__format__(self, specifier, context, _localeconv) return '€ {}'.format(amount) # print amount in Euros using the current locale from django.utils.formats import number_format price = EuroDecimal('1.23') print number_format(price)
comment:15 by , 10 years ago
Triage Stage: | Accepted → Ready for checkin |
---|
comment:16 by , 10 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
Patch with tests welcome!