[Assorted-commits] SF.net SVN: assorted:[982] python-commons/trunk/src/commons/strs.py
Brought to you by:
yangzhang
From: <yan...@us...> - 2008-10-02 19:15:09
|
Revision: 982 http://assorted.svn.sourceforge.net/assorted/?rev=982&view=rev Author: yangzhang Date: 2008-10-02 19:14:50 +0000 (Thu, 02 Oct 2008) Log Message: ----------- added indent, underline, dos2unix, unicode2html 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-02 19:14:28 UTC (rev 981) +++ python-commons/trunk/src/commons/strs.py 2008-10-02 19:14:50 UTC (rev 982) @@ -5,7 +5,7 @@ String formatting. """ -import itertools +import itertools, cgi, re def format( *args ): """Formats the args as they would be by the C{print} built-in.""" @@ -64,3 +64,28 @@ """ if isinstance(s, str): s = s.strip().split('\n') return ' '.join( line.strip() for line in s ) + +def indent(s, ind = ' '): + """ + Prefixes each line in L{s} with L{ind}. L{s} can be either a string (which + will be broken up into a list of lines) or a list of strings (treated as + lines). Returns a single (indented) string. + """ + if isinstance(s, str): s = s.split('\n') + return '\n'.join( ind + line for line in s ) + +def underline(s, sep): + """ + Appends to L{s} a newline and a number of repetitions of L{sep}; the number + of repetitions is the length of L{s}. + """ + return s + '\n' + (sep * len(s)) + +def dos2unix(s): + "Removes carriage returns." + return s.replace('\r','') + +pat = re.compile(u'[\u0080-\uffff]') +def unicode2html(s): + "Extends cgi.escape() with escapes for all unicode characters." + return pat.sub(lambda m: '&#%d;' % ord(m.group()), cgi.escape(s)) This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |