Python ast.In() Examples
The following are 30
code examples of ast.In().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
ast
, or try the search function
.
Example #1
Source File: test_transformers.py From mutatest with MIT License | 6 votes |
def test_MutateAST_visit_compare(idx, mut_op, lineno, compare_file, compare_expected_locs): """Test mutation of the == to != in the compare op.""" tree = Genome(compare_file).ast # apply the mutation to the original tree copy testing_tree = deepcopy(tree) mutated_tree = MutateAST(target_idx=compare_expected_locs[idx], mutation=mut_op).visit( testing_tree ) # revisit in read-only mode to gather the locations of the new nodes mast = MutateAST(readonly=True) mast.visit(mutated_tree) assert len(mast.locs) == 3 # check that the lineno marked for mutation is changed, otherwise original ops should # still be present without modification for loc in mast.locs: if loc.lineno == lineno and loc.col_offset == 11: assert loc.op_type == mut_op else: assert loc.op_type in {ast.Eq, ast.Is, ast.In} # based on compare_file fixture
Example #2
Source File: magic_check_fn.py From recipes-py with Apache License 2.0 | 6 votes |
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
Example #3
Source File: helper.py From YAPyPy with MIT License | 6 votes |
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]()
Example #4
Source File: test_main.py From executing with MIT License | 6 votes |
def test_decode_source(self): def check(source, encoding, exception=None, matches=True): encoded = source.encode(encoding) if exception: with self.assertRaises(exception): Source.decode_source(encoded) else: decoded = Source.decode_source(encoded) if matches: self.assertEqual(decoded, source) else: self.assertNotEqual(decoded, source) check(u'# coding=utf8\né', 'utf8') check(u'# coding=gbk\né', 'gbk') check(u'# coding=utf8\né', 'gbk', exception=UnicodeDecodeError) check(u'# coding=gbk\né', 'utf8', matches=False) # In Python 3 the default encoding is assumed to be UTF8 if PY3: check(u'é', 'utf8') check(u'é', 'gbk', exception=SyntaxError)
Example #5
Source File: rewrite.py From ward with MIT License | 6 votes |
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
Example #6
Source File: transpiler.py From pyrs with MIT License | 5 votes |
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)
Example #7
Source File: pytables.py From predictive-maintenance-using-machine-learning with Apache License 2.0 | 5 votes |
def translate_In(self, op): return ast.Eq() if isinstance(op, ast.In) else op
Example #8
Source File: test_ast.py From ironpython3 with Apache License 2.0 | 5 votes |
def test_compare(self): left = ast.Name("x", ast.Load()) comp = ast.Compare(left, [ast.In()], []) self.expr(comp, "no comparators") comp = ast.Compare(left, [ast.In()], [ast.Num(4), ast.Num(5)]) self.expr(comp, "different number of comparators and operands") comp = ast.Compare(ast.Num("blah"), [ast.In()], [left]) self.expr(comp, "non-numeric", exc=TypeError) comp = ast.Compare(left, [ast.In()], [ast.Num("blah")]) self.expr(comp, "non-numeric", exc=TypeError)
Example #9
Source File: _recompute.py From icontract with MIT License | 5 votes |
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
Example #10
Source File: hgawk_grammar.py From histogrammar-python with Apache License 2.0 | 5 votes |
def p_comp_op_7(p): '''comp_op : IN''' # 1 p[0] = ast.In(rule=inspect.currentframe().f_code.co_name)
Example #11
Source File: pytables.py From Splunking-Crime with GNU Affero General Public License v3.0 | 5 votes |
def translate_In(self, op): return ast.Eq() if isinstance(op, ast.In) else op
Example #12
Source File: test_ast.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 5 votes |
def test_compare(self): left = ast.Name("x", ast.Load()) comp = ast.Compare(left, [ast.In()], []) self.expr(comp, "no comparators") comp = ast.Compare(left, [ast.In()], [ast.Num(4), ast.Num(5)]) self.expr(comp, "different number of comparators and operands") comp = ast.Compare(ast.Num("blah"), [ast.In()], [left]) self.expr(comp, "non-numeric", exc=TypeError) comp = ast.Compare(left, [ast.In()], [ast.Num("blah")]) self.expr(comp, "non-numeric", exc=TypeError)
Example #13
Source File: pytables.py From elasticintel with GNU General Public License v3.0 | 5 votes |
def translate_In(self, op): return ast.Eq() if isinstance(op, ast.In) else op
Example #14
Source File: test_ast.py From Fluid-Designer with GNU General Public License v3.0 | 5 votes |
def test_compare(self): left = ast.Name("x", ast.Load()) comp = ast.Compare(left, [ast.In()], []) self.expr(comp, "no comparators") comp = ast.Compare(left, [ast.In()], [ast.Num(4), ast.Num(5)]) self.expr(comp, "different number of comparators and operands") comp = ast.Compare(ast.Num("blah"), [ast.In()], [left]) self.expr(comp, "non-numeric", exc=TypeError) comp = ast.Compare(left, [ast.In()], [ast.Num("blah")]) self.expr(comp, "non-numeric", exc=TypeError)
Example #15
Source File: clike.py From pyrs with MIT License | 5 votes |
def visit_Compare(self, node): left = self.visit(node.left) op = self.visit(node.ops[0]) right = self.visit(node.comparators[0]) if isinstance(node.ops[0], ast.In): return "{0}.any({1})".format(right, left) return "{0} {1} {2}".format(left, op, right)
Example #16
Source File: gen.py From ChromeController with BSD 3-Clause "New" or "Revised" License | 5 votes |
def __build_conditional_arg_check(self, argname, argtype): target_value = ast.Subscript( value=ast.Name(id='kwargs', ctx=ast.Load()), slice=ast.Index(ast.Str(s=argname)), ctx=ast.Load() ) presence_check = ast.Call(func = ast.Name(id='isinstance', ctx=ast.Load()), args = [target_value, argtype], keywords = [], lineno = self.__get_line()) # Assumes that argtype is a ast.Tuple of ast.Name items types = [t.id for t in argtype.elts] check_message = ast.BinOp( left = ast.Str(s='Optional argument \'{}\' must be of type \'{}\'. Received type: \'%s\''.format(argname, types)), op = ast.Mod(), right = ast.Call(func=ast.Name(id='type', ctx=ast.Load()), args=[target_value], keywords=[]), lineno = self.__get_line()) assert_check = ast.Assert( test = presence_check, msg = check_message, lineno = self.__get_line()) check_body = [assert_check] check = ast.Compare(left=ast.Str(s=argname, ctx=ast.Load()), ops=[ast.In()], comparators=[ast.Name(id='kwargs', ctx=ast.Load())]) new_ret = ast.If( test = check, body = check_body, orelse = [], lineno = self.__get_line()) return new_ret
Example #17
Source File: compiler.py From Transcrypt with Apache License 2.0 | 5 votes |
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(')')
Example #18
Source File: compiler.py From Transcrypt with Apache License 2.0 | 5 votes |
def visit_Dict (self, node): if not self.allowJavaScriptKeys: # If we don't want JavaScript treatment of keys, for literal keys it doesn't make a difference for key in node.keys: if not type (key) in (ast.Str, ast.Num): # but if there's only one non-literal key there's a difference, and all keys are treated the Python way self.emit ('dict ([') for index, (key, value) in enumerate (zip (node.keys, node.values)): self.emitComma (index) self.emit ('[') self.visit (key) # In a JavaScript list, name is evaluated as variable or function call to produce a key self.emit (', ') self.visit (value) self.emit (']') self.emit ('])') return if self.allowJavaScriptIter: self.emit ('{{') else: self.emit ('dict ({{') # Since we didn't return, we want identifier keys to be treated as string literals for index, (key, value) in enumerate (zip (node.keys, node.values)): self.emitComma (index) self.idFiltering = False # The key may be a string or an identifier, the latter normally would be filtered, which we don't want self.visit (key) # In a JavaScript object literal, an identifier isn't evaluated but literally taken to be a key. self.idFiltering = True self.emit (': ') self.visit (value) if self.allowJavaScriptIter: self.emit ('}}') else: self.emit ('}})')
Example #19
Source File: subscripts.py From wemake-python-styleguide with MIT License | 5 votes |
def _check_implicit_get(self, node: ast.If) -> None: if not isinstance(node.test, ast.Compare): return if not isinstance(node.test.ops[0], ast.In): return checked_key = source.node_to_string(node.test.left) checked_collection = source.node_to_string(node.test.comparators[0]) for sub in ast.walk(node): if not isinstance(sub, ast.Subscript): continue if slices.is_same_slice(checked_collection, checked_key, sub): self.add_violation(refactoring.ImplicitDictGetViolation(sub))
Example #20
Source File: rewrite.py From python-netsurv with MIT License | 5 votes |
def push_format_context(self): """Create a new formatting context. The format context is used for when an explanation wants to have a variable value formatted in the assertion message. In this case the value required can be added using .explanation_param(). Finally .pop_format_context() is used to format a string of %-formatted values as added by .explanation_param(). """ self.explanation_specifiers = {} self.stack.append(self.explanation_specifiers)
Example #21
Source File: conftest.py From mutatest with MIT License | 5 votes |
def compare_expected_locs(): """The compare expected locations based on the fixture""" # Py 3.7 if sys.version_info < (3, 8): return [ LocIndex(ast_class="Compare", lineno=2, col_offset=11, op_type=ast.Eq), LocIndex(ast_class="CompareIs", lineno=5, col_offset=11, op_type=ast.Is), LocIndex(ast_class="CompareIn", lineno=8, col_offset=11, op_type=ast.In), ] # Py 3.8 return [ LocIndex( ast_class="Compare", lineno=2, col_offset=11, op_type=ast.Eq, end_lineno=2, end_col_offset=17, ), LocIndex( ast_class="CompareIs", lineno=5, col_offset=11, op_type=ast.Is, end_lineno=5, end_col_offset=17, ), LocIndex( ast_class="CompareIn", lineno=8, col_offset=11, op_type=ast.In, end_lineno=8, end_col_offset=17, ), ] #################################################################################################### # TRANSFORMERS: IF FIXTURES ####################################################################################################
Example #22
Source File: magic_check_fn.py From recipes-py with Apache License 2.0 | 5 votes |
def _process_frame(cls, frame, ignore_set, with_vars, additional_varmap=None): """This processes a stack frame into an expect_tests.CheckFrame, which includes file name, line number, function name (of the function containing the frame), the parsed statement at that line, and the relevant local variables/subexpressions (if with_vars is True). In addition to transforming the expression with _checkTransformer, this will: * omit subexpressions which resolve to callable()'s * omit the overall step ordered dictionary * transform all subexpression values using render_user_value(). """ nodes = cls._get_statements_for_frame(frame) raw_frame, filename, lineno, func_name, _, _ = frame varmap = None if with_vars: varmap = dict(additional_varmap or {}) xfrmr = _checkTransformer(raw_frame.f_locals, raw_frame.f_globals) xfrmd = xfrmr.visit(ast.Module(copy.deepcopy(nodes))) for n in itertools.chain(ast.walk(xfrmd), xfrmr.extras): if isinstance(n, _resolved): val = n.value if isinstance(val, ast.AST): continue if n.representation in ('True', 'False', 'None'): continue if callable(val) or id(val) in ignore_set: continue if n.representation not in varmap: varmap[n.representation] = render_user_value(val) return CheckFrame( filename, lineno, func_name, '; '.join(astunparse.unparse(n).strip() for n in nodes), varmap )
Example #23
Source File: pytables.py From recruit with Apache License 2.0 | 5 votes |
def translate_In(self, op): return ast.Eq() if isinstance(op, ast.In) else op
Example #24
Source File: rewrite.py From python-netsurv with MIT License | 5 votes |
def push_format_context(self): """Create a new formatting context. The format context is used for when an explanation wants to have a variable value formatted in the assertion message. In this case the value required can be added using .explanation_param(). Finally .pop_format_context() is used to format a string of %-formatted values as added by .explanation_param(). """ self.explanation_specifiers = {} self.stack.append(self.explanation_specifiers)
Example #25
Source File: rewrite.py From python-netsurv with MIT License | 5 votes |
def visit_Name(self, name): # Display the repr of the name if it's a local variable or # _should_repr_global_name() thinks it's acceptable. locs = ast.Call(self.builtin("locals"), [], []) inlocs = ast.Compare(ast.Str(name.id), [ast.In()], [locs]) dorepr = self.helper("_should_repr_global_name", name) test = ast.BoolOp(ast.Or(), [inlocs, dorepr]) expr = ast.IfExp(test, self.display(name), ast.Str(name.id)) return name, self.explanation_param(expr)
Example #26
Source File: rewrite.py From pytest with MIT License | 5 votes |
def visit_Name(self, name: ast.Name) -> Tuple[ast.Name, str]: # Display the repr of the name if it's a local variable or # _should_repr_global_name() thinks it's acceptable. locs = ast.Call(self.builtin("locals"), [], []) inlocs = ast.Compare(ast.Str(name.id), [ast.In()], [locs]) dorepr = self.helper("_should_repr_global_name", name) test = ast.BoolOp(ast.Or(), [inlocs, dorepr]) expr = ast.IfExp(test, self.display(name), ast.Str(name.id)) return name, self.explanation_param(expr)
Example #27
Source File: _ast_to_ir2.py From tmppy with Apache License 2.0 | 5 votes |
def compare_ast_to_ir2(ast_node: ast.Compare, compilation_context: CompilationContext, in_match_pattern: bool, check_var_reference: Callable[[ast.Name], None], match_lambda_argument_names: Set[str], current_stmt_line: int): if len(ast_node.ops) != 1 or len(ast_node.comparators) != 1: raise CompilationError(compilation_context, ast_node, 'Comparison not supported.') # pragma: no cover if in_match_pattern: raise CompilationError(compilation_context, ast_node, 'Comparisons are not allowed in match patterns') lhs = ast_node.left op = ast_node.ops[0] rhs = ast_node.comparators[0] if isinstance(op, ast.Eq): return eq_ast_to_ir2(lhs, rhs, compilation_context, in_match_pattern, check_var_reference, current_stmt_line) elif isinstance(op, ast.NotEq): return not_eq_ast_to_ir2(lhs, rhs, compilation_context, in_match_pattern, check_var_reference, current_stmt_line) elif isinstance(op, ast.In): return in_ast_to_ir2(lhs, rhs, compilation_context, in_match_pattern, check_var_reference, current_stmt_line) elif isinstance(op, ast.Lt): return int_comparison_ast_to_ir2(lhs, rhs, '<', compilation_context, in_match_pattern, check_var_reference, current_stmt_line) elif isinstance(op, ast.LtE): return int_comparison_ast_to_ir2(lhs, rhs, '<=', compilation_context, in_match_pattern, check_var_reference, current_stmt_line) elif isinstance(op, ast.Gt): return int_comparison_ast_to_ir2(lhs, rhs, '>', compilation_context, in_match_pattern, check_var_reference, current_stmt_line) elif isinstance(op, ast.GtE): return int_comparison_ast_to_ir2(lhs, rhs, '>=', compilation_context, in_match_pattern, check_var_reference, current_stmt_line) else: raise CompilationError(compilation_context, ast_node, 'Comparison not supported.') # pragma: no cover
Example #28
Source File: pytables.py From vnpy_crypto with MIT License | 5 votes |
def translate_In(self, op): return ast.Eq() if isinstance(op, ast.In) else op
Example #29
Source File: builder.py From staticfg with Apache License 2.0 | 5 votes |
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
Example #30
Source File: pytables.py From Computable with MIT License | 5 votes |
def translate_In(self, op): return ast.Eq() if isinstance(op, ast.In) else op