From 887ca7e16e93c889829260e9e40f1fc431b19871 Mon Sep 17 00:00:00 2001 From: wirawan Date: Fri, 4 Dec 2009 19:57:22 +0000 Subject: [PATCH] * text_tools: module to contain simple text utilities. --- graph_digitizer.py | 21 ++------------------- text_tools.py | 31 +++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 19 deletions(-) create mode 100644 text_tools.py diff --git a/graph_digitizer.py b/graph_digitizer.py index 713ddc1..58adb36 100644 --- a/graph_digitizer.py +++ b/graph_digitizer.py @@ -1,6 +1,6 @@ #!/usr/bin/ipython -pylab # -# \$Id: graph_digitizer.py,v 1.1 2009-12-04 19:30:26 wirawan Exp \$ +# \$Id: graph_digitizer.py,v 1.2 2009-12-04 19:57:22 wirawan Exp \$ # # Created: 20091204 # Wirawan Purwanto @@ -10,24 +10,7 @@ # import numpy - -def make_matrix(Str, debug=None): - """Simple tool to convert a string like - '''1 2 3 - 4 5 6 - 7 8 9''' - into a numpy matrix (or, actually, an array object).""" - if isinstance(Str, numpy.matrix): - return numpy.array(Str) - elif isinstance(Str, numpy.ndarray): - if len(Str.shape) == 2: - return Str.copy() - else: - raise ValueError, "Cannot make matrix out of non-2D array" - Str2 = ";".join([ row.rstrip().rstrip(";") for row in Str.split("\n") if row.strip() != "" ]) - rslt = numpy.matrix(Str2) - if debug: print rslt - return numpy.array(rslt) +from wpylib.text_tools import make_matrix def get_axis_scaler(data, axis): """Simple routine to obtain the scaling factor from pixel coordinate to diff --git a/text_tools.py b/text_tools.py new file mode 100644 index 0000000..b458605 --- /dev/null +++ b/text_tools.py @@ -0,0 +1,31 @@ +# \$Id: text_tools.py,v 1.1 2009-12-04 19:57:22 wirawan Exp \$ +# +# Created: 20091204 +# Wirawan Purwanto +# +# Simple and dirty text tools +# + +import numpy + +def make_matrix(Str, debug=None): + """Simple tool to convert a string like + '''1 2 3 + 4 5 6 + 7 8 9''' + into a numpy matrix (or, actually, an array object). + This is for convenience in programming quick scripts, much like octave matrix + format (but without the evaluation of math expressions that octave has, + of course).""" + if isinstance(Str, numpy.matrix): + return numpy.array(Str) + elif isinstance(Str, numpy.ndarray): + if len(Str.shape) == 2: + return Str.copy() + else: + raise ValueError, "Cannot make matrix out of non-2D array" + Str2 = ";".join([ row.rstrip().rstrip(";") for row in Str.split("\n") if row.strip() != "" ]) + rslt = numpy.matrix(Str2) + if debug: print rslt + return numpy.array(rslt) +