def fix_block(body, returns=True, prints=False): # if not isinstance(body[0], ast.Global): body.insert(0, ast.Global(names=['it'])) # body.insert(1, kast.setter(name('it'),kast.none)) last_statement = body[-1] if isinstance(last_statement, list) and len(last_statement) == 1: last_statement = last_statement[0] print("HOW??") if not isinstance( last_statement, (ast.Assign, ast.If, nodes.FunctionDef, ast.Return, ast.Assert)): if isinstance(last_statement, kast.Print): body[-1] = (assign("it", last_statement.values[0])) last_statement.values[0] = name("it") body.append(last_statement) else: body[-1] = (assign("it", last_statement)) if isinstance(last_statement, ast.Assign): if not "it" in [x.id for x in last_statement.targets]: last_statement.targets.append(Name(id="it", ctx=Store())) if returns and not isinstance(body[-1], ast.Return): body.append(ast.Return(name("it"))) # if prints: # if py3:pass#body.append(kast.call("print", name("it"))) # else:body.append(Print(dest=None, values=[name("it")], nl=True)) # call symbolically! return body
def fix_block(body, returns=True, prints=False): def using_IT(last_statement): # YES, use 'it': as namespace in exec(code) because it doesn't return directly return not isinstance(last_statement, (ast.Assign, ast.If, nodes.FunctionDef, ast.Return, ast.Assert, ast.While)) # if using_IT(body): # body.insert(0, ast.Global(names=['it'])) last_statement = body[-1] if isinstance(last_statement, list) and len(last_statement) == 1: last_statement = last_statement[0] print("HOW??") # can be removed? body= [fix_expression(exp) for exp in body] if isinstance(last_statement,expr): body[-1] = (assign("it", last_statement)) if isinstance(last_statement, kast.Print): body[-1] = (assign("it", last_statement.values[0])) last_statement.values[0] = name("it") body.append(last_statement) if isinstance(last_statement, ast.Assign): if not "it" in [x.id for x in last_statement.targets]: last_statement.targets.append(Name(id="it",ctx=Store())) if returns and not isinstance(body[-1], ast.Return): body.append(ast.Return(name("it"))) # else: Module's dont return # if prints: # if py3:pass#body.append(kast.call("print", name("it"))) # else:body.append(Print(dest=None, values=[name("it")], nl=True)) # call symbolically! return body
def visit_BinOp(self, node): if isinstance(node.left , nodes.Variable): # node.left.context =_ast.Load() node.left=kast.name(str(node.left.name)) if isinstance(node.right, nodes.Variable): node.right.context=_ast.Load() # node.right= ast_magic.wrap_value(node.right) #why here? node.left= ast_magic.wrap_value(node.left) #why here? return node
def visit_BinOp(self, node): if isinstance(node.left , nodes.Variable): # node.left.context =_ast.Load() node.left=kast.name(node.left.name) if isinstance(node.right, nodes.Variable): node.right.context=_ast.Load() # node.right= ast_magic.wrap_value(node.right) #why here? node.left= ast_magic.wrap_value(node.left) #why here? return node
def __init__(self, func=None, arguments=None, object=None, *margs, **args): super(FunctionCall, self).__init__(*margs, **args) # self.args = [] # self.keywords = [] # self.kwargs = self.starargs = None if callable(func): func = func.__name__ # lulwoot keep self.method=func? if isinstance(func, FunctionDef): func = func.name # eek name->func! func = args['func'] if 'func' in args else func func = args['name'] if 'name' in args else func self.func = func self.name = func self.arguments = args['arguments'] if 'arguments' in args else arguments self.object = object # self. = args['object'] if 'object' in args else object NOOO, MESSES # if 'object' in args and args['object']: self.object = args['object'] if 'scope' in args: self.scope = args['scope'] # if 'return_type' in args: self.returns = args['return_type'] if 'class' in args: self.clazz = args['class'] if 'module' in args: self.clazz = self.clazz or args['module'] # AST CONTENT: if isinstance(func, (str, extensions.unicode)): func = kast.name(func) if not isinstance(func, kast.Name): raise Exception("NOT A NAME %s" % func) self.targets = [kast.Name(id="it", ctx=ast.Store())] if self.arguments == None: self.arguments = [] elif not isinstance(self.arguments, (list, dict)): self.arguments = [self.arguments] # if not isinstance(self.arguments,dict): # self.arguments=map(emitters.kast_emitter.wrap_value,self.arguments) if 'returns' in args: self.returns = self.return_type = args[ 'returns'] # = self.return_typeS plural? else: self.returns = self.return_type = self.resolve_return_type() if (self.object): # x.y(z) self.value = kast.call_attribute(kast.name( self.object), func.id, self.arguments) # ast.Call(func=name, else: # y(z) self.value = kast.call(func, self.arguments) # ast.Call(func=name,
def __init__(self, func=None, arguments=None, object=None, *margs, **args): super(FunctionCall, self).__init__(*margs, **args) # self.args = [] # self.keywords = [] # self.kwargs = self.starargs = None if callable(func): func = func.__name__ # lulwoot keep self.method=func? if isinstance(func, FunctionDef): func = func.name # eek name->func! func = args['func'] if 'func' in args else func func = args['name'] if 'name' in args else func self.func = func self.name = func self.arguments = args['arguments'] if 'arguments' in args else arguments self.object = object # self. = args['object'] if 'object' in args else object NOOO, MESSES # if 'object' in args and args['object']: self.object = args['object'] if 'scope' in args: self.scope = args['scope'] # if 'return_type' in args: self.returns = args['return_type'] if 'class' in args: self.clazz = args['class'] if 'module' in args: self.clazz = self.clazz or args['module'] # AST CONTENT: if isinstance(func, (str, extensions.unicode)): func = kast.name(func) if not isinstance(func, kast.Name): raise Exception("NOT A NAME %s" % func) self.targets = [kast.Name(id="it", ctx=ast.Store())] if self.arguments == None: self.arguments = [] elif not isinstance(self.arguments, (list, dict)): self.arguments = [self.arguments] # if not isinstance(self.arguments,dict): # self.arguments=map(emitters.kast_emitter.wrap_value,self.arguments) if 'returns' in args: self.returns = self.return_type = args['returns'] # = self.return_typeS plural? else: self.returns = self.return_type = self.resolve_return_type() if (self.object): # x.y(z) self.value = kast.call_attribute(kast.name(self.object), func.id, self.arguments) # ast.Call(func=name, else: # y(z) self.value = kast.call(func, self.arguments) # ast.Call(func=name,
def fix_block(body, returns=True, prints=False): # if not isinstance(body[0], ast.Global): body.insert(0, ast.Global(names=['it'])) # body.insert(1, kast.setter(name('it'),kast.none)) last_statement = body[-1] if isinstance(last_statement, list) and len(last_statement) == 1: last_statement = last_statement[0] print("HOW??") if not isinstance(last_statement, (ast.Assign, ast.If, nodes.FunctionDef, ast.Return, ast.Assert)): if isinstance(last_statement, kast.Print): body[-1] = (assign("it", last_statement.values[0])) last_statement.values[0] = name("it") body.append(last_statement) else: body[-1] = (assign("it", last_statement)) if isinstance(last_statement, ast.Assign): if not "it" in [x.id for x in last_statement.targets]: last_statement.targets.append(Name(id="it",ctx=Store())) if returns and not isinstance(body[-1], ast.Return): body.append(ast.Return(name("it"))) # if prints: # if py3:pass#body.append(kast.call("print", name("it"))) # else:body.append(Print(dest=None, values=[name("it")], nl=True)) # call symbolically! return body
def fix_expression(exp): if isinstance(exp, nodes.Variable): return name(str(exp.name)) if isinstance(exp,expr) and not isinstance(exp,Expr) : return Expr(exp) # standalone Num, Call vs Assign(...Call) return exp
def visit_function(self, x): return name(x.__name__) # assign method via name