[pypy-commit] pypy default: (jterrace) added tolist to various objects in numpy.
alex_gaynor
noreply at buildbot.pypy.org
Sun Dec 11 06:14:08 CET 2011
Author: Alex Gaynor <alex.gaynor at gmail.com>
Branch:
Changeset: r50362:3eba2ed546ad
Date: 2011-12-11 00:13 -0500
http://bitbucket.org/pypy/pypy/changeset/3eba2ed546ad/
Log: (jterrace) added tolist to various objects in numpy.
diff --git a/pypy/module/micronumpy/interp_boxes.py b/pypy/module/micronumpy/interp_boxes.py
--- a/pypy/module/micronumpy/interp_boxes.py
+++ b/pypy/module/micronumpy/interp_boxes.py
@@ -91,6 +91,9 @@
descr_neg = _unaryop_impl("negative")
descr_abs = _unaryop_impl("absolute")
+ def descr_tolist(self, space):
+ return self.get_dtype(space).itemtype.to_builtin_type(space, self)
+
class W_BoolBox(W_GenericBox, PrimitiveBox):
descr__new__, get_dtype = new_dtype_getter("bool")
@@ -179,6 +182,8 @@
__neg__ = interp2app(W_GenericBox.descr_neg),
__abs__ = interp2app(W_GenericBox.descr_abs),
+
+ tolist = interp2app(W_GenericBox.descr_tolist),
)
W_BoolBox.typedef = TypeDef("bool_", W_GenericBox.typedef,
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
@@ -876,6 +876,17 @@
arr.setshape(space, new_shape)
return arr
+ def descr_tolist(self, space):
+ if len(self.shape) == 0:
+ assert isinstance(self, Scalar)
+ return self.value.descr_tolist(space)
+ w_result = space.newlist([])
+ for i in range(self.shape[0]):
+ space.call_method(w_result, "append",
+ space.call_method(self.descr_getitem(space, space.wrap(i)), "tolist")
+ )
+ return w_result
+
def descr_mean(self, space):
return space.div(self.descr_sum(space), space.wrap(self.find_size()))
@@ -1485,6 +1496,7 @@
copy = interp2app(BaseArray.descr_copy),
reshape = interp2app(BaseArray.descr_reshape),
+ tolist = interp2app(BaseArray.descr_tolist),
)
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
@@ -879,6 +879,45 @@
b[0] = 3
assert b.__debug_repr__() == 'Call2(add, forced=Array)'
+ def test_tolist_scalar(self):
+ from numpypy import int32, bool_
+ x = int32(23)
+ assert x.tolist() == 23
+ assert type(x.tolist()) is int
+ y = bool_(True)
+ assert y.tolist() is True
+
+ def test_tolist_zerodim(self):
+ from numpypy import array
+ x = array(3)
+ assert x.tolist() == 3
+ assert type(x.tolist()) is int
+
+ def test_tolist_singledim(self):
+ from numpypy import array
+ a = array(range(5))
+ assert a.tolist() == [0, 1, 2, 3, 4]
+ assert type(a.tolist()[0]) is int
+ b = array([0.2, 0.4, 0.6])
+ assert b.tolist() == [0.2, 0.4, 0.6]
+
+ def test_tolist_multidim(self):
+ from numpypy import array
+ a = array([[1, 2], [3, 4]])
+ assert a.tolist() == [[1, 2], [3, 4]]
+
+ def test_tolist_view(self):
+ from numpypy import array
+ a = array([[1,2],[3,4]])
+ assert (a + a).tolist() == [[2, 4], [6, 8]]
+
+ def test_tolist_slice(self):
+ from numpypy import array
+ a = array([[17.1, 27.2], [40.3, 50.3]])
+ assert a[:,0].tolist() == [17.1, 40.3]
+ assert a[0].tolist() == [17.1, 27.2]
+
+
class AppTestMultiDim(BaseNumpyAppTest):
def test_init(self):
import numpypy
diff --git a/pypy/module/micronumpy/types.py b/pypy/module/micronumpy/types.py
--- a/pypy/module/micronumpy/types.py
+++ b/pypy/module/micronumpy/types.py
@@ -77,6 +77,9 @@
w_obj.__init__(self._coerce(space, w_item).value)
return w_obj
+ def to_builtin_type(self, space, box):
+ return space.wrap(self.unbox(box))
+
def _coerce(self, space, w_item):
raise NotImplementedError
More information about the pypy-commit
mailing list