From 8da8937009fa1d37f39f65e7481f466936afb88f Mon Sep 17 00:00:00 2001 From: Wirawan Purwanto Date: Mon, 7 Apr 2014 12:26:09 -0400 Subject: [PATCH] * Added function str_indent for completeness. --- text_tools.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/text_tools.py b/text_tools.py index 7f99254..b216af6 100644 --- a/text_tools.py +++ b/text_tools.py @@ -57,9 +57,23 @@ def matrix_str(M, fmt="%22.15g", prefix="", suffix=""): return prefix + linesep.join([ " ".join([ fmt % c for c in R ]) for R in M ]) + suffix +def str_indent(text, indent=" "*4): + """Indents a text block by a given prefix. + If the indent is a number 'N', a string of N white spaces are taken as + the prefix. + + In python 3, textwrap.indent can accomplish the same thing.""" + if not isinstance(indent, basestring): + # Assume this is a numeric: + indent = " " * indent + return indent + ('\n'+indent).join(x for x in str(text).splitlines()) + def str_unindent(S, amount=None): """Automatically unidents a string based on the first indentation found - on a nonempty string line. Assuming UNIX LF end-of-line.""" + on a nonempty string line. Assuming UNIX LF end-of-line. + + Note: textwrap.dedent accomplishes a similar function (but the amount of + white spaces to remove is automatically detected).""" if amount == None: nindent = -1 # autodetect, default