Python ast 模块,IfExp() 实例源码

我们从Python开源项目中,提取了以下19个代码示例,用于说明如何使用ast.IfExp()

项目:tidy    作者:cyrus-    | 项目源码 | 文件源码
def _compile_rule(rule_translation, rest):
        test, binding_translations, ctx_update, branch_translation = \
            rule_translation

        if len(binding_translations) == 0:
            body = branch_translation
        else:
            body_lambda = astx.make_Lambda(
                (ctx_update[id][0] 
                 for id in binding_translations.iterkeys()),
                branch_translation)
            body = astx.make_simple_Call(
                body_lambda, 
                binding_translations.values())

        if len(rest) == 0:
            orelse = astx.expr_Raise_Exception_string("Match failure.")
        else:
            orelse = fn._compile_rule(rest[0], rest[1:])

        return ast.IfExp(
            test=test,
            body=body,
            orelse=orelse)
项目:hostapd-mana    作者:adde88    | 项目源码 | 文件源码
def visit_Name(self, name):
        # Display the repr of the name if it's a local variable or
        # _should_repr_global_name() thinks it's acceptable.
        locs = ast_Call(self.builtin("locals"), [], [])
        inlocs = ast.Compare(ast.Str(name.id), [ast.In()], [locs])
        dorepr = self.helper("should_repr_global_name", name)
        test = ast.BoolOp(ast.Or(), [inlocs, dorepr])
        expr = ast.IfExp(test, self.display(name), ast.Str(name.id))
        return name, self.explanation_param(expr)
项目:sslstrip-hsts-openwrt    作者:adde88    | 项目源码 | 文件源码
def visit_Name(self, name):
        # Display the repr of the name if it's a local variable or
        # _should_repr_global_name() thinks it's acceptable.
        locs = ast_Call(self.builtin("locals"), [], [])
        inlocs = ast.Compare(ast.Str(name.id), [ast.In()], [locs])
        dorepr = self.helper("should_repr_global_name", name)
        test = ast.BoolOp(ast.Or(), [inlocs, dorepr])
        expr = ast.IfExp(test, self.display(name), ast.Str(name.id))
        return name, self.explanation_param(expr)
项目:web_ctp    作者:molebot    | 项目源码 | 文件源码
def test_ifexp(self):
        l = ast.Name("x", ast.Load())
        s = ast.Name("y", ast.Store())
        for args in (s, l, l), (l, s, l), (l, l, s):
            self.expr(ast.IfExp(*args), "must have Load context")
项目:godot-python    作者:touilleMan    | 项目源码 | 文件源码
def visit_Name(self, name):
        # Display the repr of the name if it's a local variable or
        # _should_repr_global_name() thinks it's acceptable.
        locs = ast_Call(self.builtin("locals"), [], [])
        inlocs = ast.Compare(ast.Str(name.id), [ast.In()], [locs])
        dorepr = self.helper("should_repr_global_name", name)
        test = ast.BoolOp(ast.Or(), [inlocs, dorepr])
        expr = ast.IfExp(test, self.display(name), ast.Str(name.id))
        return name, self.explanation_param(expr)
项目:godot-python    作者:touilleMan    | 项目源码 | 文件源码
def visit_Name(self, name):
        # Display the repr of the name if it's a local variable or
        # _should_repr_global_name() thinks it's acceptable.
        locs = ast_Call(self.builtin("locals"), [], [])
        inlocs = ast.Compare(ast.Str(name.id), [ast.In()], [locs])
        dorepr = self.helper("should_repr_global_name", name)
        test = ast.BoolOp(ast.Or(), [inlocs, dorepr])
        expr = ast.IfExp(test, self.display(name), ast.Str(name.id))
        return name, self.explanation_param(expr)
项目:sublimeTextConfig    作者:luoye-fe    | 项目源码 | 文件源码
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)
项目:ouroboros    作者:pybee    | 项目源码 | 文件源码
def test_ifexp(self):
        l = ast.Name("x", ast.Load())
        s = ast.Name("y", ast.Store())
        for args in (s, l, l), (l, s, l), (l, l, s):
            self.expr(ast.IfExp(*args), "must have Load context")
