Skip to content
This repository was archived by the owner on Jul 12, 2021. It is now read-only.

Commit 586f316

Browse files
committed
Formatter can now support keyword arguments, known as 'options' in the public API
1 parent 56354d8 commit 586f316

3 files changed

Lines changed: 50 additions & 10 deletions

File tree

table_fu/__init__.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -399,12 +399,11 @@ def __str__(self):
399399
if self.table.formatting.has_key(self.column_name):
400400
func = self.table.formatting[self.column_name].get('filter', None)
401401
args = self.table.formatting[self.column_name].get('args', [])
402-
402+
kwargs = self.table.formatting[self.column_name].get('options', {})
403403
if func:
404404
row = self.table[self.row_num]
405405
args = [row[arg].value for arg in args]
406-
return format(self.value, func, *args)
407-
406+
return format(self.value, func, *args, **kwargs)
408407
return self.value
409408

410409
def __eq__(self, other):

table_fu/formatting.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def _saferound(value, decimal_places):
1919
return format % f
2020

2121

22-
def ap_state(value):
22+
def ap_state(value, failure_string=None):
2323
"""
2424
Converts a state's name, postal abbreviation or FIPS to A.P. style.
2525
@@ -32,7 +32,10 @@ def ap_state(value):
3232
try:
3333
return statestyle.get(value).ap
3434
except:
35-
return value
35+
if failure_string:
36+
return failure_string
37+
else:
38+
return value
3639

3740

3841
def capfirst(value, failure_string='N/A'):
@@ -267,11 +270,10 @@ def __init__(self):
267270
for name, func in DEFAULT_FORMATTERS.items():
268271
self.register(name, func)
269272

270-
def __call__(self, value, func, *args):
273+
def __call__(self, value, func, *args, **kwargs):
271274
if not callable(func):
272275
func = self._filters[func]
273-
274-
return func(value, *args)
276+
return func(value, *args, **kwargs)
275277

276278
def register(self, name=None, func=None):
277279
if not func and not name:

test.py

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,6 @@ def test(value, *args):
476476
args = list(args)
477477
args.insert(0, value)
478478
return args
479-
480479
self.format.register(test)
481480
self.assertEqual(test, self.format._filters['test'])
482481

@@ -509,6 +508,10 @@ def test_ap_state(self):
509508
self.format('foo', 'ap_state'),
510509
'foo'
511510
)
511+
self.assertEqual(
512+
self.format('foo', 'ap_state', failure_string='bar'),
513+
'bar'
514+
)
512515

513516
def test_capfirst(self):
514517
"Returns a string with only the first character capitalized"
@@ -532,6 +535,10 @@ def test_capfirst(self):
532535
self.format(1, 'capfirst'),
533536
'N/A'
534537
)
538+
self.assertEqual(
539+
self.format(1, 'capfirst', failure_string='bar'),
540+
'bar'
541+
)
535542

536543
def test_dollar_signs(self):
537544
"Converts an integer into the corresponding number of dollar sign symbols."
@@ -547,7 +554,11 @@ def test_dollar_signs(self):
547554
self.format('foo', 'dollar_signs'),
548555
'N/A'
549556
)
550-
557+
self.assertEqual(
558+
self.format('foo', 'dollar_signs', failure_string='bar'),
559+
'bar'
560+
)
561+
551562
def test_image(self):
552563
"Returns an HTML image tag"
553564
self.assertEqual(
@@ -565,10 +576,22 @@ def test_percentage(self):
565576
self.format(0.10560, 'percentage'),
566577
'10.6%'
567578
)
579+
self.assertEqual(
580+
self.format(0.10560, 'percentage', multiply=False),
581+
'0.1%'
582+
)
583+
self.assertEqual(
584+
self.format(0.10560, 'percentage', decimal_places=3),
585+
'10.560%'
586+
)
568587
self.assertEqual(
569588
self.format('foo', 'percentage'),
570589
'N/A'
571590
)
591+
self.assertEqual(
592+
self.format('foo', 'percentage', failure_string='bar'),
593+
'bar'
594+
)
572595

573596
def test_percent_change(self):
574597
"Converts a floating point value into a percentage change value."
@@ -580,10 +603,22 @@ def test_percent_change(self):
580603
self.format(-0.10560, 'percent_change'),
581604
'-10.6%'
582605
)
606+
self.assertEqual(
607+
self.format(-0.10560, 'percent_change', multiply=False),
608+
'-0.1%'
609+
)
610+
self.assertEqual(
611+
self.format(-0.10560, 'percent_change', decimal_places=3),
612+
'-10.560%'
613+
)
583614
self.assertEqual(
584615
self.format('foo', 'percent_change'),
585616
'N/A'
586617
)
618+
self.assertEqual(
619+
self.format('foo', 'percent_change', failure_string='bar'),
620+
'bar'
621+
)
587622

588623
def test_ratio(self):
589624
"Converts a floating point value a X:1 ratio."
@@ -603,6 +638,10 @@ def test_ratio(self):
603638
self.format('foo', 'ratio'),
604639
'N/A'
605640
)
641+
self.assertEqual(
642+
self.format('foo', 'ratio', failure_string='bar'),
643+
'bar'
644+
)
606645

607646
def test_stateface(self):
608647
"Returns ProPublica stateface"

0 commit comments

Comments
 (0)