Show More
@@ -138,3 +138,17 b' def test_dollar_formatter():' | |||
|
138 | 138 | nt.assert_equals(s, "12 $HOME") |
|
139 | 139 | s = f.format("${foo}", foo="HOME") |
|
140 | 140 | nt.assert_equals(s, "$HOME") |
|
141 | ||
|
142 | ||
|
143 | def test_strip_email(): | |
|
144 | src = """\ | |
|
145 | >> >>> def f(x): | |
|
146 | >> ... return x+1 | |
|
147 | >> ... | |
|
148 | >> >>> zz = f(2.5)""" | |
|
149 | cln = """\ | |
|
150 | >>> def f(x): | |
|
151 | ... return x+1 | |
|
152 | ... | |
|
153 | >>> zz = f(2.5)""" | |
|
154 | nt.assert_equals(text.strip_email_quotes(src), cln) |
@@ -488,6 +488,7 b' def format_screen(strng):' | |||
|
488 | 488 | strng = par_re.sub('',strng) |
|
489 | 489 | return strng |
|
490 | 490 | |
|
491 | ||
|
491 | 492 | def dedent(text): |
|
492 | 493 | """Equivalent of textwrap.dedent that ignores unindented first line. |
|
493 | 494 | |
@@ -514,6 +515,7 b' def dedent(text):' | |||
|
514 | 515 | rest = textwrap.dedent(rest) |
|
515 | 516 | return '\n'.join([first, rest]) |
|
516 | 517 | |
|
518 | ||
|
517 | 519 | def wrap_paragraphs(text, ncols=80): |
|
518 | 520 | """Wrap multiple paragraphs to fit a specified width. |
|
519 | 521 | |
@@ -540,6 +542,39 b' def wrap_paragraphs(text, ncols=80):' | |||
|
540 | 542 | return out_ps |
|
541 | 543 | |
|
542 | 544 | |
|
545 | def long_substr(data): | |
|
546 | """Return the longest common substring in a list of strings. | |
|
547 | ||
|
548 | Credit: http://stackoverflow.com/questions/2892931/longest-common-substring-from-more-than-two-strings-python | |
|
549 | """ | |
|
550 | substr = '' | |
|
551 | if len(data) > 1 and len(data[0]) > 0: | |
|
552 | for i in range(len(data[0])): | |
|
553 | for j in range(len(data[0])-i+1): | |
|
554 | if j > len(substr) and all(data[0][i:i+j] in x for x in data): | |
|
555 | substr = data[0][i:i+j] | |
|
556 | return substr | |
|
557 | ||
|
558 | ||
|
559 | def strip_email_quotes(text): | |
|
560 | """Strip leading email quotation characters ('>'). | |
|
561 | """ | |
|
562 | lines = text.splitlines() | |
|
563 | matches = set() | |
|
564 | for line in lines: | |
|
565 | prefix = re.match(r'^(\s*>[ >]*)', line) | |
|
566 | if prefix: | |
|
567 | matches.add(prefix.group(1)) | |
|
568 | else: | |
|
569 | break | |
|
570 | else: | |
|
571 | prefix = long_substr(list(matches)) | |
|
572 | if prefix: | |
|
573 | strip = len(prefix) | |
|
574 | text = '\n'.join([ ln[strip:] for ln in lines]) | |
|
575 | return text | |
|
576 | ||
|
577 | ||
|
543 | 578 | class EvalFormatter(Formatter): |
|
544 | 579 | """A String Formatter that allows evaluation of simple expressions. |
|
545 | 580 |
General Comments 0
You need to be logged in to leave comments.
Login now