def put(space, w_arr, w_indices, w_values, w_mode): from pypy.module.micronumpy.support import index_w arr = convert_to_array(space, w_arr) mode = clipmode_converter(space, w_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 = index_w(space, idx) if index < 0 or index >= arr.get_size(): if mode == NPY_RAISE: raise OperationError( space.w_IndexError, space.wrap( "index %d is out of bounds for axis 0 with size %d" % (index, arr.get_size()))) elif mode == NPY_WRAP: index = index % arr.get_size() elif mode == NPY_CLIP: if index < 0: index = 0 else: index = arr.get_size() - 1 else: assert False value = values[v_idx] if v_idx + 1 < len(values): v_idx += 1 arr.setitem(space, [index], dtype.coerce(space, value))
def put(space, w_arr, w_indices, w_values, w_mode): from pypy.module.micronumpy.support import index_w arr = convert_to_array(space, w_arr) mode = clipmode_converter(space, w_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 = index_w(space, idx) if index < 0 or index >= arr.get_size(): if mode == NPY_RAISE: raise OperationError( space.w_IndexError, space.wrap("index %d is out of bounds for axis 0 with size %d" % (index, arr.get_size())), ) elif mode == NPY_WRAP: index = index % arr.get_size() elif mode == NPY_CLIP: if index < 0: index = 0 else: index = arr.get_size() - 1 else: assert False value = values[v_idx] if v_idx + 1 < len(values): v_idx += 1 arr.setitem(space, [index], dtype.coerce(space, value))
def choose(space, w_arr, w_choices, w_out, w_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")) 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() mode = clipmode_converter(space, w_mode) loop.choose(space, arr, choices, shape, dtype, out, mode) return out
def choose(space, w_arr, w_choices, w_out, w_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")) 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() mode = clipmode_converter(space, w_mode) loop.choose(space, arr, choices, shape, dtype, out, mode) return out