我们从Python开源项目中,提取了以下6个代码示例,用于说明如何使用ast.Del()。
def checkGlobalIds(a, l): if not isinstance(a, ast.AST): return elif type(a) in [ ast.Load, ast.Store, ast.Del, ast.AugLoad, ast.AugStore, ast.Param ]: return if not hasattr(a, "global_id"): addedNodes = ["propagatedVariable", "orderedBinOp", "augAssignVal", "augAssignBinOp", "combinedConditional", "combinedConditionalOp", "multiCompPart", "multiCompOp", "second_global_id", "moved_line", # above this line has individualize functions. below does not. "addedNot", "addedNotOp", "addedOther", "addedOtherOp", "collapsedExpr", "removedLines", "helperVar", "helperReturn", "typeCastFunction", ] for t in addedNodes: if hasattr(a, t): break else: # only enter the else if none of the provided types are an attribute of a log("canonicalize\tcheckGlobalIds\tNo global id: " + str(l) + "," + str(a.__dict__) + "," + printFunction(a, 0), "bug") for f in ast.iter_child_nodes(a): checkGlobalIds(f, l + [type(a)])
def to_source_any(n): """ Convert AST node to string, handling all node types, without fixing comments. """ try: return astor.to_source(n) except AttributeError: pass cls = n.__class__ if cls in astor.misc.all_symbols: return astor.misc.all_symbols[cls] def wrap(s): return '___' + s + '___' extra_d = {ast.Load: wrap('load'), ast.Store: wrap('store'), ast.Del: wrap('del'), ast.AugLoad: wrap('augload'), ast.AugStore: wrap('augstore'), ast.Param: wrap('param'), ast.keyword: wrap('keyword')} if cls in extra_d: return extra_d[cls] raise AttributeError('unknown node type {}'.format(cls))
def NAME(self, node): """ Handle occurrence of Name (which can be a load/store/delete access.) """ # Locate the name in locals / function / globals scopes. if isinstance(node.ctx, (ast.Load, ast.AugLoad)): self.handleNodeLoad(node) if (node.id == 'locals' and isinstance(self.scope, FunctionScope) and isinstance(node.parent, ast.Call)): # we are doing locals() call in current scope self.scope.usesLocals = True elif isinstance(node.ctx, (ast.Store, ast.AugStore)): self.handleNodeStore(node) elif isinstance(node.ctx, ast.Del): self.handleNodeDelete(node) else: # must be a Param context -- this only happens for names in function # arguments, but these aren't dispatched through here raise RuntimeError("Got impossible expression context: %r" % (node.ctx,))
def visit_Name(self, node): if isinstance(node.ctx, ast.Load): self.loaded.add(node.id) elif isinstance(node.ctx, ast.Store): self.stored.add(node.id) elif isinstance(node.ctx, ast.Del): self.deleted.add(node.id) # Sample usage