[pypy-commit] pypy numpy-singledim: Added simple repr and str for ViewArrays

justinpeel noreply at buildbot.pypy.org
Sat Jul 16 18:38:23 CEST 2011


Author: Justin Peel <notmuchtotell at gmail.com>
Branch: numpy-singledim
Changeset: r45673:29e15adb6915
Date: 2011-07-15 18:10 -0600
http://bitbucket.org/pypy/pypy/changeset/29e15adb6915/

Log:	Added simple repr and str for ViewArrays

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
@@ -341,10 +341,10 @@
         return space.wrap(self.find_ndim())
 
     def descr_repr(self, space):
-        return self.get_concrete().descr_repr(space)
+        return self.get_concrete()._repr(space)
 
     def descr_str(self, space):
-        return self.get_concrete().descr_str(space)
+        return self.get_concrete()._str(space)
 
     def descr_getitem(self, space, w_idx):
         # TODO: indexing by tuples and lists
@@ -548,6 +548,26 @@
     def calc_index(self, item):
         raise NotImplementedError
 
+    def _getnums(self, comma):
+        if self.find_size() > 1000:
+            nums = [str(self.getitem(index)) for index \
+                in range(3)]
+            nums.append("..." + "," * comma)
+            nums.extend([str(self.getitem(index)) for index \
+                in range(self.find_size() - 3, self.find_size())])
+        else:
+            nums = [str(self.getitem(index)) for index \
+                in range(self.find_size())]
+        return nums
+
+    def _repr(self, space):
+        # Simple implementation so that we can see the array. Needs work.
+        return space.wrap("array([" + ", ".join(self._getnums(False)) + "])")
+
+    def _str(self,space):
+        # Simple implementation so that we can see the array. Needs work.
+        return space.wrap("[" + " ".join(self._getnums(True)) + "]")
+
 class SingleDimSlice(ViewArray):
     _immutable_fields_ = ["start", "stop", "step", "size"]
     static_signature = Signature()
@@ -624,11 +644,11 @@
                 in range(self.find_size())]
         return nums
 
-    def descr_repr(self, space):
+    def _repr(self, space):
         # Simple implementation so that we can see the array. Needs work.
         return space.wrap("array([" + ", ".join(self._getnums(False)) + "])")
 
-    def descr_str(self,space):
+    def _str(self,space):
         # Simple implementation so that we can see the array. Needs work.
         return space.wrap("[" + " ".join(self._getnums(True)) + "]")
 
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
@@ -50,6 +50,15 @@
         a = zeros(1001)
         assert repr(a) == "array([0.0, 0.0, 0.0, ..., 0.0, 0.0, 0.0])"
 
+    def test_repr_slice(self):
+        from numpy import array, zeros
+        a = array(range(5))
+        b = a[1::2]
+        assert repr(b) == "array([1.0, 3.0])"
+        a = zeros(2002)
+        b = a[::2]
+        assert repr(b) == "array([0.0, 0.0, 0.0, ..., 0.0, 0.0, 0.0])"
+
     def test_str(self):
         from numpy import array, zeros
         a = array(range(5))
@@ -57,6 +66,15 @@
         a = zeros(1001)
         assert str(a) == "[0.0 0.0 0.0 ..., 0.0 0.0 0.0]" 
 
+    def test_str_slice(self):
+        from numpy import array, zeros
+        a = array(range(5))
+        b = a[1::2]
+        assert str(b) == "[1.0 3.0]"
+        a = zeros(2002)
+        b = a[::2]
+        assert str(b) == "[0.0 0.0 0.0 ..., 0.0 0.0 0.0]"
+
     def test_getitem(self):
         from numpy import array
         a = array(range(5))


More information about the pypy-commit mailing list