summaryrefslogtreecommitdiff
path: root/src/zope
diff options
context:
space:
mode:
authorStephan Richter <stephan.richter@gmail.com>2007-06-25 22:25:44 +0000
committerStephan Richter <stephan.richter@gmail.com>2007-06-25 22:25:44 +0000
commitf6a0a6b33dd98f92f5dbbea5db8f45286b0bd9e5 (patch)
tree7ad949d76d035fc69d11f07f19286a5728481b32 /src/zope
parenta3968d3e68f2779ede2f4993e233964af7f15065 (diff)
downloadzope-i18n-f6a0a6b33dd98f92f5dbbea5db8f45286b0bd9e5.tar.gz
Provided hook, so that numbers can be parsed into custom types, such as
decimal.
Diffstat (limited to 'src/zope')
-rw-r--r--src/zope/i18n/format.py4
-rw-r--r--src/zope/i18n/interfaces/__init__.py10
-rw-r--r--src/zope/i18n/tests/test_formats.py9
3 files changed, 22 insertions, 1 deletions
diff --git a/src/zope/i18n/format.py b/src/zope/i18n/format.py
index a7e7b7a..1c291d3 100644
--- a/src/zope/i18n/format.py
+++ b/src/zope/i18n/format.py
@@ -205,6 +205,8 @@ class NumberFormat(object):
implements(INumberFormat)
+ type = None
+
def __init__(self, pattern=None, symbols={}):
# setup default symbols
self.symbols = {
@@ -299,6 +301,8 @@ class NumberFormat(object):
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):
diff --git a/src/zope/i18n/interfaces/__init__.py b/src/zope/i18n/interfaces/__init__.py
index 494403a..91b3ea1 100644
--- a/src/zope/i18n/interfaces/__init__.py
+++ b/src/zope/i18n/interfaces/__init__.py
@@ -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 @@ class INumberFormat(IFormat):
' 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(
diff --git a/src/zope/i18n/tests/test_formats.py b/src/zope/i18n/tests/test_formats.py
index b820abe..27d0bd4 100644
--- a/src/zope/i18n/tests/test_formats.py
+++ b/src/zope/i18n/tests/test_formats.py
@@ -15,6 +15,7 @@
$Id$
"""
+import decimal
import os
import datetime
import pytz
@@ -980,6 +981,14 @@ class TestNumberFormat(TestCase):
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')