本文整理匯總了Python中ast.NotIn方法的典型用法代碼示例。如果您正苦於以下問題:Python ast.NotIn方法的具體用法?Python ast.NotIn怎麽用?Python ast.NotIn使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類ast
的用法示例。
在下文中一共展示了ast.NotIn方法的10個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: visit_Compare
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import NotIn [as 別名]
def visit_Compare(self, node):
"""Compare nodes occur for all sequences of comparison (`in`, gt, lt, etc.)
operators. We only want to match `___ in instanceof(dict)` here, so we
restrict this to Compare ops with a single operator which is `In` or
`NotIn`.
"""
node = self.generic_visit(node)
if len(node.ops) == 1 and isinstance(node.ops[0], (ast.In, ast.NotIn)):
cmps = node.comparators
if len(cmps) == 1 and self._is_valid_resolved(cmps[0]):
rslvd = cmps[0]
if isinstance(rslvd.value, dict):
node = ast.Compare(
node.left,
node.ops,
[_resolved(rslvd.representation+".keys()",
sorted(rslvd.value.keys()),
valid=False)])
return node
示例2: comp_op_rewrite
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import NotIn [as 別名]
def comp_op_rewrite(op: t.Union[Tokenizer, t.List[Tokenizer]]):
"""
('<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not')
"""
if isinstance(op, list):
op = tuple(map(lambda it: it.value, op))
else:
op = op.value
return {
'<': ast.Lt,
'>': ast.Gt,
'==': ast.Eq,
'>=': ast.GtE,
'<=': ast.LtE,
'<>': lambda: raise_exp(NotImplemented),
'!=': ast.NotEq,
'in': ast.In,
('is', ): ast.Is,
('is', 'not'): ast.IsNot,
('not', 'in'): ast.NotIn,
}[op]()
示例3: visit_Assert
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import NotIn [as 別名]
def visit_Assert(self, node):
if is_binary_comparison(node):
if is_comparison_type(node, ast.Eq):
return make_call_node(node, assert_equal.__name__)
elif is_comparison_type(node, ast.NotEq):
return make_call_node(node, assert_not_equal.__name__)
elif is_comparison_type(node, ast.In):
return make_call_node(node, assert_in.__name__)
elif is_comparison_type(node, ast.NotIn):
return make_call_node(node, assert_not_in.__name__)
elif is_comparison_type(node, ast.Is):
return make_call_node(node, assert_is.__name__)
elif is_comparison_type(node, ast.IsNot):
return make_call_node(node, assert_is_not.__name__)
elif is_comparison_type(node, ast.Lt):
return make_call_node(node, assert_less_than.__name__)
elif is_comparison_type(node, ast.LtE):
return make_call_node(node, assert_less_than_equal_to.__name__)
elif is_comparison_type(node, ast.Gt):
return make_call_node(node, assert_greater_than.__name__)
elif is_comparison_type(node, ast.GtE):
return make_call_node(node, assert_greater_than_equal_to.__name__)
return node
示例4: invert
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import NotIn [as 別名]
def invert(node):
"""
Invert the operation in an ast node object (get its negation).
Args:
node: An ast node object.
Returns:
An ast node object containing the inverse (negation) of the input node.
"""
inverse = {ast.Eq: ast.NotEq,
ast.NotEq: ast.Eq,
ast.Lt: ast.GtE,
ast.LtE: ast.Gt,
ast.Gt: ast.LtE,
ast.GtE: ast.Lt,
ast.Is: ast.IsNot,
ast.IsNot: ast.Is,
ast.In: ast.NotIn,
ast.NotIn: ast.In}
if type(node) == ast.Compare:
op = type(node.ops[0])
inverse_node = ast.Compare(left=node.left, ops=[inverse[op]()],
comparators=node.comparators)
elif isinstance(node, ast.BinOp) and type(node.op) in inverse:
op = type(node.op)
inverse_node = ast.BinOp(node.left, inverse[op](), node.right)
elif type(node) == ast.NameConstant and node.value in [True, False]:
inverse_node = ast.NameConstant(value=not node.value)
else:
inverse_node = ast.UnaryOp(op=ast.Not(), operand=node)
return inverse_node
示例5: visit_Compare
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import NotIn [as 別名]
def visit_Compare(self, node: ast.Compare) -> Any:
"""Recursively visit the comparators and apply the operations on them."""
# pylint: disable=too-many-branches
left = self.visit(node=node.left)
comparators = [self.visit(node=comparator) for comparator in node.comparators]
result = None # type: Optional[Any]
for comparator, op in zip(comparators, node.ops):
if isinstance(op, ast.Eq):
comparison = left == comparator
elif isinstance(op, ast.NotEq):
comparison = left != comparator
elif isinstance(op, ast.Lt):
comparison = left < comparator
elif isinstance(op, ast.LtE):
comparison = left <= comparator
elif isinstance(op, ast.Gt):
comparison = left > comparator
elif isinstance(op, ast.GtE):
comparison = left >= comparator
elif isinstance(op, ast.Is):
comparison = left is comparator
elif isinstance(op, ast.IsNot):
comparison = left is not comparator
elif isinstance(op, ast.In):
comparison = left in comparator
elif isinstance(op, ast.NotIn):
comparison = left not in comparator
else:
raise NotImplementedError("Unhandled op of {}: {}".format(node, op))
if result is None:
result = comparison
else:
result = result and comparison
left = comparator
self.recomputed_values[node] = result
return result
示例6: p_comp_op_8
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import NotIn [as 別名]
def p_comp_op_8(p):
'''comp_op : NOT IN'''
# 1 2
p[0] = ast.NotIn(rule=inspect.currentframe().f_code.co_name)
示例7: visit_Compare
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import NotIn [as 別名]
def visit_Compare(self, node):
left = self.visit(node.left)
right = self.visit(node.comparators[0])
if isinstance(node.ops[0], ast.In):
return "{0}.iter().any(|&x| x == {1})".format(right, left) #is it too much?
elif isinstance(node.ops[0], ast.NotIn):
return "{0}.iter().all(|&x| x != {1})".format(right, left) #is it even more?
return super(RustTranspiler, self).visit_Compare(node)
示例8: visit_Compare
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import NotIn [as 別名]
def visit_Compare (self, node):
if len (node.comparators) > 1:
self.emit ('(')
left = node.left
for index, (op, right) in enumerate (zip (node.ops, node.comparators)):
if index:
self.emit (' && ')
if type (op) in (ast.In, ast.NotIn) or (self.allowOperatorOverloading and type (op) in (
ast.Eq, ast.NotEq, ast.Lt, ast.LtE, ast.Gt, ast.GtE
)):
self.emit ('{} ('.format (self.filterId (
# Non-overloaded
'__in__' if type (op) == ast.In else
'!__in__' if type (op) == ast.NotIn else
# Overloaded
'__eq__' if type (op) == ast.Eq else
'__ne__' if type (op) == ast.NotEq else
'__lt__' if type (op) == ast.Lt else
'__le__' if type (op) == ast.LtE else
'__gt__' if type (op) == ast.Gt else
'__ge__' if type (op) == ast.GtE else
'Never here'
)))
self.visitSubExpr (node, left)
self.emit (', ')
self.visitSubExpr (node, right)
self.emit (')')
else:
self.visitSubExpr (node, left)
self.emit (' {0} '.format (self.operators [type (op)][0]))
self.visitSubExpr (node, right)
left = right
if len (node.comparators) > 1:
self.emit(')')
示例9: visit_Compare
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import NotIn [as 別名]
def visit_Compare(self, node: ast.Compare) -> ast.AST:
"""Compare nodes are ``==, >=, is, in`` etc. There are multiple Compare categories."""
self.generic_visit(node)
log_header = f"visit_Compare: {self.src_file}:"
# taking only the first operation in the compare node
# in basic testing, things like (a==b)==1 still end up with lists of 1,
# but since the AST docs specify a list of operations this seems safer.
# idx = LocIndex("CompareIs", node.lineno, node.col_offset, type(node.ops[0]))
cmpop_is_types: Set[type] = {ast.Is, ast.IsNot}
cmpop_in_types: Set[type] = {ast.In, ast.NotIn}
op_type = type(node.ops[0])
node_span = NodeSpan(node)
locidx_kwargs = {
"lineno": node_span.lineno,
"col_offset": node_span.col_offset,
"op_type": op_type,
"end_lineno": node_span.end_lineno,
"end_col_offset": node_span.end_col_offset,
}
if op_type in cmpop_is_types:
idx = LocIndex(ast_class="CompareIs", **locidx_kwargs) # type: ignore
elif op_type in cmpop_in_types:
idx = LocIndex(ast_class="CompareIn", **locidx_kwargs) # type: ignore
else:
idx = LocIndex(ast_class="Compare", **locidx_kwargs) # type: ignore
self.locs.add(idx)
if idx == self.target_idx and self.mutation and not self.readonly:
LOGGER.debug("%s mutating idx: %s with %s", log_header, self.target_idx, self.mutation)
# TODO: Determine when/how this case would actually be called
if len(node.ops) > 1:
# unlikely test case where the comparison has multiple values
LOGGER.debug("%s multiple compare ops in node, len: %s", log_header, len(node.ops))
existing_ops = [i for i in node.ops]
mutation_ops = [self.mutation()] + existing_ops[1:]
return ast.copy_location(
ast.Compare(left=node.left, ops=mutation_ops, comparators=node.comparators),
node,
)
else:
# typical comparison case, will also catch (a==b)==1 as an example.
LOGGER.debug("%s single comparison node operation", log_header)
return ast.copy_location(
ast.Compare(
left=node.left, ops=[self.mutation()], comparators=node.comparators
),
node,
)
LOGGER.debug("%s (%s, %s): no mutations applied.", log_header, node.lineno, node.col_offset)
return node
示例10: _translate_compare
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import NotIn [as 別名]
def _translate_compare(self, left, ops, comparators, location):
if isinstance(ops[0], ast.In) or isinstance(ops[0], ast.NotIn):
if len(comparators) != 1:
raise translation_error('only <element> [not] in <sequence> supported',
location, self.lines[location[0]],
suggestion='2 in [2] in [[2]] is cute, but it\'s not supported')
else:
in_node = self._translate_in(left, comparators[0], location)
if isinstance(ops[0], ast.In):
return in_node
else:
return {'type': 'unary_op', 'op': 'not', 'value': in_node, 'pseudo_type': 'Boolean'}
op = PSEUDO_OPS[type(ops[0])]
right_node = self._translate_node(comparators[0])
left_node = self._translate_node(left)
self._confirm_comparable(op, left_node['pseudo_type'], right_node['pseudo_type'], location)
result = {
'type': 'comparison',
'op': op,
'left': left_node,
'right': right_node,
'pseudo_type': 'Boolean'
}
if len(comparators) == 1:
return result
else:
for r in comparators[1:]:
left_node, right_node = right_node, self._translate_node(r)
self._confirm_comparable(op, left_node['pseudo_type'], right_node['pseudo_type'], location)
result = {
'type': 'binary_op',
'op': 'and',
'left': result,
'right': {
'type': 'comparison',
'op': op,
'left': left_node,
'right': right_node,
'pseudo_type': 'Boolean'
},
'pseudo_type': 'Boolean'
}
return result