From 6433e5da3c639fe0eb3ef26565f38e6fe0144209 Mon Sep 17 00:00:00 2001 From: Wirawan Purwanto Date: Wed, 23 May 2012 00:25:42 -0400 Subject: [PATCH] * Added prefix and suffix options for matrix_str and vector_str. --- text_tools.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/text_tools.py b/text_tools.py index 86637ba..ca15fc8 100644 --- a/text_tools.py +++ b/text_tools.py @@ -40,20 +40,21 @@ def make_matrix(Str, debug=None): if debug: print rslt return numpy.array(rslt) -def vector_str(M, fmt="%22.15g", V=False): +def vector_str(M, fmt="%22.15g", V=False, prefix="", suffix=""): if len(M.shape) != 1: raise ValueError, "Wrong shape: expecting a one-dimensional array." if V: - return "\n".join([ fmt % m for m in M ]) + return prefix + (suffix + "\n" + prefix).join([ fmt % m for m in M ]) + suffix else: - return " ".join([ fmt % m for m in M ]) + return prefix + " ".join([ fmt % m for m in M ]) + suffix -def matrix_str(M, fmt="%22.15g"): +def matrix_str(M, fmt="%22.15g", prefix="", suffix=""): + linesep = suffix + "\n" + prefix if isinstance(M, numpy.matrix): M = numpy.asarray(M) if len(M.shape) != 2: raise ValueError, "Wrong shape: expecting a two-dimensional array." - return "\n".join([ " ".join([ fmt % c for c in R ] ) for R in M ]) + return prefix + linesep.join([ " ".join([ fmt % c for c in R ]) for R in M ]) + suffix def str_unindent(S, amount=None): @@ -66,7 +67,7 @@ def str_unindent(S, amount=None): nindent = amount indent_whsp = " " * nindent - strs = S.split("\n") + strs = S.splitlines() rslt = [] for s in strs: if s.strip() != "":