Show More
@@ -80,13 +80,13 b' def eval_formatter_check(f):' | |||||
80 | nt.assert_equal(s, ns['stuff']) |
|
80 | nt.assert_equal(s, ns['stuff']) | |
81 | s = f.format("{stuff!r}", **ns) |
|
81 | s = f.format("{stuff!r}", **ns) | |
82 | nt.assert_equal(s, repr(ns['stuff'])) |
|
82 | nt.assert_equal(s, repr(ns['stuff'])) | |
83 |
|
83 | |||
84 | # Check with unicode: |
|
84 | # Check with unicode: | |
85 | s = f.format("{u}", **ns) |
|
85 | s = f.format("{u}", **ns) | |
86 | nt.assert_equal(s, ns['u']) |
|
86 | nt.assert_equal(s, ns['u']) | |
87 | # This decodes in a platform dependent manner, but it shouldn't error out |
|
87 | # This decodes in a platform dependent manner, but it shouldn't error out | |
88 | s = f.format("{b}", **ns) |
|
88 | s = f.format("{b}", **ns) | |
89 |
|
89 | |||
90 | nt.assert_raises(NameError, f.format, '{dne}', **ns) |
|
90 | nt.assert_raises(NameError, f.format, '{dne}', **ns) | |
91 |
|
91 | |||
92 | def eval_formatter_slicing_check(f): |
|
92 | def eval_formatter_slicing_check(f): | |
@@ -97,18 +97,18 b' def eval_formatter_slicing_check(f):' | |||||
97 | nt.assert_equal(s, " ['there', 'hello'] ") |
|
97 | nt.assert_equal(s, " ['there', 'hello'] ") | |
98 | s = f.format("{stuff[::2]}", **ns) |
|
98 | s = f.format("{stuff[::2]}", **ns) | |
99 | nt.assert_equal(s, ns['stuff'][::2]) |
|
99 | nt.assert_equal(s, ns['stuff'][::2]) | |
100 |
|
100 | |||
101 | nt.assert_raises(SyntaxError, f.format, "{n:x}", **ns) |
|
101 | nt.assert_raises(SyntaxError, f.format, "{n:x}", **ns) | |
102 |
|
102 | |||
103 | def eval_formatter_no_slicing_check(f): |
|
103 | def eval_formatter_no_slicing_check(f): | |
104 | ns = dict(n=12, pi=math.pi, stuff='hello there', os=os) |
|
104 | ns = dict(n=12, pi=math.pi, stuff='hello there', os=os) | |
105 |
|
105 | |||
106 | s = f.format('{n:x} {pi**2:+f}', **ns) |
|
106 | s = f.format('{n:x} {pi**2:+f}', **ns) | |
107 | nt.assert_equal(s, "c +9.869604") |
|
107 | nt.assert_equal(s, "c +9.869604") | |
108 |
|
108 | |||
109 | s = f.format('{stuff[slice(1,4)]}', **ns) |
|
109 | s = f.format('{stuff[slice(1,4)]}', **ns) | |
110 | nt.assert_equal(s, 'ell') |
|
110 | nt.assert_equal(s, 'ell') | |
111 |
|
111 | |||
112 | if sys.version_info >= (3, 4): |
|
112 | if sys.version_info >= (3, 4): | |
113 | # String formatting has changed in Python 3.4, so this now works. |
|
113 | # String formatting has changed in Python 3.4, so this now works. | |
114 | s = f.format("{a[:]}", a=[1, 2]) |
|
114 | s = f.format("{a[:]}", a=[1, 2]) | |
@@ -130,7 +130,7 b' def test_dollar_formatter():' | |||||
130 | f = text.DollarFormatter() |
|
130 | f = text.DollarFormatter() | |
131 | eval_formatter_check(f) |
|
131 | eval_formatter_check(f) | |
132 | eval_formatter_slicing_check(f) |
|
132 | eval_formatter_slicing_check(f) | |
133 |
|
133 | |||
134 | ns = dict(n=12, pi=math.pi, stuff='hello there', os=os) |
|
134 | ns = dict(n=12, pi=math.pi, stuff='hello there', os=os) | |
135 | s = f.format("$n", **ns) |
|
135 | s = f.format("$n", **ns) | |
136 | nt.assert_equal(s, "12") |
|
136 | nt.assert_equal(s, "12") | |
@@ -161,12 +161,12 b' def test_strip_email():' | |||||
161 | src = """\ |
|
161 | src = """\ | |
162 | >> >>> def f(x): |
|
162 | >> >>> def f(x): | |
163 | >> ... return x+1 |
|
163 | >> ... return x+1 | |
164 | >> ... |
|
164 | >> ... | |
165 | >> >>> zz = f(2.5)""" |
|
165 | >> >>> zz = f(2.5)""" | |
166 | cln = """\ |
|
166 | cln = """\ | |
167 | >>> def f(x): |
|
167 | >>> def f(x): | |
168 | ... return x+1 |
|
168 | ... return x+1 | |
169 | ... |
|
169 | ... | |
170 | >>> zz = f(2.5)""" |
|
170 | >>> zz = f(2.5)""" | |
171 | nt.assert_equal(text.strip_email_quotes(src), cln) |
|
171 | nt.assert_equal(text.strip_email_quotes(src), cln) | |
172 |
|
172 | |||
@@ -187,7 +187,4 b' def test_SList():' | |||||
187 | nt.assert_equal(sl.s, 'a 11 b 1 a 2') |
|
187 | nt.assert_equal(sl.s, 'a 11 b 1 a 2') | |
188 | nt.assert_equal(sl.grep(lambda x: x.startswith('a')), text.SList(['a 11', 'a 2'])) |
|
188 | nt.assert_equal(sl.grep(lambda x: x.startswith('a')), text.SList(['a 11', 'a 2'])) | |
189 | nt.assert_equal(sl.fields(0), text.SList(['a', 'b', 'a'])) |
|
189 | nt.assert_equal(sl.fields(0), text.SList(['a', 'b', 'a'])) | |
190 |
nt.assert_equal(sl.sort(field=1, nums=True), text.SList(['b 1', 'a 2', 'a 11'])) |
|
190 | nt.assert_equal(sl.sort(field=1, nums=True), text.SList(['b 1', 'a 2', 'a 11'])) No newline at end of file | |
191 |
|
||||
192 | def test_non_local_path_import(): |
|
|||
193 | nt.assert_in("path", sys.modules) |
|
@@ -7,7 +7,6 b' Inheritance diagram:' | |||||
7 | .. inheritance-diagram:: IPython.utils.text |
|
7 | .. inheritance-diagram:: IPython.utils.text | |
8 | :parts: 3 |
|
8 | :parts: 3 | |
9 | """ |
|
9 | """ | |
10 | from __future__ import absolute_import |
|
|||
11 |
|
10 | |||
12 | import os |
|
11 | import os | |
13 | import re |
|
12 | import re | |
@@ -412,7 +411,7 b' def wrap_paragraphs(text, ncols=80):' | |||||
412 |
|
411 | |||
413 | def long_substr(data): |
|
412 | def long_substr(data): | |
414 | """Return the longest common substring in a list of strings. |
|
413 | """Return the longest common substring in a list of strings. | |
415 |
|
414 | |||
416 | Credit: http://stackoverflow.com/questions/2892931/longest-common-substring-from-more-than-two-strings-python |
|
415 | Credit: http://stackoverflow.com/questions/2892931/longest-common-substring-from-more-than-two-strings-python | |
417 | """ |
|
416 | """ | |
418 | substr = '' |
|
417 | substr = '' | |
@@ -454,7 +453,7 b' def strip_email_quotes(text):' | |||||
454 |
|
453 | |||
455 | So if any line has no quote marks ('>') , then none are stripped from any |
|
454 | So if any line has no quote marks ('>') , then none are stripped from any | |
456 | of them :: |
|
455 | of them :: | |
457 |
|
456 | |||
458 | In [5]: strip_email_quotes('> > text\\n> > more\\nlast different') |
|
457 | In [5]: strip_email_quotes('> > text\\n> > more\\nlast different') | |
459 | Out[5]: '> > text\\n> > more\\nlast different' |
|
458 | Out[5]: '> > text\\n> > more\\nlast different' | |
460 | """ |
|
459 | """ | |
@@ -476,7 +475,7 b' def strip_email_quotes(text):' | |||||
476 | def strip_ansi(source): |
|
475 | def strip_ansi(source): | |
477 | """ |
|
476 | """ | |
478 | Remove ansi escape codes from text. |
|
477 | Remove ansi escape codes from text. | |
479 |
|
478 | |||
480 | Parameters |
|
479 | Parameters | |
481 | ---------- |
|
480 | ---------- | |
482 | source : str |
|
481 | source : str | |
@@ -487,11 +486,11 b' def strip_ansi(source):' | |||||
487 |
|
486 | |||
488 | class EvalFormatter(Formatter): |
|
487 | class EvalFormatter(Formatter): | |
489 | """A String Formatter that allows evaluation of simple expressions. |
|
488 | """A String Formatter that allows evaluation of simple expressions. | |
490 |
|
489 | |||
491 | Note that this version interprets a : as specifying a format string (as per |
|
490 | Note that this version interprets a : as specifying a format string (as per | |
492 | standard string formatting), so if slicing is required, you must explicitly |
|
491 | standard string formatting), so if slicing is required, you must explicitly | |
493 | create a slice. |
|
492 | create a slice. | |
494 |
|
493 | |||
495 | This is to be used in templating cases, such as the parallel batch |
|
494 | This is to be used in templating cases, such as the parallel batch | |
496 | script templates, where simple arithmetic on arguments is useful. |
|
495 | script templates, where simple arithmetic on arguments is useful. | |
497 |
|
496 | |||
@@ -517,13 +516,13 b' class EvalFormatter(Formatter):' | |||||
517 | @skip_doctest_py3 |
|
516 | @skip_doctest_py3 | |
518 | class FullEvalFormatter(Formatter): |
|
517 | class FullEvalFormatter(Formatter): | |
519 | """A String Formatter that allows evaluation of simple expressions. |
|
518 | """A String Formatter that allows evaluation of simple expressions. | |
520 |
|
519 | |||
521 | Any time a format key is not found in the kwargs, |
|
520 | Any time a format key is not found in the kwargs, | |
522 | it will be tried as an expression in the kwargs namespace. |
|
521 | it will be tried as an expression in the kwargs namespace. | |
523 |
|
522 | |||
524 | Note that this version allows slicing using [1:2], so you cannot specify |
|
523 | Note that this version allows slicing using [1:2], so you cannot specify | |
525 | a format string. Use :class:`EvalFormatter` to permit format strings. |
|
524 | a format string. Use :class:`EvalFormatter` to permit format strings. | |
526 |
|
525 | |||
527 | Examples |
|
526 | Examples | |
528 | -------- |
|
527 | -------- | |
529 | :: |
|
528 | :: | |
@@ -597,7 +596,7 b' class DollarFormatter(FullEvalFormatter):' | |||||
597 | def parse(self, fmt_string): |
|
596 | def parse(self, fmt_string): | |
598 | for literal_txt, field_name, format_spec, conversion \ |
|
597 | for literal_txt, field_name, format_spec, conversion \ | |
599 | in Formatter.parse(self, fmt_string): |
|
598 | in Formatter.parse(self, fmt_string): | |
600 |
|
599 | |||
601 | # Find $foo patterns in the literal text. |
|
600 | # Find $foo patterns in the literal text. | |
602 | continue_from = 0 |
|
601 | continue_from = 0 | |
603 | txt = "" |
|
602 | txt = "" | |
@@ -610,7 +609,7 b' class DollarFormatter(FullEvalFormatter):' | |||||
610 | yield (txt + new_txt, new_field, "", None) |
|
609 | yield (txt + new_txt, new_field, "", None) | |
611 | txt = "" |
|
610 | txt = "" | |
612 | continue_from = m.end() |
|
611 | continue_from = m.end() | |
613 |
|
612 | |||
614 | # Re-yield the {foo} style pattern |
|
613 | # Re-yield the {foo} style pattern | |
615 | yield (txt + literal_txt[continue_from:], field_name, format_spec, conversion) |
|
614 | yield (txt + literal_txt[continue_from:], field_name, format_spec, conversion) | |
616 |
|
615 | |||
@@ -762,4 +761,4 b' def get_text_list(list_, last_sep=\' and \', sep=", ", wrap_item_with=""):' | |||||
762 | return list_[0] |
|
761 | return list_[0] | |
763 | return '%s%s%s' % ( |
|
762 | return '%s%s%s' % ( | |
764 | sep.join(i for i in list_[:-1]), |
|
763 | sep.join(i for i in list_[:-1]), | |
765 |
last_sep, list_[-1]) |
|
764 | last_sep, list_[-1]) No newline at end of file |
General Comments 0
You need to be logged in to leave comments.
Login now