项目:hazzy    作者:KurtJacobson    | 项目源码 | 文件源码
def __init__(self, operators=None, functions=None, names=None):
        """
            Create the evaluator instance.  Set up valid operators (+,-, etc)
            functions (add, random, get_val, whatever) and names. """

        if not operators:
            operators = DEFAULT_OPERATORS
        if not functions:
            functions = DEFAULT_FUNCTIONS
        if not names:
            names = DEFAULT_NAMES

        self.operators = operators
        self.functions = functions
        self.names = names

        self.nodes = {
            ast.Num: self._eval_num,
            ast.Str: self._eval_str,
            ast.Name: self._eval_name,
            ast.UnaryOp: self._eval_unaryop,
            ast.BinOp: self._eval_binop,
            ast.BoolOp: self._eval_boolop,
            ast.Compare: self._eval_compare,
            ast.IfExp: self._eval_ifexp,
            ast.Call: self._eval_call,
            ast.keyword: self._eval_keyword,
            ast.Subscript: self._eval_subscript,
            ast.Attribute: self._eval_attribute,
            ast.Index: self._eval_index,
            ast.Slice: self._eval_slice,
        }

        # py3k stuff:
        if hasattr(ast, 'NameConstant'):
            self.nodes[ast.NameConstant] = self._eval_nameconstant
        elif isinstance(self.names, dict) and "None" not in self.names:
            self.names["None"] = None
项目:blackmamba    作者:zrzka    | 项目源码 | 文件源码
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)
项目:kbe_server    作者:xiaohaoppy    | 项目源码 | 文件源码
def test_ifexp(self):
        l = ast.Name("x", ast.Load())
        s = ast.Name("y", ast.Store())
        for args in (s, l, l), (l, s, l), (l, l, s):
            self.expr(ast.IfExp(*args), "must have Load context")
项目:GSM-scanner    作者:yosriayed    | 项目源码 | 文件源码
def visit_Name(self, name):
        # Display the repr of the name if it's a local variable or
        # _should_repr_global_name() thinks it's acceptable.
        locs = ast_Call(self.builtin("locals"), [], [])
        inlocs = ast.Compare(ast.Str(name.id), [ast.In()], [locs])
        dorepr = self.helper("should_repr_global_name", name)
        test = ast.BoolOp(ast.Or(), [inlocs, dorepr])
        expr = ast.IfExp(test, self.display(name), ast.Str(name.id))
        return name, self.explanation_param(expr)
项目:femtocode    作者:diana-hep    | 项目源码 | 文件源码
def pythonast(self, args, tonative=False):
        predicates = args[:-1][0::3]
        antipredicates = args[:-1][1::3]
        consequents = args[:-1][2::3]
        alternate = args[-1]
        return reduce(lambda x, y: ast.IfExp(y[0], y[1], x), reversed(list(zip(predicates, consequents))), alternate)
项目:femtocode    作者:diana-hep    | 项目源码 | 文件源码
def pythonast(self, args, tonative=False):
        arg, = args
        justlog = ast.Call(ast.Attribute(ast.Name("$math", ast.Load()), "log", ast.Load()), [arg], [], None, None)
        if self.base == math.e:
            if tonative:
                return justlog
            else:
                return ast.IfExp(ast.Compare(arg, [ast.Gt()], [ast.Num(0)]), justlog, ast.Num(-inf))
        else:
            scaled = ast.BinOp(justlog, ast.Div(), ast.Num(math.log(self.base)))
            if tonative:
                return scaled
            else:
                return ast.IfExp(ast.Compare(arg, [ast.Gt()], [ast.Num(0)]), scaled, ast.Num(-inf))
