本文整理汇总了Python中builtins.slice方法的典型用法代码示例。如果您正苦于以下问题:Python builtins.slice方法的具体用法?Python builtins.slice怎么用?Python builtins.slice使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类builtins
的用法示例。
在下文中一共展示了builtins.slice方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _get_indexing_dispatch_code
# 需要导入模块: import builtins [as 别名]
# 或者: from builtins import slice [as 别名]
def _get_indexing_dispatch_code(key):
"""Returns a dispatch code for calling basic or advanced indexing functions."""
if isinstance(key, (NDArray, np.ndarray)):
return _NDARRAY_ADVANCED_INDEXING
elif isinstance(key, list):
# TODO(junwu): Add support for nested lists besides integer list
for i in key:
if not isinstance(i, integer_types):
raise TypeError('Indexing NDArray only supports a list of integers as index'
' when key is of list type, received element=%s of type=%s'
% (str(i), str(type(i))))
return _NDARRAY_ADVANCED_INDEXING
elif isinstance(key, (integer_types, py_slice)):
return _NDARRAY_BASIC_INDEXING
elif isinstance(key, tuple):
for idx in key:
if isinstance(idx, (NDArray, np.ndarray, list, tuple)):
return _NDARRAY_ADVANCED_INDEXING
elif not isinstance(idx, (py_slice, integer_types)):
raise ValueError("NDArray does not support slicing with key %s of type %s."
% (str(idx), str(type(idx))))
return _NDARRAY_BASIC_INDEXING
else:
return _NDARRAY_UNSUPPORTED_INDEXING
示例2: slice
# 需要导入模块: import builtins [as 别名]
# 或者: from builtins import slice [as 别名]
def slice(*args):
"""
slice([start], end [,step])
nd version of slice, where each arg can be a vector of the same length
Parameters:
[start] (vector): the start
"""
# if passed in scalars call the built-in range
if not isinstance(args[0], (list, tuple, np.ndarray)):
return builtins.slice(*args)
start, end, step = _prep_range(*args)
# prepare
idx = [slice(start[i], end[i], step[i]) for i in range(len(end))]
return idx
示例3: __getitem__
# 需要导入模块: import builtins [as 别名]
# 或者: from builtins import slice [as 别名]
def __getitem__(self, key):
"""x.__getitem__(i) <=> x[i]
Returns a sliced view of this array.
Parameters
----------
key : slice
Indexing key.
Examples
--------
>>> x = mx.nd.sparse.zeros('row_sparse', (2, 3))
>>> x[:].asnumpy()
array([[ 0., 0., 0.],
[ 0., 0., 0.]], dtype=float32)
"""
if isinstance(key, int):
raise Exception("__getitem__ with int key is not implemented for RowSparseNDArray yet")
if isinstance(key, py_slice):
if key.step is not None or key.start is not None or key.stop is not None:
raise Exception('RowSparseNDArray only supports [:] for __getitem__')
else:
return self
if isinstance(key, tuple):
raise ValueError('Multi-dimension indexing is not supported')
raise ValueError('Undefined behaviour for {}'.format(key))
示例4: __getitem__
# 需要导入模块: import builtins [as 别名]
# 或者: from builtins import slice [as 别名]
def __getitem__(self, key):
"""x.__getitem__(i) <=> x[i]
Returns a sliced view of this array.
Parameters
----------
key : slice
Indexing key.
Examples
--------
>>> x = mx.nd.sparse.zeros('row_sparse', (2, 3))
>>> x[:].asnumpy()
array([[ 0., 0., 0.],
[ 0., 0., 0.]], dtype=float32)
"""
if isinstance(key, int):
raise Exception("__getitem__ with int key is not implemented for RowSparseNDArray yet")
if isinstance(key, py_slice):
if key.step is not None or key.start is not None or key.stop is not None:
raise Exception('RowSparseNDArray only supports [:] for __getitem__')
else:
return self
if isinstance(key, tuple):
raise ValueError('Multi-dimension indexing is not supported')
示例5: evaluate_operation
# 需要导入模块: import builtins [as 别名]
# 或者: from builtins import slice [as 别名]
def evaluate_operation(cls, operation, context, **kwargs):
"""
Evaluate an operation or constant given a context.
"""
try:
if isinstance(operation, Operation):
return operation.evaluate(context, **kwargs)
partial = functools.partial(cls.evaluate_operation, context=context, **kwargs)
if isinstance(operation, tuple):
return tuple(partial(element) for element in operation)
if isinstance(operation, list):
return [partial(element) for element in operation]
if isinstance(operation, dict):
return {partial(key): partial(value) for key, value in operation.items()}
if isinstance(operation, slice):
return slice(*[partial(getattr(operation, attr))
for attr in ['start', 'stop', 'step']])
return operation
except Exception as ex: # pragma: no cover
stack = []
interactive = False
for frame in reversed(operation._stack): # pylint: disable=protected-access
# Do not capture any internal stack traces
if 'pythonflow' in frame.filename:
continue
# Stop tracing at the last interactive cell
if interactive and not frame.filename.startswith('<'):
break # pragma: no cover
interactive = frame.filename.startswith('<')
stack.append(frame)
stack = "".join(traceback.format_list(reversed(stack)))
message = "Failed to evaluate operation `%s` defined at:\n\n%s" % (operation, stack)
raise ex from EvaluationError(message)
示例6: slice
# 需要导入模块: import builtins [as 别名]
# 或者: from builtins import slice [as 别名]
def slice(source, *args):
"""Slice an asynchronous sequence.
The arguments are the same as the builtin type slice.
There are two limitations compare to regular slices:
- Positive stop index with negative start index is not supported
- Negative step is not supported
"""
s = builtins.slice(*args)
start, stop, step = s.start or 0, s.stop, s.step or 1
# Filter the first items
if start < 0:
source = takelast.raw(source, abs(start))
elif start > 0:
source = skip.raw(source, start)
# Filter the last items
if stop is not None:
if stop >= 0 and start < 0:
raise ValueError(
"Positive stop with negative start is not supported")
elif stop >= 0:
source = take.raw(source, stop - start)
else:
source = skiplast.raw(source, abs(stop))
# Filter step items
if step is not None:
if step > 1:
source = filterindex.raw(source, lambda i: i % step == 0)
elif step < 0:
raise ValueError("Negative step not supported")
# Return
return source
示例7: getitem
# 需要导入模块: import builtins [as 别名]
# 或者: from builtins import slice [as 别名]
def getitem(source, index):
"""Forward one or several items from an asynchronous sequence.
The argument can either be a slice or an integer.
See the slice and item operators for more information.
"""
if isinstance(index, builtins.slice):
return slice.raw(source, index.start, index.stop, index.step)
if isinstance(index, int):
return item.raw(source, index)
raise TypeError("Not a valid index (int or slice)")
示例8: __setitem__
# 需要导入模块: import builtins [as 别名]
# 或者: from builtins import slice [as 别名]
def __setitem__(self, key, value):
"""x.__setitem__(i, y) <=> x[i]=y
Set self[key] to value. Only slice key [:] is supported.
Parameters
----------
key : slice
The indexing key.
value : NDArray or CSRNDArray or numpy.ndarray
The value to set.
Examples
--------
>>> src = mx.nd.sparse.zeros('csr', (3,3))
>>> src.asnumpy()
array([[ 0., 0., 0.],
[ 0., 0., 0.],
[ 0., 0., 0.]], dtype=float32)
>>> # assign CSRNDArray with same storage type
>>> x = mx.nd.ones((3,3)).tostype('csr')
>>> x[:] = src
>>> x.asnumpy()
array([[ 1., 1., 1.],
[ 1., 1., 1.],
[ 1., 1., 1.]], dtype=float32)
>>> # assign NDArray to CSRNDArray
>>> x[:] = mx.nd.ones((3,3)) * 2
>>> x.asnumpy()
array([[ 2., 2., 2.],
[ 2., 2., 2.],
[ 2., 2., 2.]], dtype=float32)
"""
if not self.writable:
raise ValueError('Failed to assign to a readonly CSRNDArray')
if isinstance(key, py_slice):
if key.step is not None or key.start is not None or key.stop is not None:
raise ValueError('Assignment with slice for CSRNDArray is not ' \
'implmented yet.')
if isinstance(value, NDArray):
# avoid copying to itself
if value.handle is not self.handle:
value.copyto(self)
elif isinstance(value, numeric_types):
raise ValueError("Assigning numeric types to CSRNDArray is " \
"not implemented yet.")
elif isinstance(value, (np.ndarray, np.generic)):
# TODO(haibin/anisub) check scipy.sparse and use _sync_copy_from to
# avoid the temporary copy
warnings.warn('Assigning non-NDArray object to CSRNDArray is not efficient',
RuntimeWarning)
tmp = _array(value)
tmp.copyto(self)
else:
raise TypeError('type %s not supported' % str(type(value)))
else:
assert(isinstance(key, (int, tuple)))
raise Exception('CSRNDArray only supports [:] for assignment')
示例9: __setitem__
# 需要导入模块: import builtins [as 别名]
# 或者: from builtins import slice [as 别名]
def __setitem__(self, key, value):
"""x.__setitem__(i, y) <=> x[i]=y
Set self[key] to value. Only slice key [:] is supported.
Parameters
----------
key : slice
The indexing key.
value : NDArray or CSRNDArray or numpy.ndarray
The value to set.
Examples
--------
>>> src = mx.nd.sparse.zeros('csr', (3,3))
>>> src.asnumpy()
array([[ 0., 0., 0.],
[ 0., 0., 0.],
[ 0., 0., 0.]], dtype=float32)
>>> # assign CSRNDArray with same storage type
>>> x = mx.nd.ones('row_sparse', (3,3)).tostype('csr')
>>> x[:] = src
>>> x.asnumpy()
array([[ 1., 1., 1.],
[ 1., 1., 1.],
[ 1., 1., 1.]], dtype=float32)
>>> # assign NDArray to CSRNDArray
>>> x[:] = mx.nd.ones((3,3)) * 2
>>> x.asnumpy()
array([[ 2., 2., 2.],
[ 2., 2., 2.],
[ 2., 2., 2.]], dtype=float32)
"""
if not self.writable:
raise ValueError('Failed to assign to a readonly CSRNDArray')
if isinstance(key, py_slice):
if key.step is not None or key.start is not None or key.stop is not None:
raise ValueError('Assignment with slice for CSRNDArray is not ' \
'implmented yet.')
if isinstance(value, NDArray):
# avoid copying to itself
if value.handle is not self.handle:
value.copyto(self)
elif isinstance(value, numeric_types):
raise ValueError("Assigning numeric types to CSRNDArray is " \
"not implemented yet.")
elif isinstance(value, (np.ndarray, np.generic)):
# TODO(haibin/anisub) check scipy.sparse and use _sync_copy_from to
# avoid the temporary copy
warnings.warn('Assigning non-NDArray object to CSRNDArray is not efficient',
RuntimeWarning)
tmp = _array(value)
tmp.copyto(self)
else:
raise TypeError('type %s not supported' % str(type(value)))
else:
assert(isinstance(key, (int, tuple)))
raise Exception('CSRNDArray only supports [:] for assignment')
示例10: __getitem__
# 需要导入模块: import builtins [as 别名]
# 或者: from builtins import slice [as 别名]
def __getitem__(self, index):
"""x.__getitem__(i) <=> x[i]
Returns a sliced view of the input symbol.
Example
-------
>>> a = mx.sym.var('a')
>>> a.__getitem__(0)
<Symbol a>
>>> a[0]
<Symbol a>
Parameters
----------
index : int or str
Indexing key
"""
output_names = self.list_outputs()
if isinstance(index, py_slice):
start = 0 if index.start is None else index.start
stop = len(output_names) if index.stop is None else index.stop
step = 1 if index.step is None else index.step
return Group([self[i] for i in range(start, stop, step)])
if isinstance(index, string_types):
idx = None
for i, name in enumerate(output_names):
if name == index:
if idx is not None:
raise ValueError('There are multiple outputs with name \"%s\"' % index)
idx = i
if idx is None:
raise ValueError('Cannot find output that matches name \"%s\"' % index)
index = idx
if not isinstance(index, int):
raise TypeError('Symbol only support integer index to fetch i-th output')
if index >= len(output_names):
# Important, python determines the end by this exception
raise IndexError
handle = SymbolHandle()
check_call(_LIB.MXSymbolGetOutput(
self.handle, mx_uint(index), ctypes.byref(handle)))
return Symbol(handle=handle)