def grab(tree, **kw): keywords = [keyword(k.id, Name(k.id, Load())) for k in tree.elts] return Call( Name('dict', Load()), [], keywords, )
def name_constant(name): """AST constant for True, False, and None.""" # Python 3 uses NameConstant for True, False and None, # while python 2 uses (assignable) variables named "True", "False" and "None". return Name(id=name, ctx=Load()) if is_py2 else \ NameConstant(value=True) if 'True' == name else \ NameConstant(value=False) if 'False' == name else \ NameConstant(value=None) if 'None' == name else \ Name(id=name, ctx=Load())
def deslicify(subscript): ''' Rewrite a subscript expression as a call to the builtin `slice` function. The result should be what your `__getitem__` method would see. :param subscript: The AST node for a subscript expression. :return: A literal equivalent to the subscript expression. ''' def fix_none(node): if node is None: return name_constant(None, loc=subscript) else: return node def fix_slice(node): if isinstance(node, Slice): return call6(func=_slice, args=[fix_none(node.lower), fix_none(node.upper), fix_none(node.step)], loc=node) else: return fix_none(node) if isinstance(subscript, Index): return subscript.value elif isinstance(subscript, ExtSlice): elems = map(fix_slice, subscript.dims) return cl(Tuple(elts=list(elems), ctx=Load()), subscript) elif isinstance(subscript, Slice): return fix_slice(subscript) else: raise TypeError('Expected {} to be a subscript expression.'.format(dump(subscript)))
def decorate(fun, args): """Create a decorator call. To get jit(nopython=True), call decorate('jit', {"nopython": "True"}""" return Call(func=Name(id=fun, ctx=Load()), args=[], keywords=list( map(lambda kv: keyword(arg=kv[0], value=read(kv[1])), itr(args))), starargs=None, kwargs=None)
def __call__(self, source, filename, symbol): tree = ast.parse(source) required_pkgs = _detect_macros(tree) for p in required_pkgs: __import__(p) self.modules.update(sys.modules[p] for p in required_pkgs) tree = _expand_ast(tree, self.modules) tree = _ast_ctx_fixer.recurse(tree, Load()) fill_line_numbers(tree, 0, 0) tree = ast.Interactive(tree.body) codeob = compile(tree, filename, symbol, self.flags, 1) for feature in _features: if codeob.co_flags & feature.compiler_flag: self.flags |= feature.compiler_flag return codeob
def field_read(scope, field): """AST field read.""" scope = scope if isinstance(scope, AST) or isinstance(scope, arg) else read(scope) return Attribute(value=scope, attr=field, ctx=Load())
def read_false(): """AST read a False.""" return Name(id='False', ctx=Load()) if is_py2 else NameConstant( value=False)
def read_true(): """AST read a True.""" return Name(id='True', ctx=Load()) if is_py2 else NameConstant(value=True)
def read_none(): """AST read a None.""" return Name(id='None', ctx=Load()) if is_py2 else NameConstant(value=None)
def read(name): """AST read.""" return Name(id=name.id, ctx=Load()) if isinstance(name, Name) else \ name_constant(name) if isinstance(name, str) else \ Name(id=name.arg, ctx=Load()) if isinstance(name, arg) else name
def array_access(var_name, index): """AST array access.""" var_name = read(var_name) # var_name if isinstance(var_name, AST) else read(var_name) if is_py2 else arg(arg=var_name, annotation=None) index = index if isinstance(index, AST) else Num(index) return Subscript(value=var_name, ctx=Load(), slice=Index(value=index))
def new_list(items): """AST list.""" return List( ctx=Load(), elts=to_ast(items) if isinstance(items, list) else [to_ast(items)])
def assign(target, value): """AST assignment.""" # Convert strings to Name(id=target, ctx=Store()) target = target if isinstance(target, AST) else write(target) return Assign(targets=[target], ctx=Load(), value=value)