Author: Brian Kearns <[email protected]>
Branch: cleanup-numpypy-namespace
Changeset: r61758:89d20ee06fb3
Date: 2013-02-25 08:01 -0500
http://bitbucket.org/pypy/pypy/changeset/89d20ee06fb3/
Log: enable numpypy.{choose,repeat}
diff --git a/lib_pypy/numpypy/core/fromnumeric.py
b/lib_pypy/numpypy/core/fromnumeric.py
--- a/lib_pypy/numpypy/core/fromnumeric.py
+++ b/lib_pypy/numpypy/core/fromnumeric.py
@@ -16,6 +16,7 @@
######################################################################
import numpypy
+from _numpypy import choose, repeat
# Module containing non-deprecated functions borrowed from Numeric.
__docformat__ = "restructuredtext en"
@@ -274,7 +275,7 @@
[-1, -2, -3, -4, -5]]])
"""
- raise NotImplementedError('Waiting on interp level method')
+ return choose(a, choices, out, mode)
def repeat(a, repeats, axis=None):
@@ -316,7 +317,7 @@
[3, 4]])
"""
- raise NotImplementedError('Waiting on interp level method')
+ return repeat(a, repeats, axis)
def put(a, ind, v, mode='raise'):
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
@@ -16,7 +16,6 @@
'fromstring': 'interp_support.fromstring',
'flatiter': 'interp_flatiter.W_FlatIterator',
'concatenate': 'interp_arrayops.concatenate',
- 'repeat': 'interp_arrayops.repeat',
'where': 'interp_arrayops.where',
'count_nonzero': 'interp_arrayops.count_nonzero',
@@ -180,7 +179,10 @@
class Module(MixedModule):
applevel_name = '_numpypy'
appleveldefs = {}
- interpleveldefs = {}
+ interpleveldefs = {
+ 'choose': 'interp_arrayops.choose',
+ 'repeat': 'interp_arrayops.repeat',
+ }
submodules = {
'multiarray': MultiArrayModule,
'numerictypes': NumericTypesModule,
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
@@ -135,7 +135,7 @@
return res
@unwrap_spec(repeats=int)
-def repeat(space, w_arr, repeats, w_axis=None):
+def repeat(space, w_arr, repeats, w_axis):
arr = convert_to_array(space, w_arr)
if space.is_none(w_axis):
arr = arr.descr_flatten(space)
@@ -161,14 +161,21 @@
def count_nonzero(space, w_obj):
return space.wrap(loop.count_all_true(convert_to_array(space, w_obj)))
-def choose(space, arr, w_choices, out, mode):
+@unwrap_spec(mode=str)
+def choose(space, w_arr, w_choices, w_out, mode):
+ arr = convert_to_array(space, w_arr)
choices = [convert_to_array(space, w_item) for w_item
in space.listview(w_choices)]
if not choices:
raise OperationError(space.w_ValueError,
space.wrap("choices list cannot be empty"))
- shape = shape_agreement_multiple(space, choices + [out])
- out = interp_dtype.dtype_agreement(space, choices, shape, out)
+ if space.is_none(w_out):
+ w_out = None
+ elif not isinstance(w_out, W_NDimArray):
+ raise OperationError(space.w_TypeError, space.wrap(
+ "return arrays must be of ArrayType"))
+ shape = shape_agreement_multiple(space, choices + [w_out])
+ out = interp_dtype.dtype_agreement(space, choices, shape, w_out)
dtype = out.get_dtype()
if mode not in MODES:
raise OperationError(space.w_ValueError,
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
@@ -14,7 +14,7 @@
from pypy.module.micronumpy.appbridge import get_appbridge_cache
from pypy.module.micronumpy import loop
from pypy.module.micronumpy.dot import match_dot_shapes
-from pypy.module.micronumpy.interp_arrayops import repeat
+from pypy.module.micronumpy.interp_arrayops import repeat, choose
from rpython.tool.sourcetools import func_with_new_name
from rpython.rlib import jit
from rpython.rlib.rstring import StringBuilder
@@ -452,13 +452,8 @@
return res
@unwrap_spec(mode=str)
- def descr_choose(self, space, w_choices, mode='raise', w_out=None):
- if space.is_none(w_out):
- w_out = None
- elif not isinstance(w_out, W_NDimArray):
- raise OperationError(space.w_TypeError, space.wrap(
- "return arrays must be of ArrayType"))
- return interp_arrayops.choose(space, self, w_choices, w_out, mode)
+ def descr_choose(self, space, w_choices, w_out=None, mode='raise'):
+ return choose(space, self, w_choices, w_out, mode)
def descr_clip(self, space, w_min, w_max, w_out=None):
if space.is_none(w_out):
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
@@ -85,10 +85,12 @@
assert c == 12.0
def test_choose_basic(self):
- from numpypy import array
+ from numpypy import array, choose
a, b, c = array([1, 2, 3]), array([4, 5, 6]), array([7, 8, 9])
r = array([2, 1, 0]).choose([a, b, c])
assert (r == [7, 5, 3]).all()
+ r = choose(array([2, 1, 0]), [a, b, c])
+ assert (r == [7, 5, 3]).all()
def test_choose_broadcast(self):
from numpypy import array
@@ -110,7 +112,7 @@
from numpypy import array
a, b, c = array([1, 2, 3]), [4, 5, 6], 13
raises(ValueError, "array([3, 1, 0]).choose([a, b, c])")
- raises(ValueError, "array([3, 1, 0]).choose([a, b, c], 'raises')")
+ raises(ValueError, "array([3, 1, 0]).choose([a, b, c], mode='raises')")
raises(ValueError, "array([3, 1, 0]).choose([])")
raises(ValueError, "array([-1, -2, -3]).choose([a, b, c])")
r = array([4, 1, 0]).choose([a, b, c], mode='clip')
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit