Permalink
Browse files

Formatter can now support keyword arguments, known as 'options' in th…

…e public API
  • Loading branch information...
1 parent 56354d8 commit 586f316d3d761c4a9db12c4e908e287c34e5e700 @palewire palewire committed Mar 30, 2012
Showing with 50 additions and 10 deletions.
  1. +2 −3 table_fu/__init__.py
  2. +7 −5 table_fu/formatting.py
  3. +41 −2 test.py
View
@@ -399,12 +399,11 @@ def __str__(self):
if self.table.formatting.has_key(self.column_name):
func = self.table.formatting[self.column_name].get('filter', None)
args = self.table.formatting[self.column_name].get('args', [])
-
+ kwargs = self.table.formatting[self.column_name].get('options', {})
if func:
row = self.table[self.row_num]
args = [row[arg].value for arg in args]
- return format(self.value, func, *args)
-
+ return format(self.value, func, *args, **kwargs)
return self.value
def __eq__(self, other):
View
@@ -19,7 +19,7 @@ def _saferound(value, decimal_places):
return format % f
-def ap_state(value):
+def ap_state(value, failure_string=None):
"""
Converts a state's name, postal abbreviation or FIPS to A.P. style.
@@ -32,7 +32,10 @@ def ap_state(value):
try:
return statestyle.get(value).ap
except:
- return value
+ if failure_string:
+ return failure_string
+ else:
+ return value
def capfirst(value, failure_string='N/A'):
@@ -267,11 +270,10 @@ def __init__(self):
for name, func in DEFAULT_FORMATTERS.items():
self.register(name, func)
- def __call__(self, value, func, *args):
+ def __call__(self, value, func, *args, **kwargs):
if not callable(func):
func = self._filters[func]
-
- return func(value, *args)
+ return func(value, *args, **kwargs)
def register(self, name=None, func=None):
if not func and not name:
View
43 test.py
@@ -476,7 +476,6 @@ def test(value, *args):
args = list(args)
args.insert(0, value)
return args
-
self.format.register(test)
self.assertEqual(test, self.format._filters['test'])
@@ -509,6 +508,10 @@ def test_ap_state(self):
self.format('foo', 'ap_state'),
'foo'
)
+ self.assertEqual(
+ self.format('foo', 'ap_state', failure_string='bar'),
+ 'bar'
+ )
def test_capfirst(self):
"Returns a string with only the first character capitalized"
@@ -532,6 +535,10 @@ def test_capfirst(self):
self.format(1, 'capfirst'),
'N/A'
)
+ self.assertEqual(
+ self.format(1, 'capfirst', failure_string='bar'),
+ 'bar'
+ )
def test_dollar_signs(self):
"Converts an integer into the corresponding number of dollar sign symbols."
@@ -547,7 +554,11 @@ def test_dollar_signs(self):
self.format('foo', 'dollar_signs'),
'N/A'
)
-
+ self.assertEqual(
+ self.format('foo', 'dollar_signs', failure_string='bar'),
+ 'bar'
+ )
+
def test_image(self):
"Returns an HTML image tag"
self.assertEqual(
@@ -565,10 +576,22 @@ def test_percentage(self):
self.format(0.10560, 'percentage'),
'10.6%'
)
+ self.assertEqual(
+ self.format(0.10560, 'percentage', multiply=False),
+ '0.1%'
+ )
+ self.assertEqual(
+ self.format(0.10560, 'percentage', decimal_places=3),
+ '10.560%'
+ )
self.assertEqual(
self.format('foo', 'percentage'),
'N/A'
)
+ self.assertEqual(
+ self.format('foo', 'percentage', failure_string='bar'),
+ 'bar'
+ )
def test_percent_change(self):
"Converts a floating point value into a percentage change value."
@@ -580,10 +603,22 @@ def test_percent_change(self):
self.format(-0.10560, 'percent_change'),
'-10.6%'
)
+ self.assertEqual(
+ self.format(-0.10560, 'percent_change', multiply=False),
+ '-0.1%'
+ )
+ self.assertEqual(
+ self.format(-0.10560, 'percent_change', decimal_places=3),
+ '-10.560%'
+ )
self.assertEqual(
self.format('foo', 'percent_change'),
'N/A'
)
+ self.assertEqual(
+ self.format('foo', 'percent_change', failure_string='bar'),
+ 'bar'
+ )
def test_ratio(self):
"Converts a floating point value a X:1 ratio."
@@ -603,6 +638,10 @@ def test_ratio(self):
self.format('foo', 'ratio'),
'N/A'
)
+ self.assertEqual(
+ self.format('foo', 'ratio', failure_string='bar'),
+ 'bar'
+ )
def test_stateface(self):
"Returns ProPublica stateface"

0 comments on commit 586f316

Please sign in to comment.