def test__set_name_and_type(self) -> None: """ Tests that `_set_name_and_type` parsed AST code into a code str. Not working since I explicitly deleted the typ from ``` quoted defaults. Changed mock to match. """ self.assertTupleEqual( _set_name_and_type( ( "adder", { "default": BinOp( set_value(5), Mult(), set_value(5), ), }, ), infer_type=True, word_wrap=True, ), ("adder", { "default": "```(5 * 5)```" }), ) self.assertTupleEqual( _set_name_and_type( ( "adder", { "default": BinOp( set_value(5), Mult(), set_value(5), ), "doc": ["5", "b"], }, ), infer_type=True, word_wrap=True, ), ("adder", { "default": "```(5 * 5)```", "doc": "5b" }), )
def mod_expr(expr, token, opclass): e = expr.replace("*", token) a = parse(e, mode="eval") for node in walk(a): if type(node) == BinOp and type(node.op) == opclass: node.op = Mult() return eval(compile(a, "", mode="eval"))
def zero_Multiplier(node): # 0 -> int(n * 0) return Call(func=Name(id='int', ctx=Load()), args=[ BinOp(left=Num(n=random.random()), right=Num(n=0), op=Mult()) ], keywords=[], starargs=None, kwargs=None)
def p_term(tokens): op = tokens[1].gettokentype() left = tokens[0] right = tokens[2] if (op == 'STAR'): return Mult(left, right) if (op == 'SLASH'): return Div(left, right) else: raise Exception(f"{tokens} passaram liso")
def _make_product(terms): """ Return an AST expressing the product of all the terms. """ if terms: product = terms[0] for term in terms[1:]: product = BinOp(left=product, op=Mult(), right=term) return product else: return Constant(value=1)
def token2obj(token): if token == '+': return Add() elif token == '-': return Sub() elif token == '*': return Mult() elif token == '/': return Div() try: return Num(int(token)) except: pass return Name(id=token)
def arith_expr(self, a): a, op, b = a op = { '+': Add(), '-': Sub(), '*': Mult(), '@': MatMult(), '/': Div(), '//': FloorDiv(), '%': Mod(), '&': BitAnd(), '|': BitOr(), '^': BitXor(), '<<': LShift(), '>>': RShift(), }.get(op, None) return BinOp(left=a, op=op, right=b)
def expression(p): left = p[0] right = p[2] operator = p[1] if operator.gettokentype() == 'PLUS': return Sum(left, right) elif operator.gettokentype() == 'MINUS': return Sub(left, right) elif operator.gettokentype() == 'MULT': return Mult(left, right) elif operator.gettokentype() == 'DIV': return Div(left, right) elif operator.gettokentype() == 'ASSIGN': return Assign(left, right) elif operator.gettokentype() == 'GREATERTHAN': return GreaterThan(left, right) elif operator.gettokentype() == 'LESSTHAN': return LessThan(left, right) elif operator.gettokentype() == 'EQUAL': return Equal(left, right)
def test_param2argparse_param_default_ast_binop(self) -> None: """ Tests that param2argparse_param works to change the type based on the default whence said default is a non specially handled ast.AST """ run_ast_test( gen_ast=param2argparse_param( ( "byo", { "default": BinOp( set_value(5), Mult(), set_value(5), ), "typ": "str", }, ), ), gold=Expr( Call( args=[set_value("--byo")], func=Attribute( Name("argument_parser", Load()), "add_argument", Load(), ), keywords=[ keyword(arg="required", value=set_value(True), identifier=None), keyword( arg="default", value=set_value("```(5 * 5)```"), identifier=None, ), ], expr=None, expr_func=None, ) ), test_case_instance=self, )
def expression_arg(p): left = p[0] right = p[2] operator = p[1] if isinstance(p[0], Identificador): if p[0].nome in self.vars.keys(): left = self.vars[p[0].nome] if isinstance(p[2], Identificador): if p[2].nome in self.vars.keys(): right = self.vars[p[2].nome] if operator.gettokentype() == 'ADD': return Add(left, right) elif operator.gettokentype() == 'SUB': return Sub(left, right) elif operator.gettokentype() == 'MULT': return Mult(left, right) elif operator.gettokentype() == 'DIV': return Div(left, right)
def _make_print(self, ns: List[expr], prefix: str = None) -> Expr: # create the indent: ' ' * depth mul_by = Name(self._DEPTH_VAR, Load()) indent = BinOp(Constant(" "), Mult(), mul_by) # if prefix is given, indent is: ' ' * (depth - len(prefix)) + prefix if prefix is not None: assert len( prefix ) <= self._INDENT, f"too long {prefix} for given indent {self._INDENT}" indent.right = BinOp(mul_by, Sub(), Constant(len(prefix))) indent = BinOp(indent, Add(), Constant(prefix)) return Expr( Call( Name("print", Load()), args=cast(List[expr], [indent]) + ns, keywords=[ keyword("sep", Constant("")), keyword("file", Attribute(Name("sys", Load()), "stderr", Load())), ], ))
def visit_AugAssign(self, ast_aug_assign: AugAssign): # need to do some trick of +=, -=, *=, /= if type(ast_aug_assign.op) == Add: new_op = BinOp(ast_aug_assign.target, Add(), ast_aug_assign.value) elif type(ast_aug_assign.op) == Sub: new_op = BinOp(ast_aug_assign.target, Sub(), ast_aug_assign.value) elif type(ast_aug_assign.op) == Mult: new_op = BinOp(ast_aug_assign.target, Mult(), ast_aug_assign.value) elif type(ast_aug_assign.op) == Div: new_op = BinOp(ast_aug_assign.target, Div(), ast_aug_assign.value) else: raise Exception("does not support operator: ", ast_aug_assign.op) ast_assign = Assign(ast_aug_assign.target, new_op) # create Big-O AST assign node assign_node = AssignNode() # coord = coordinate(ast_aug_assign.col_offset, ast_aug_assign.lineno) # self.set_coordinate(assign_node, coord) assign_node.target = self.visit(ast_assign.targets) assign_node.value = self.visit(ast_assign.value) return assign_node
value=set_value( "pass this as arguments to data_loader function", ), identifier=None, ), ], expr=None, expr_func=None, ) ), Expr( Call( args=[ BinOp( set_value(5), Mult(), set_value(5), ) ], func=Name("print", Load()), keywords=[], expr=None, expr_func=None, ) ), If( body=[ Expr( Call( args=[set_value(True)], func=Name("print", Load()),
def withFactor(factor, node): if not isinstance(factor, float): raise ValueError('Incompatible units!') if factor == 1.0: return node return BinOp(Num(factor), Mult(), node)
def replace_ops_1(node): if isinstance(node, BinOp) and isinstance(node.op, Sub): node.op = Mult(left=replace_ops_1(node.left), right=replace_ops_1(node.right)) elif isinstance(node, BinOp) and isinstance(node.op, Add): node.op = Add(left=replace_ops_1(node.left), right=replace_ops_1(node.right))
def visit_Sub(self, node): new_node = Mult() return self.replace(node, new_node)