[Matrix-SIG] Limiting size of arrays printed by Numeric

Joe Van Andel vanandel@ucar.edu
Fri, 23 Jul 1999 16:23:52 -0600


This is a multi-part message in MIME format.
--------------009AE0FFEF300E5932AF0151
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

Roger:
Thanks for the incredibly fast response to my question.  I wanted a
module I could re-use in multiple routines, and here's my first cut:


I may add some of the features Roger had in his procedure later, but for
now, I can use Numeric Python interactively without being buried by
unwanted output.
-- 

Joe VanAndel  	          
National Center for Atmospheric Research
http://www.atd.ucar.edu/~vanandel/
Internet: vanandel@ucar.edu
--------------009AE0FFEF300E5932AF0151
Content-Type: text/plain; charset=us-ascii;
 name="NumUtil.py"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="NumUtil.py"

# smarter array printing by limiting maximum size

MaxElems = 100
import Numeric

def smartArrayRepr(a, max_line_width = None, precision = None, suppress_small = None
):
    global MaxElems
    shape = a.shape
    elems =  Numeric.product(shape)
    if elems > MaxElems:
        return 'array(%s),%s'% (shape,a.typecode())
    else:
        return Numeric.array2string(a, max_line_width, precision, suppress_small, ', ',1)

def smartArrayStr(a, max_line_width = None, precision = None, suppress_small =None ):
    global MaxElems
    shape = a.shape
    elems =  product(shape)
    if elems > MaxElems:
        return 'array(%s),%s'% (shape,a.typecode())
    else:
        return Numeric.array2string(a, max_line_width, precision, suppress_small, ', ', 1)

# replace the default with a smarter function for huge arrays
Numeric.array_repr = smartArrayRepr
Numeric.array_str = smartArrayStr

# fixup multiarray, as well.
import multiarray
multiarray.set_string_function(smartArrayStr, 0)
multiarray.set_string_function(smartArrayRepr, 1)

--------------009AE0FFEF300E5932AF0151--