[Checkins] SVN: zope.i18n/trunk/src/zope/i18n/ Provided hook,
so that numbers can be parsed into custom types, such as
Stephan Richter
srichter at cosmos.phy.tufts.edu
Mon Jun 25 18:25:44 EDT 2007
Log message for revision 77080:
Provided hook, so that numbers can be parsed into custom types, such as
decimal.
Changed:
U zope.i18n/trunk/src/zope/i18n/format.py
U zope.i18n/trunk/src/zope/i18n/interfaces/__init__.py
U zope.i18n/trunk/src/zope/i18n/tests/test_formats.py
-=-
Modified: zope.i18n/trunk/src/zope/i18n/format.py
===================================================================
--- zope.i18n/trunk/src/zope/i18n/format.py 2007-06-25 20:42:25 UTC (rev 77079)
+++ zope.i18n/trunk/src/zope/i18n/format.py 2007-06-25 22:25:44 UTC (rev 77080)
@@ -205,6 +205,8 @@
implements(INumberFormat)
+ type = None
+
def __init__(self, pattern=None, symbols={}):
# setup default symbols
self.symbols = {
@@ -299,6 +301,8 @@
if self.symbols['exponential'] in num_str:
type = float
num_str = num_str.replace(self.symbols['exponential'], 'E')
+ if self.type:
+ type = self.type
return sign*type(num_str)
def _format_integer(self, integer, pattern):
Modified: zope.i18n/trunk/src/zope/i18n/interfaces/__init__.py
===================================================================
--- zope.i18n/trunk/src/zope/i18n/interfaces/__init__.py 2007-06-25 20:42:25 UTC (rev 77079)
+++ zope.i18n/trunk/src/zope/i18n/interfaces/__init__.py 2007-06-25 22:25:44 UTC (rev 77080)
@@ -16,7 +16,7 @@
$Id$
"""
from zope.interface import Interface, Attribute
-from zope.schema import TextLine, Dict, Choice
+from zope.schema import TextLine, Dict, Choice, Field
class II18nAware(Interface):
@@ -351,6 +351,14 @@
' Used to quote special characters in a prefix or suffix
"""
+ type = Field(
+ title=u'Type',
+ description=(u'The type into which a string is parsed. If ``None``, '
+ u'then ``int`` will be used for whole numbers and '
+ u'``float`` for decimals.'),
+ default=None,
+ required=False)
+
symbols = Dict(
title=u"Number Symbols",
key_type=Choice(
Modified: zope.i18n/trunk/src/zope/i18n/tests/test_formats.py
===================================================================
--- zope.i18n/trunk/src/zope/i18n/tests/test_formats.py 2007-06-25 20:42:25 UTC (rev 77079)
+++ zope.i18n/trunk/src/zope/i18n/tests/test_formats.py 2007-06-25 22:25:44 UTC (rev 77080)
@@ -15,6 +15,7 @@
$Id$
"""
+import decimal
import os
import datetime
import pytz
@@ -980,6 +981,14 @@
symbols={'decimal': '.', 'group': ',', 'exponential': 'X'})
self.assertEqual(format.parse('1.2X11', '#.#E0'), 1.2e11)
+ def testChangeOutputType(self):
+ format = NumberFormat()
+ format.type = decimal.Decimal
+ self.assertEqual(format.parse('23341', '###0'),
+ decimal.Decimal('23341'))
+ self.assertEqual(format.parse('233.41', '###0.00'),
+ decimal.Decimal('233.41'))
+
def testFormatSimpleInteger(self):
self.assertEqual(self.format.format(23341, '###0'),
'23341')
More information about the Checkins
mailing list