##// END OF EJS Templates
Add DollarFormatter and tests.
Thomas Kluyver -
Show More
@@ -44,8 +44,7 b' def test_columnize_long():'
44 out = text.columnize(items, displaywidth=size-1)
44 out = text.columnize(items, displaywidth=size-1)
45 nt.assert_equals(out, '\n'.join(items+['']))
45 nt.assert_equals(out, '\n'.join(items+['']))
46
46
47 def test_eval_formatter():
47 def eval_formatter_check(f):
48 f = text.EvalFormatter()
49 ns = dict(n=12, pi=math.pi, stuff='hello there', os=os)
48 ns = dict(n=12, pi=math.pi, stuff='hello there', os=os)
50 s = f.format("{n} {n//4} {stuff.split()[0]}", **ns)
49 s = f.format("{n} {n//4} {stuff.split()[0]}", **ns)
51 nt.assert_equals(s, "12 3 hello")
50 nt.assert_equals(s, "12 3 hello")
@@ -60,10 +59,7 b' def test_eval_formatter():'
60
59
61 nt.assert_raises(NameError, f.format, '{dne}', **ns)
60 nt.assert_raises(NameError, f.format, '{dne}', **ns)
62
61
63
62 def eval_formatter_slicing_check(f):
64 def test_eval_formatter_slicing():
65 f = text.EvalFormatter()
66 f.allow_slicing = True
67 ns = dict(n=12, pi=math.pi, stuff='hello there', os=os)
63 ns = dict(n=12, pi=math.pi, stuff='hello there', os=os)
68 s = f.format(" {stuff.split()[:]} ", **ns)
64 s = f.format(" {stuff.split()[:]} ", **ns)
69 nt.assert_equals(s, " ['hello', 'there'] ")
65 nt.assert_equals(s, " ['hello', 'there'] ")
@@ -75,9 +71,7 b' def test_eval_formatter_slicing():'
75 nt.assert_raises(SyntaxError, f.format, "{n:x}", **ns)
71 nt.assert_raises(SyntaxError, f.format, "{n:x}", **ns)
76
72
77
73
78 def test_eval_formatter_no_slicing():
74 def eval_formatter_no_slicing_check(f):
79 f = text.EvalFormatter()
80 f.allow_slicing = False
81 ns = dict(n=12, pi=math.pi, stuff='hello there', os=os)
75 ns = dict(n=12, pi=math.pi, stuff='hello there', os=os)
82
76
83 s = f.format('{n:x} {pi**2:+f}', **ns)
77 s = f.format('{n:x} {pi**2:+f}', **ns)
@@ -85,3 +79,25 b' def test_eval_formatter_no_slicing():'
85
79
86 nt.assert_raises(SyntaxError, f.format, "{a[:]}")
80 nt.assert_raises(SyntaxError, f.format, "{a[:]}")
87
81
82 def test_eval_formatter():
83 f = text.EvalFormatter()
84 eval_formatter_check(f)
85 eval_formatter_no_slicing_check(f)
86
87 def test_full_eval_formatter():
88 f = text.FullEvalFormatter()
89 eval_formatter_check(f)
90 eval_formatter_slicing_check(f)
91
92 def test_dollar_formatter():
93 f = text.DollarFormatter()
94 eval_formatter_check(f)
95 eval_formatter_slicing_check(f)
96
97 ns = dict(n=12, pi=math.pi, stuff='hello there', os=os)
98 s = f.format("$n", **ns)
99 nt.assert_equals(s, "12")
100 s = f.format("$n.real", **ns)
101 nt.assert_equals(s, "12")
102 s = f.format("$n/{stuff[:5]}", **ns)
103 nt.assert_equals(s, "12/hello")
@@ -677,6 +677,38 b' class FullEvalFormatter(Formatter):'
677
677
678 return ''.join(result)
678 return ''.join(result)
679
679
680 class DollarFormatter(FullEvalFormatter):
681 """Formatter allowing Itpl style $foo replacement, for names and attribute
682 access only. Standard {foo} replacement also works, and allows full
683 evaluation of its arguments.
684
685 Examples
686 --------
687 In [1]: f = DollarFormatter()
688 In [2]: f.format('{n//4}', n=8)
689 Out[2]: '2'
690
691 In [3]: f.format('23 * 76 is $result', result=23*76)
692 Out[3]: '23 * 76 is 1748'
693
694 In [4]: f.format('$a or {b}', a=1, b=2)
695 Out[4]: '1 or 2'
696 """
697 _dollar_pattern = re.compile("(.*)\$([\w\.]+)")
698 def parse(self, fmt_string):
699 for literal_txt, field_name, format_spec, conversion \
700 in Formatter.parse(self, fmt_string):
701
702 # Find $foo patterns in the literal text.
703 continue_from = 0
704 for m in self._dollar_pattern.finditer(literal_txt):
705 new_txt, new_field = m.group(1,2)
706 yield (new_txt, new_field, "", "s")
707 continue_from = m.end()
708
709 # Re-yield the {foo} style pattern
710 yield (literal_txt[continue_from:], field_name, format_spec, conversion)
711
680
712
681 def columnize(items, separator=' ', displaywidth=80):
713 def columnize(items, separator=' ', displaywidth=80):
682 """ Transform a list of strings into a single string with columns.
714 """ Transform a list of strings into a single string with columns.
General Comments 0
You need to be logged in to leave comments. Login now