[pypy-commit] pypy numpy-multidim: repr and str rework

mattip noreply at buildbot.pypy.org
Sun Oct 30 22:42:42 CET 2011


Author: mattip
Branch: numpy-multidim
Changeset: r48614:723ed1ac5a3a
Date: 2011-10-30 23:40 +0200
http://bitbucket.org/pypy/pypy/changeset/723ed1ac5a3a/

Log:	repr and str rework

diff --git a/pypy/module/micronumpy/interp_numarray.py b/pypy/module/micronumpy/interp_numarray.py
--- a/pypy/module/micronumpy/interp_numarray.py
+++ b/pypy/module/micronumpy/interp_numarray.py
@@ -246,7 +246,10 @@
     def descr_repr(self, space):
         # Simple implementation so that we can see the array. Needs work.
         concrete = self.get_concrete()
-        res = "array([" + ", ".join(concrete._getnums(False)) + "]"
+        new_sig = signature.Signature.find_sig([
+            NDimSlice.signature, self.signature
+        ])
+        res = "array(" + NDimSlice(concrete, new_sig, [], self.shape[:]).tostr(True)
         dtype = concrete.find_dtype()
         if (dtype is not space.fromcache(interp_dtype.W_Float64Dtype) and
             dtype is not space.fromcache(interp_dtype.W_Int64Dtype)) or not self.find_size():
@@ -257,7 +260,10 @@
     def descr_str(self, space):
         # Simple implementation so that we can see the array. Needs work.
         concrete = self.get_concrete()
-        return space.wrap("[" + " ".join(concrete._getnums(True)) + "]")
+        new_sig = signature.Signature.find_sig([
+            NDimSlice.signature, self.signature
+        ])
+        return space.wrap(NDimSlice(concrete, new_sig, [], self.shape[:]).tostr(False))
 
     def _index_of_single_item(self, space, w_idx):
         # we assume C ordering for now
@@ -582,6 +588,7 @@
     _immutable_fields_ = ['shape[*]', 'chunks[*]']
 
     def __init__(self, parent, signature, chunks, shape):
+        print 'NDimSlice.__init__(...,',chunks,',',shape,')'
         ViewArray.__init__(self, parent, signature, shape)
         self.chunks = chunks
         self.shape_reduction = 0
@@ -628,6 +635,7 @@
     @jit.unroll_safe
     def calc_index(self, item):
         index = []
+        __item = item
         _item = item
         for i in range(len(self.shape) -1, 0, -1):
             s = self.shape[i]
@@ -654,11 +662,49 @@
             if k != 0:
                 item *= shape[k]
             k += 1
-            item += index[i]
+            try:
+                item += index[i]
+            except:
+               import pdb;pdb.set_trace()
             i += 1
         return item
-
-
+    def tostr(self, comma):
+        ret = ''
+        dtype = self.find_dtype()
+        ndims = len(self.shape)#-self.shape_reduction
+        if ndims>2:
+            ret += '['
+            for i in range(self.shape[0]):
+                res = NDimSlice(self.parent, self.signature, [(i,0,0,1)], self.shape[1:]).tostr(comma)
+                ret += '\n '.join(res.split('\n'))
+                if i+1<self.shape[0]:
+                    ret += ',\n\n'
+            ret += ']'
+        elif ndims==2:
+            ret += '['
+            for i in range(self.shape[0]):
+                ret += '['
+                ret += (','*comma + ' ' ).join([dtype.str_format(self.eval(i*self.shape[1]+j)) \
+                                                    for j in range(self.shape[1])])
+                ret += ']'
+                if i+1< self.shape[0]:
+                    ret += ',\n       '
+            ret += ']'
+        elif ndims==1:
+            ret += '['
+            if self.shape[0]>1000:
+                ret += (','*comma + ' ').join([dtype.str_format(self.eval(j)) \
+                                                    for j in range(3)])
+                ret += ','*comma + ' ..., '
+                ret += (','*comma + ' ').join([dtype.str_format(self.eval(j)) \
+                                                    for j in range(self.shape[0]-3,self.shape[0])])
+            else:
+                ret += (','*comma + ' ').join([dtype.str_format(self.eval(j)) \
+                                                    for j in range(self.shape[0])])
+            ret += ']'
+        else:
+            ret += 'shape=%s, reduction=%s'%(str(self.shape), str(self.shape_reduction))
+        return ret
 class NDimArray(BaseArray):
     def __init__(self, size, shape, dtype):
         BaseArray.__init__(self, shape)
diff --git a/pypy/module/micronumpy/test/test_numarray.py b/pypy/module/micronumpy/test/test_numarray.py
--- a/pypy/module/micronumpy/test/test_numarray.py
+++ b/pypy/module/micronumpy/test/test_numarray.py
@@ -67,6 +67,10 @@
         assert repr(a) == "array([], dtype=int64)"
         a = array([True, False, True, False], "?")
         assert repr(a) == "array([True, False, True, False], dtype=bool)"
+        a = zeros((3,4))
+        assert repr(a) == '''array([[0.0, 0.0, 0.0, 0.0],
+       [0.0, 0.0, 0.0, 0.0],
+       [0.0, 0.0, 0.0, 0.0]])'''
 
     def test_repr_slice(self):
         from numpy import array, zeros


More information about the pypy-commit mailing list