def __repr__(self): s = "IArg for " + self.arg_name + ":\n" s += "-" * 20 + "\n" for d in self.defs: s += astutils.to_source(d).strip() + "\n" s += "return " + astutils.to_source(self.expr).strip() + "\n" s += "-" * 20 + "\n" s += "Dependencies : " + str(self.dependencies) + "\n" s += "-" * 20 + "\n" return s
def get_inp_out_types(gen_ast: ast.FunctionDef) -> Tuple[str, str]: inp_types: str = None out_types: str = None for decorator in getattr(gen_ast, 'decorator_list', []): if isinstance(decorator, ast.Call) and decorator.func.id == 'inp_types': if len(decorator.args) == 1 and isinstance(decorator.args[0], ast.List): inp_types = astutils.to_source(decorator.args[0]) elif isinstance(decorator, ast.Call) and decorator.func.id == 'out_types': if len(decorator.args) == 1 and isinstance(decorator.args[0], ast.List): out_types = astutils.to_source(decorator.args[0]) if inp_types is None or out_types is None: raise Exception("Could not infer input/output types") return inp_types, out_types
def compile_gen(igen: IGenerator) -> ast.ClassDef: """ The compiler converts the internal representation into a class that can be used for enumeration, much like the 'Unit' class in autopandas-v1 """ # First instantiate the raw class gen_class: ast.ClassDef = astutils.parse( "class {}(BaseGenerator): pass".format(igen.name)) gen_class.body = [] # Add the argument generator definitions for arg_name in igen.enum_order: compile_iarg(arg_name, igen, gen_class) # Add an initialization method for the important generator information init_method: ast.FunctionDef = astutils.parse("def init(self): pass") init_method.body = [] init_method.body.append( astutils.parse("self.target = {}".format( astutils.to_source(igen.target).strip()))) init_method.body.append( astutils.parse("self.enum_order = {}".format(repr(igen.enum_order)))) init_method.body.append( astutils.parse("self.arg_names = {}".format(repr(igen.sig.arg_names)))) init_method.body.append( astutils.parse("self.default_vals = {}".format( repr(dict(igen.sig.kw_args))))) qual_name = igen.namespace + '.' + igen.name init_method.body.append( astutils.parse("self.qual_name = {}".format(repr(qual_name)))) init_method.body.append( astutils.parse("self.name = {}".format(repr(igen.name)))) init_method.body.append( astutils.parse("self.arity = {}".format(igen.arity))) init_method.body.append( astutils.parse("self.inp_types = {}".format(igen.inp_types))) init_method.body.append( astutils.parse("self.out_types = {}".format(igen.out_types))) init_method.body.append( astutils.parse("self.representation = {}".format( repr(igen.representation)))) gen_class.body.append(init_method) return gen_class
def get_extension_of(gen_ast: ast.FunctionDef) -> Optional[str]: for decorator in getattr(gen_ast, 'decorator_list', []): if isinstance(decorator, ast.Call) and decorator.func.id == 'extend': if len(decorator.args) == 1 and isinstance(decorator.args[0], ast.Attribute): return astutils.to_source(decorator.args[0]).strip()