[pypy-commit] pypy default: Merged in andrewsmedina/numpypy/ndarray-ptp (pull request #157)

rguillebert noreply at buildbot.pypy.org
Mon Jul 1 15:40:02 CEST 2013


Author: Romain Guillebert <romain.py at gmail.com>
Branch: 
Changeset: r65140:1968b8b5d429
Date: 2013-07-01 15:39 +0200
http://bitbucket.org/pypy/pypy/changeset/1968b8b5d429/

Log:	Merged in andrewsmedina/numpypy/ndarray-ptp (pull request #157)

	implemented ndarray put

diff --git a/pypy/module/micronumpy/__init__.py b/pypy/module/micronumpy/__init__.py
--- a/pypy/module/micronumpy/__init__.py
+++ b/pypy/module/micronumpy/__init__.py
@@ -184,6 +184,7 @@
     appleveldefs = {}
     interpleveldefs = {
         'choose': 'interp_arrayops.choose',
+        'put': 'interp_arrayops.put',
         'repeat': 'interp_arrayops.repeat',
     }
     submodules = {
diff --git a/pypy/module/micronumpy/interp_arrayops.py b/pypy/module/micronumpy/interp_arrayops.py
--- a/pypy/module/micronumpy/interp_arrayops.py
+++ b/pypy/module/micronumpy/interp_arrayops.py
@@ -192,6 +192,61 @@
     loop.choose(space, arr, choices, shape, dtype, out, MODES[mode])
     return out
 
+
+ at unwrap_spec(mode=str)
+def put(space, w_arr, w_indices, w_values, mode='raise'):
+    from pypy.module.micronumpy import constants
+    from pypy.module.micronumpy.support import int_w
+
+    arr = convert_to_array(space, w_arr)
+
+    if mode not in constants.MODES:
+        raise OperationError(space.w_ValueError,
+                             space.wrap("mode %s not known" % (mode,)))
+    if not w_indices:
+        raise OperationError(space.w_ValueError,
+                             space.wrap("indice list cannot be empty"))
+    if not w_values:
+        raise OperationError(space.w_ValueError,
+                             space.wrap("value list cannot be empty"))
+
+    dtype = arr.get_dtype()
+
+    if space.isinstance_w(w_indices, space.w_list):
+        indices = space.listview(w_indices)
+    else:
+        indices = [w_indices]
+
+    if space.isinstance_w(w_values, space.w_list):
+        values = space.listview(w_values)
+    else:
+        values = [w_values]
+
+    v_idx = 0
+    for idx in indices:
+        index = int_w(space, idx)
+
+        if index < 0 or index >= arr.get_size():
+            if constants.MODES[mode] == constants.MODE_RAISE:
+                raise OperationError(space.w_ValueError, space.wrap(
+                    "invalid entry in choice array"))
+            elif constants.MODES[mode] == constants.MODE_WRAP:
+                index = index % arr.get_size()
+            else:
+                assert constants.MODES[mode] == constants.MODE_CLIP
+                if index < 0:
+                    index = 0
+                else:
+                    index = arr.get_size() - 1
+
+        value = values[v_idx]
+
+        if v_idx + 1 < len(values):
+            v_idx += 1
+
+        arr.setitem(space, [index], dtype.coerce(space, value))
+
+
 def diagonal(space, arr, offset, axis1, axis2):
     shape = arr.get_shape()
     shapelen = len(shape)
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
@@ -550,9 +550,10 @@
         raise OperationError(space.w_NotImplementedError, space.wrap(
             "ptp (peak to peak) not implemented yet"))
 
-    def descr_put(self, space, w_indices, w_values, w_mode='raise'):
-        raise OperationError(space.w_NotImplementedError, space.wrap(
-            "put not implemented yet"))
+    @unwrap_spec(mode=str)
+    def descr_put(self, space, w_indices, w_values, mode='raise'):
+        from pypy.module.micronumpy.interp_arrayops import put
+        put(space, self, w_indices, w_values, mode)
 
     def descr_resize(self, space, w_new_shape, w_refcheck=True):
         raise OperationError(space.w_NotImplementedError, space.wrap(
@@ -989,6 +990,7 @@
     prod = interp2app(W_NDimArray.descr_prod),
     max = interp2app(W_NDimArray.descr_max),
     min = interp2app(W_NDimArray.descr_min),
+    put = interp2app(W_NDimArray.descr_put),
     argmax = interp2app(W_NDimArray.descr_argmax),
     argmin = interp2app(W_NDimArray.descr_argmin),
     all = interp2app(W_NDimArray.descr_all),
diff --git a/pypy/module/micronumpy/test/test_arrayops.py b/pypy/module/micronumpy/test/test_arrayops.py
--- a/pypy/module/micronumpy/test/test_arrayops.py
+++ b/pypy/module/micronumpy/test/test_arrayops.py
@@ -132,3 +132,26 @@
         x = array([0, 0, 0], dtype='i2')
         r = array([2, 1, 0]).choose([a, b, c], out=x)
         assert r.dtype == 'i2'
+
+    def test_put_basic(self):
+        from numpypy import arange, array
+        a = arange(5)
+        a.put([0, 2], [-44, -55])
+        assert (a == array([-44, 1, -55, 3, 4])).all()
+        a = arange(5)
+        a.put([3, 4], 9)
+        assert (a == array([0, 1, 2, 9, 9])).all()
+        a = arange(5)
+        a.put(1, [7, 8])
+        assert (a == array([0, 7, 2, 3, 4])).all()
+
+    def test_put_modes(self):
+        from numpypy import array, arange
+        a = arange(5)
+        a.put(22, -5, mode='clip')
+        assert (a == array([0, 1, 2, 3, -5])).all()
+        a = arange(5)
+        a.put(22, -5, mode='wrap')
+        assert (a == array([0, 1, -5, 3, 4])).all()
+        raises(ValueError, "arange(5).put(22, -5, mode='raise')")
+        raises(ValueError, "arange(5).put(22, -5, mode='wrongmode')")


More information about the pypy-commit mailing list