我们从Python开源项目中,提取了以下8个代码示例,用于说明如何使用ast.ExtSlice()。
def slicev(self, node): if isinstance(node, ast.Slice): if node.lower: self.visit(node.lower) if node.upper: self.visit(node.upper) if node.step: self.visit(node.step) elif isinstance(node, ast.ExtSlice): if node.dims: for d in node.dims: self.visit(d) else: self.visit(node.value) # operator = Add | Sub | Mult | MatMult | Div | Mod | Pow | LShift | RShift | BitOr | BitXor | BitAnd | FloorDiv
def test_subscript(self): sub = ast.Subscript(ast.Name("x", ast.Store()), ast.Index(ast.Num(3)), ast.Load()) self.expr(sub, "must have Load context") x = ast.Name("x", ast.Load()) sub = ast.Subscript(x, ast.Index(ast.Name("y", ast.Store())), ast.Load()) self.expr(sub, "must have Load context") s = ast.Name("x", ast.Store()) for args in (s, None, None), (None, s, None), (None, None, s): sl = ast.Slice(*args) self.expr(ast.Subscript(x, sl, ast.Load()), "must have Load context") sl = ast.ExtSlice([]) self.expr(ast.Subscript(x, sl, ast.Load()), "empty dims on ExtSlice") sl = ast.ExtSlice([ast.Index(s)]) self.expr(ast.Subscript(x, sl, ast.Load()), "must have Load context")
def syn_Subscript(self, ctx, e): slice_ = e.slice if isinstance(slice_, ast.Ellipsis): raise _errors.TyError("stringing slice cannot be an Ellipsis.", e) elif isinstance(slice_, ast.ExtSlice): raise _errors.TyError("stringing slice can only have one dimension.", e) elif isinstance(slice_, ast.Index): ctx.ana(slice_.value, _numeric.num) else: # if isinstance(slice_, ast.Slice): lower, upper, step = slice_.lower, slice_.upper, slice_.step if lower is not None: ctx.ana(lower, _numeric.num) if upper is not None: ctx.ana(upper, _numeric.num) if not _is_None(step): ctx.ana(step, _numeric.num) return self
def slicev(self, node): if isinstance(node, ast.Slice): if node.lower: self.visit(node.lower) if node.upper: self.visit(node.upper) if node.step: self.visit(node.step) elif isinstance(node, ast.ExtSlice): if node.dims: for d in node.dims: self.visit(d) else: self.visit(node.value)
def make_unconcat_slice(axis, lower, upper): dims = [] for i in range(axis): dims.append(ast.Slice(lower=None, upper=None, step=None)) dims.append(ast.Slice(lower=lower, upper=upper, step=None)) dims.append(ast.Ellipsis()) ext_slice = ast.ExtSlice(dims=dims) return ext_slice
def handle_Subscript(state, node, ctx): state, value_result = _peval_expression(state, node.value, ctx) state, slice_result = _peval_expression(state, node.slice, ctx) if is_known_value(value_result) and is_known_value(slice_result): success, elem = try_call_method( value_result.value, '__getitem__', args=(slice_result.value,)) if success: return state, KnownValue(value=elem) state, new_value = map_reify(state, value_result) state, new_slice = map_reify(state, slice_result) if type(new_slice) not in (ast.Index, ast.Slice, ast.ExtSlice): new_slice = ast.Index(value=new_slice) return state, replace_fields(node, value=new_value, slice=new_slice)