本文整理匯總了Python中ast.Slice方法的典型用法代碼示例。如果您正苦於以下問題:Python ast.Slice方法的具體用法?Python ast.Slice怎麽用?Python ast.Slice使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類ast
的用法示例。
在下文中一共展示了ast.Slice方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: slicev
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Slice [as 別名]
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
示例2: test_subscript
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Slice [as 別名]
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")
示例3: visit_Name
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Slice [as 別名]
def visit_Name( self, node ):
self.generic_visit( node )
if isinstance( node._object, slice ):
if node._object.step:
raise VerilogTranslationError(
'Slices with steps ([start:stop:step]) are not translatable!\n',
node.lineno
)
new_node = ast.Slice( ast.Num( node._object.start ),
ast.Num( node._object.stop ),
None )
return ast.copy_location( new_node, node )
return node
#-------------------------------------------------------------------------
# BitStructToSlice
#-------------------------------------------------------------------------
示例4: visit_Attribute
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Slice [as 別名]
def visit_Attribute( self, node ):
self.generic_visit( node )
if isinstance( node._object, _SignalSlice ):
if node._object.slice.step:
raise VerilogTranslationError(
'Slices with steps ([start:stop:step]) are not translatable!\n',
node.lineno
)
new_node = ast.Subscript( node.value,
ast.Slice( ast.Num( node._object.slice.start ),
ast.Num( node._object.slice.stop ),
None ),
None,
)
new_node._object = node._object
return ast.copy_location( new_node, node )
return node
#-------------------------------------------------------------------------
# InferTemporaryTypes
#-------------------------------------------------------------------------
示例5: visit_Slice
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Slice [as 別名]
def visit_Slice(self, node: ast.Slice) -> slice:
"""Visit ``lower``, ``upper`` and ``step`` and recompute the node as a ``slice``."""
lower = None # type: Optional[int]
if node.lower is not None:
lower = self.visit(node=node.lower)
upper = None # type: Optional[int]
if node.upper is not None:
upper = self.visit(node=node.upper)
step = None # type: Optional[int]
if node.step is not None:
step = self.visit(node=node.step)
result = slice(lower, upper, step)
self.recomputed_values[node] = result
return result
示例6: process_Slice
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Slice [as 別名]
def process_Slice(self, block: "Block", node: ast.Slice) -> ANFNode:
"""Process subscript slices."""
op = block.operation("slice")
if node.lower is None:
lower = Constant(None)
else:
lower = self.process_node(block, node.lower)
if node.upper is None:
upper = Constant(None)
else:
upper = self.process_node(block, node.upper)
if node.step is None:
step = Constant(None)
else:
step = self.process_node(block, node.step)
return block.apply(op, lower, upper, step)
示例7: visit_Index
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Slice [as 別名]
def visit_Index(self, node):
new_value = self._visit(node.value)
if isinstance(new_value, ast.Ellipsis):
new_node = new_value
elif isinstance(new_value, ast.Tuple):
if any(isinstance(elt, ast.Ellipsis) for elt in new_value.elts):
new_elts = [elt if isinstance(elt, (ast.Ellipsis, ast.Slice))
else ast.Index(elt)
for elt in new_value.elts]
new_node = ast.ExtSlice(new_elts)
else:
new_node = ast.Index(new_value)
else:
new_node = ast.Index(new_value)
ast.copy_location(new_node, node)
return new_node
示例8: normalize_tuple_slice
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Slice [as 別名]
def normalize_tuple_slice(node):
"""
Normalize an ast.Tuple node representing the internals of a slice.
Returns the node wrapped in an ast.Index.
Returns an ExtSlice node built from the tuple elements if there are any
slices.
"""
if not any(isinstance(elt, ast.Slice) for elt in node.elts):
return ast.Index(value=node)
return ast.ExtSlice(
[
# Wrap non-Slice nodes in Index nodes.
elt if isinstance(elt, ast.Slice) else ast.Index(value=elt)
for elt in node.elts
]
)
示例9: _make_expr_build_slice
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Slice [as 別名]
def _make_expr_build_slice(toplevel, stack_builders):
# Arg is always either 2 or 3. If it's 3, then the first expression is the
# step value.
if toplevel.arg == 3:
step = make_expr(stack_builders)
else:
step = None
def normalize_empty_slice(node):
"""
Convert LOAD_CONST(None) to just None.
This normalizes slices of the form a[b:None] to just a[b:].
"""
if isinstance(node, ast.NameConstant) and node.value is None:
return None
return node
upper = normalize_empty_slice(make_expr(stack_builders))
lower = normalize_empty_slice(make_expr(stack_builders))
return ast.Slice(lower=lower, upper=upper, step=step)
示例10: delete_code
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Slice [as 別名]
def delete_code(self, target):
if type(target) is ast.Attribute:
return [T('delattr({}, {!r})').format(self.visit(target.value), target.attr)]
elif type(target) is ast.Subscript:
if type(target.slice) is ast.Slice and target.slice.step is None:
return [T("(lambda o, **t: type('translator', (), {{t[m]: "
"staticmethod(object.__getattribute__(d[m], '__get__'"
")(o, type(o))) for d in [object.__getattribute__("
"type(o), '__dict__')] for m in t if m in d}})())({},"
" __delitem__='__getitem__', __delslice__="
"'__getslice__', __len__='__len__')[{}]").format(
self.visit(target.value),
self.visit(target.slice))]
else:
return [T("{__operator}.delitem({}, {})").format(
self.visit(target.value),
self.slice_repr(target.slice))]
elif type(target) is ast.Name:
return [self.delete_var(target.id)]
elif type(target) in (ast.List, ast.Tuple):
return [c for elt in target.elts for c in self.delete_code(elt)]
else:
raise NotImplementedError('Case not caught: %s' % str(type(target)))
示例11: _translate_get_slice
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Slice [as 別名]
def _translate_get_slice(self, lineno, target, start=None, stop=None, step=None):
pre = []
target_pre, target_value = self.translate(target, False)
pre.extend(target_pre)
start_pre, start_value = self.translate(start, False) if start is not None else ((), None)
pre.extend(start_pre)
stop_pre, stop_value = self.translate(stop, False) if stop is not None else ((), None)
pre.extend(stop_pre)
step_pre, step_value = self.translate(step, False) if step is not None else ((), None)
pre.extend(step_pre)
return pre, ast.Subscript(value=target_value,
slice=ast.Slice(lower=start_value,
upper=stop_value,
step=step_value),
ctx=ast.Load(),
lineno=lineno,
col_offset=0)
示例12: visit_Slice
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Slice [as 別名]
def visit_Slice(self, sl: ast.Slice) -> VisitSliceReturnT:
actions = []
if sl.lower is None:
lower_flattened = None
else:
lower_flattened, lower_actions = self.visit_expr(sl.lower)
actions.extend(lower_actions)
if sl.upper is None:
upper_flattened = None
else:
upper_flattened, upper_actions = self.visit_expr(sl.upper)
actions.extend(upper_actions)
if sl.step is None:
step_flattened = None
else:
step_flattened, step_actions = self.visit_expr(sl.step)
actions.extend(step_actions)
return ast.Slice(lower=lower_flattened, upper=upper_flattened, step=step_flattened), actions
示例13: _Subscript
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Slice [as 別名]
def _Subscript(self, t):
if isinstance(t.slice, ast.Index):
#self.dispatch(t.value)
#self.write("[")
#self.dispatch(t.slice)
#self.write("]")
self.write('pyphp_subscript(')
self.dispatch(t.value)
self.write(', ')
self.dispatch(t.slice)
self.write(')')
elif isinstance(t.slice, ast.Slice):
self.write('array_slice(')
self.dispatch(t.value)
self.write(', ')
self.dispatch(t.slice)
self.write(')')
示例14: visit_Slice
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Slice [as 別名]
def visit_Slice(self, node: ast.Slice) -> None:
# actual slicing
print(f"Slice: {node}")
print(ast.dump(node))
self.generic_visit(node)
示例15: visit_Subscript
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Slice [as 別名]
def visit_Subscript(self, node: ast.Subscript) -> None:
# iterable indexing, has Slice operations
print(f"Subscript: {node}")
print(ast.dump(node))
self.generic_visit(node)
# IN DEVELOPMENT
################################################################################################
# FUTURE DEVELOPMENT
################################################################################################