[Assorted-commits] SF.net SVN: assorted:[1017] python-commons/trunk/src/commons/strs.py
Brought to you by:
yangzhang
From: <yan...@us...> - 2008-10-14 03:34:51
|
Revision: 1017 http://assorted.svn.sourceforge.net/assorted/?rev=1017&view=rev Author: yangzhang Date: 2008-10-14 03:34:36 +0000 (Tue, 14 Oct 2008) Log Message: ----------- added remove_empty_lines, nat_lang_join, or_join, and_join, unit tests for nat_lang_join et al. Modified Paths: -------------- python-commons/trunk/src/commons/strs.py Modified: python-commons/trunk/src/commons/strs.py =================================================================== --- python-commons/trunk/src/commons/strs.py 2008-10-14 03:32:58 UTC (rev 1016) +++ python-commons/trunk/src/commons/strs.py 2008-10-14 03:34:36 UTC (rev 1017) @@ -6,18 +6,22 @@ """ __all__ = ''' +and_join +cp1252_to_unicode +cp1252_to_unicode_translations +dos2unix format +indent +nat_lang_join +or_join +remove_empty_lines safe_ascii -cp1252_to_unicode_translations -cp1252_to_unicode -unwrap -indent underline -dos2unix unicode2html +unwrap '''.split() -import itertools, cgi, re +import itertools, cgi, re, unittest def format( *args ): """Formats the args as they would be by the C{print} built-in.""" @@ -86,6 +90,27 @@ if isinstance(s, str): s = s.split('\n') return '\n'.join( ind + line for line in s ) +def unindent(text, amt = None): + """ + If L{amt} is 0, removes all leading whitespace from each line in L{text}. + If L{amt} is L{None}, finds the smallest amount of leading whitespace on + any non-empty line and removes that many chars from each line. If L{amt} + is positive, removes L{amt} chars from each line. + """ + lines = text.split('\n') + if amt == 0: + return '\n'.join( line.lstrip() for line in lines ) + def count_indent(line): + for i,c in enumerate(line): + if not c.isspace(): return i + if amt is None: + amt = min( count_indent(line) for line in lines if line.strip() != '' ) + return '\n'.join( line[amt:] for line in lines ) + +def remove_empty_lines(s): + "Removes all empty lines (or lines of just whitespace)." + return '\n'.join( line for line in s.split('\n') if line.strip() != '' ) + def underline(s, sep): """ Appends to L{s} a newline and a number of repetitions of L{sep}; the number @@ -101,3 +126,41 @@ def unicode2html(s): "Extends cgi.escape() with escapes for all unicode characters." return pat.sub(lambda m: '&#%d;' % ord(m.group()), cgi.escape(s)) + +def nat_lang_join(xs, last_glue, two_glue = None, glue = ', '): + """ + Natural-language join. Join a sequence of strings together into a + comma-separated list, but where the last pair is joined with the given + special glue. (You may also override the non-last glue, which defaults to + a ', '.) + + @param xs: The sequence of strings. This must be a list-like sequence, not + a generated one. + + @param last_glue: The string used to join the final pair of elements, when + there are more than two elements. + + @param two_glue: The string used to join both elements in a 2-element + sequence. Defaults to None, which means to use last_glue. + + @param glue: The string used to join all the other elements. + """ + if len(xs) == 0: return '' + elif len(xs) == 1: return xs[0] + elif len(xs) == 2: return xs[0] + two_glue + xs[1] + else: return glue.join(xs[:-1]) + last_glue + xs[-1] + +def or_join(xs): return nat_lang_join(xs, ', or ', ' or ') +def and_join(xs): return nat_lang_join(xs, ', and ', ' and ') + +class str_test( unittest.TestCase ): + def test_nat_lang_join( self ): + self.assertEqual( nat_lang_join( 'alpha beta gamma'.split(), ' | ' ), + 'alpha, beta | gamma' ) + self.assertEqual( and_join( 'alpha beta gamma'.split() ), + 'alpha, beta, and gamma' ) + self.assertEqual( or_join( 'alpha beta'.split() ), + 'alpha or beta' ) + +if __name__ == '__main__': + unittest.main() This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |