[pypy-commit] pypy numpy-singledim: numpy - fixed repr and added str
justinpeel
noreply at buildbot.pypy.org
Thu Jul 14 03:25:42 CEST 2011
Author: Justin Peel <notmuchtotell at gmail.com>
Branch: numpy-singledim
Changeset: r45576:fe7fb5a882ab
Date: 2011-07-13 17:46 -0600
http://bitbucket.org/pypy/pypy/changeset/fe7fb5a882ab/
Log: numpy - fixed repr and added str
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
@@ -250,6 +250,12 @@
def descr_get_ndim(self, space):
return self.get_concrete().descr_get_ndim(space)
+ def descr_repr(self, space):
+ return self.get_concrete().descr_repr(space)
+
+ def descr_str(self, space):
+ return self.get_concrete().descr_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())
@@ -269,11 +275,7 @@
def descr_mean(self, space):
return space.wrap(space.float_w(self.descr_sum(space))/self.find_size())
- def descr_repr(self, space):
- concrete = self.get_concrete()
- return space.wrap("array([" +
- " ".join([str(concrete.getitem(index)) for index in range(concrete.find_size())]) +
- "])")
+
def convert_to_array (space, w_obj):
if isinstance(w_obj, BaseArray):
@@ -487,6 +489,24 @@
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 descr_repr(self, space):
+ return space.wrap("array([" + ", ".join(self._getnums(False)) + "])")
+
+ def descr_str(self,space):
+ 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)
@@ -547,6 +567,7 @@
__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),
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
@@ -44,9 +44,18 @@
assert a[3] == 3
def test_repr(self):
- from numpy import array
+ from numpy import array, zeros
a = array(range(5))
- assert repr(a) == "array([0.0 1.0 2.0 3.0 4.0])"
+ 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_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_getitem(self):
from numpy import array
More information about the pypy-commit
mailing list