我们从Python开源项目中,提取了以下5个代码示例,用于说明如何使用_ast.IfExp()。
def process_ifexpr(self, node): if isinstance(node, _ast.If): test = node.test then = node.body else_ = node.orelse assert len(then) == 1 then = then[0] assert len(else_) == 1 else_ = else_[0] if_exp = _ast.IfExp(test, then, else_, lineno=node.lineno, col_offset=0) return if_exp else: return node
def handleNodeDelete(self, node): def on_conditional_branch(): """ Return `True` if node is part of a conditional body. """ current = getattr(node, 'parent', None) while current: if isinstance(current, (ast.If, ast.While, ast.IfExp)): return True current = getattr(current, 'parent', None) return False name = getNodeName(node) if not name: return if on_conditional_branch(): # We cannot predict if this conditional branch is going to # be executed. return if isinstance(self.scope, FunctionScope) and name in self.scope.globals: self.scope.globals.remove(name) else: try: del self.scope[name] except KeyError: self.report(messages.UndefinedName, node, name)
def _(assignment, **kwargs): if isinstance(assignment.value, (_ast.IfExp)): return parse(assignment.value, name=assignment.targets[0]) else: return '({};) -> {};'.format( parse(assignment.value), parse(assignment.targets[0]) )
def handleNodeDelete(self, node): def on_conditional_branch(): """ Return `True` if node is part of a conditional body. """ current = getattr(node, 'parent', None) while current: if isinstance(current, (ast.If, ast.While, ast.IfExp)): return True current = getattr(current, 'parent', None) return False name = getNodeName(node) if not name: return if on_conditional_branch(): # We can not predict if this conditional branch is going to # be executed. return if isinstance(self.scope, FunctionScope) and name in self.scope.globals: self.scope.globals.remove(name) else: try: del self.scope[name] except KeyError: self.report(messages.UndefinedName, node, name)