项目:femtocode    作者:diana-hep    | 项目源码 | 文件源码
def pythonast(self, args, tonative=False):
        arg, = args
        return ast.IfExp(ast.Compare(arg, [ast.Eq()], [ast.Num(1)]), ast.Num(inf), ast.IfExp(ast.Compare(arg, [ast.Eq()], [ast.Num(-1)]), ast.Num(-inf), ast.Call(ast.Attribute(ast.Name("$math", ast.Load()), "atanh", ast.Load()), args, [], None, None)))
项目:wuye.vim    作者:zhaoyingnan911    | 项目源码 | 文件源码
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)
项目:PyAcid    作者:Acid-ZdS    | 项目源码 | 文件源码
def translate_if(compiler, if_):
    return python_ast.IfExp(
        test=compiler.translate(if_.condition),
        body=compiler.translate(if_.consequence),
        orelse=compiler.translate(if_.alternative)
    )
项目:tidy    作者:cyrus-    | 项目源码 | 文件源码
def translate_IfExp(cls, ctx, e):
        test_translation = ctx.translate(e.test)
        body_translation = ctx.translate(e.body)
        orelse_translation = ctx.translate(e.orelse)
        return ast.IfExp(
            test=test_translation,
            body=body_translation,
            orelse=orelse_translation)
项目:Typpete    作者:caterinaurban    | 项目源码 | 文件源码
def infer(node, context, solver, from_call=False):
    """Infer the type of a given AST node"""
    if isinstance(node, ast.Num):
        return infer_numeric(node, solver)
    elif isinstance(node, ast.Str):
        return solver.z3_types.string
    elif (sys.version_info[0] >= 3 and sys.version_info[1] >= 6 and
            (isinstance(node, ast.FormattedValue) or isinstance(node, ast.JoinedStr))):
        # Formatted strings were introduced in Python 3.6
        return solver.z3_types.string
    elif isinstance(node, ast.Bytes):
        return solver.z3_types.bytes
    elif isinstance(node, ast.List):
        return infer_list(node, context, solver)
    elif isinstance(node, ast.Dict):
        return infer_dict(node, context, solver)
    elif isinstance(node, ast.Tuple):
        return infer_tuple(node, context, solver)
    elif isinstance(node, ast.NameConstant):
        return infer_name_constant(node, solver)
    elif isinstance(node, ast.Set):
        return infer_set(node, context, solver)
    elif isinstance(node, ast.BinOp):
        return infer_binary_operation(node, context, solver)
    elif isinstance(node, ast.BoolOp):
        return infer_boolean_operation(node, context, solver)
    elif isinstance(node, ast.UnaryOp):
        return infer_unary_operation(node, context, solver)
    elif isinstance(node, ast.IfExp):
        return infer_if_expression(node, context, solver)
    elif isinstance(node, ast.Subscript):
        return infer_subscript(node, context, solver)
    elif sys.version_info[0] >= 3 and sys.version_info[1] >= 5 and isinstance(node, ast.Await):
        # Await and Async were introduced in Python 3.5
        return infer(node.value, context, solver)
    elif isinstance(node, ast.Yield):
        return infer(node.value, context, solver)
    elif isinstance(node, ast.Compare):
        return infer_compare(node, context, solver)
    elif isinstance(node, ast.Name):
        return infer_name(node, context)
    elif isinstance(node, ast.ListComp):
        return infer_sequence_comprehension(node, solver.z3_types.list, context, solver)
    elif isinstance(node, ast.SetComp):
        return infer_sequence_comprehension(node, solver.z3_types.set, context, solver)
    elif isinstance(node, ast.DictComp):
        return infer_dict_comprehension(node, context, solver)
    elif isinstance(node, ast.Call):
        return infer_func_call(node, context, solver)
    elif isinstance(node, ast.Attribute):
        return infer_attribute(node, context, from_call, solver)
    elif isinstance(node, ast.Lambda):
        return _infer_lambda(node, context, solver)
    raise NotImplementedError("Inference for expression {} is not implemented yet.".format(type(node).__name__))