Author: Brian Kearns <bdkea...@gmail.com> Branch: Changeset: r70805:c39475b4b4f7 Date: 2014-04-21 01:28 -0400 http://bitbucket.org/pypy/pypy/changeset/c39475b4b4f7/
Log: reduce code dup in searchsort diff --git a/pypy/module/micronumpy/app_numpy.py b/pypy/module/micronumpy/app_numpy.py --- a/pypy/module/micronumpy/app_numpy.py +++ b/pypy/module/micronumpy/app_numpy.py @@ -22,4 +22,3 @@ arr[j] = i i += step return arr - diff --git a/pypy/module/micronumpy/ndarray.py b/pypy/module/micronumpy/ndarray.py --- a/pypy/module/micronumpy/ndarray.py +++ b/pypy/module/micronumpy/ndarray.py @@ -677,23 +677,23 @@ def descr_round(self, space, decimals=0, w_out=None): if space.is_none(w_out): if self.get_dtype().is_bool(): - #numpy promotes bool.round() to float16. Go figure. + # numpy promotes bool.round() to float16. Go figure. w_out = W_NDimArray.from_shape(space, self.get_shape(), - descriptor.get_dtype_cache(space).w_float16dtype) + descriptor.get_dtype_cache(space).w_float16dtype) else: w_out = None elif not isinstance(w_out, W_NDimArray): raise OperationError(space.w_TypeError, space.wrap( "return arrays must be of ArrayType")) out = descriptor.dtype_agreement(space, [self], self.get_shape(), - w_out) + w_out) if out.get_dtype().is_bool() and self.get_dtype().is_bool(): calc_dtype = descriptor.get_dtype_cache(space).w_longdtype else: calc_dtype = out.get_dtype() if decimals == 0: - out = out.descr_view(space,space.type(self)) + out = out.descr_view(space, space.type(self)) loop.round(space, self, calc_dtype, self.get_shape(), decimals, out) return out @@ -711,16 +711,16 @@ side = 'r' else: raise oefmt(space.w_ValueError, - "'%s' is an invalid value for keyword 'side'", side) + "'%s' is an invalid value for keyword 'side'", side) if len(self.get_shape()) > 1: raise OperationError(space.w_ValueError, space.wrap( - "a must be a 1-d array")) + "a must be a 1-d array") v = convert_to_array(space, w_v) - if len(v.get_shape()) >1: + if len(v.get_shape()) > 1: raise OperationError(space.w_ValueError, space.wrap( - "v must be a 1-d array-like")) - ret = W_NDimArray.from_shape(space, v.get_shape(), - descriptor.get_dtype_cache(space).w_longdtype) + "v must be a 1-d array-like") + ret = W_NDimArray.from_shape( + space, v.get_shape(), descriptor.get_dtype_cache(space).w_longdtype) app_searchsort(space, self, v, space.wrap(side), ret) return ret @@ -1277,35 +1277,26 @@ app_searchsort = applevel(r""" def searchsort(arr, v, side, result): - def left_find_index(a, val): + import operator + def func(a, op, val): imin = 0 imax = a.size while imin < imax: imid = imin + ((imax - imin) >> 1) - if a[imid] < val: - imin = imid +1 - else: - imax = imid - return imin - def right_find_index(a, val): - imin = 0 - imax = a.size - while imin < imax: - imid = imin + ((imax - imin) >> 1) - if a[imid] <= val: + if op(a[imid], val): imin = imid +1 else: imax = imid return imin if side == 'l': - func = left_find_index + op = operator.lt else: - func = right_find_index + op = operator.le if v.size < 2: - result[...] = func(arr, v) + result[...] = func(arr, op, v) else: for i in range(v.size): - result[i] = func(arr, v[i]) + result[i] = func(arr, op, v[i]) return result """, filename=__file__).interphook('searchsort') diff --git a/pypy/module/micronumpy/test/test_sorting.py b/pypy/module/micronumpy/test/test_sorting.py --- a/pypy/module/micronumpy/test/test_sorting.py +++ b/pypy/module/micronumpy/test/test_sorting.py @@ -362,4 +362,3 @@ assert (ret == [0, 5, 1, 2]).all() if '__pypy__' in sys.builtin_module_names: raises(NotImplementedError, "a.searchsorted(3, sorter=range(6))") - _______________________________________________ pypy-commit mailing list pypy-commit@python.org https://mail.python.org/mailman/listinfo/pypy-commit