Author: mattip
Branch: numpy-multidim
Changeset: r48650:6eff7c357df1
Date: 2011-11-01 20:34 +0200
http://bitbucket.org/pypy/pypy/changeset/6eff7c357df1/
Log: repr/str: add tests for numpy compliance, code cleanup
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
@@ -244,12 +244,15 @@
return self.get_concrete().descr_len(space)
def descr_repr(self, space):
- # Simple implementation so that we can see the array. Needs work.
+ # Simple implementation so that we can see the array.
+ # Since what we want is to print a plethora of 2d views,
+ # use recursive calls to tostr() to do the work.
concrete = self.get_concrete()
- new_sig = signature.Signature.find_sig([
- NDimSlice.signature, self.signature
- ])
- res = "array(" + NDimSlice(concrete, new_sig, [],
self.shape[:]).tostr(True, indent=' ')
+ res = "array("
+ res0 = NDimSlice(concrete, self.signature, [], self.shape).tostr(True,
indent=' ')
+ if res0=="[]" and isinstance(self,NDimSlice):
+ res0 += ", shape=%s"%(tuple(self.shape),)
+ res += res0
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():
@@ -258,12 +261,11 @@
return space.wrap(res)
def descr_str(self, space):
- # Simple implementation so that we can see the array. Needs work.
+ # Simple implementation so that we can see the array.
+ # Since what we want is to print a plethora of 2d views, let
+ # a slice do the work for us.
concrete = self.get_concrete()
- new_sig = signature.Signature.find_sig([
- NDimSlice.signature, self.signature
- ])
- return space.wrap(NDimSlice(concrete, new_sig, [],
self.shape[:]).tostr(False))
+ return space.wrap(NDimSlice(concrete, self.signature, [],
self.shape).tostr(False))
def _index_of_single_item(self, space, w_idx):
# we assume C ordering for now
@@ -668,6 +670,9 @@
ret = ''
dtype = self.find_dtype()
ndims = len(self.shape)#-self.shape_reduction
+ if any([s==0 for s in self.shape]):
+ ret += '[]'
+ return ret
if ndims>2:
ret += '['
for i in range(self.shape[0]):
@@ -698,7 +703,7 @@
for j in
range(self.shape[0])])
ret += ']'
else:
- ret += '[]'
+ ret += dtype.str_format(self.eval(0))
return ret
class NDimArray(BaseArray):
def __init__(self, size, shape, dtype):
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
@@ -91,6 +91,9 @@
a = array((range(5),range(5,10)), dtype="int16")
b=a[1,2:]
assert repr(b) == "array([7, 8, 9], dtype=int16)"
+ #This is the way cpython numpy does it - an empty slice prints its
shape
+ b=a[2:1,]
+ assert repr(b) == "array([], shape=(0, 5), dtype=int16)"
def test_str(self):
from numpy import array, zeros
@@ -114,6 +117,9 @@
a = array((range(5),range(5,10)), dtype="int16")
assert str(a) == "[[0 1 2 3 4],\n [5 6 7 8 9]]"
+ a = array(3,dtype=int)
+ assert str(a) == "3"
+
def test_str_slice(self):
from numpy import array, zeros
a = array(range(5), float)
@@ -125,6 +131,8 @@
a = array((range(5),range(5,10)), dtype="int16")
b=a[1,2:]
assert str(b) == "[7 8 9]"
+ b=a[2:1,]
+ assert str(b) == "[]"
def test_getitem(self):
from numpy import array
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit