Commits

Justin Peel committed 9e83042

numpy: simple implementations of repr and str. includes work by Timo.

Comments (0)

Files changed (2)

pypy/module/micronumpy/interp_numarray.py

     def descr_len(self, space):
         return self.get_concrete().descr_len(space)
 
+    def descr_repr(self, space):
+        return self.get_concrete()._repr(space)
+
+    def descr_str(self, space):
+        return self.get_concrete()._str(space)
+
     def descr_getitem(self, space, w_idx):
         # TODO: indexing by tuples
         start, stop, step, slice_length = space.decode_index4(w_idx, self.find_size())
     def calc_index(self, item):
         return (self.start + item * self.step)
 
+    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 SingleDimArray(BaseArray):
     signature = Signature()
     def getitem(self, item):
         return self.storage[item]
 
+    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)) + "]")
+
     @unwrap_spec(item=int, value=float)
     def descr_setitem(self, space, item, value):
         item = self.getindex(space, item)
     __rdiv__ = interp2app(BaseArray.descr_rdiv),
     __rpow__ = interp2app(BaseArray.descr_rpow),
     __rmod__ = interp2app(BaseArray.descr_rmod),
+    __repr__ = interp2app(BaseArray.descr_repr),
+    __str__ = interp2app(BaseArray.descr_str),
 
     mean = interp2app(BaseArray.descr_mean),
     sum = interp2app(BaseArray.descr_sum),

pypy/module/micronumpy/test/test_numarray.py

         a = array(range(5))
         assert a[3] == 3
 
+    def test_repr(self):
+        from numpy import array, zeros
+        a = array(range(5))
+        assert repr(a) == "array([0.0, 1.0, 2.0, 3.0, 4.0])"
+        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))
+        assert str(a) == "[0.0 1.0 2.0 3.0 4.0]"
+        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))