python类BinOp()的实例源码

lambdazen.py 文件源码 项目:lambdazen 作者: brthor 项目源码 文件源码 阅读 31 收藏 0 点赞 0 评论 0
def _transform_multiline_assignment_statements(statements):
    assignment_statements = [statement for statement in statements
                             if type(statement) is _ast.BinOp
                             and type(statement.op) is _ast.LShift
                             and type(statement.left) is _ast.Name]

    other_statements = [statement for statement in statements if statement not in assignment_statements]

    assignments = [ast.Assign(targets=[statement.left], value=statement.right, lineno=statement.lineno, col_offset=statement.col_offset)
            for statement in assignment_statements]

    for assignment in assignments:
        assignment.targets[0].ctx = ast.Store()

    return other_statements + assignments
simple_instructions.py 文件源码 项目:femtocode 作者: diana-hep 项目源码 文件源码 阅读 72 收藏 0 点赞 0 评论 0
def BINARY_(OP):

    def BINARY_OP(self, instr):
        right = self.ast_stack.pop()
        left = self.ast_stack.pop()

        add = _ast.BinOp(left=left, right=right, op=OP(), lineno=instr.lineno, col_offset=0)

        self.ast_stack.append(add)
    return BINARY_OP
func_def.py 文件源码 项目:py_gpumap 作者: ipachev 项目源码 文件源码 阅读 22 收藏 0 点赞 0 评论 0
def visit_Assign(self, node):
        if len(node.targets) > 1:
            raise SyntaxError("GPUMAP: multiple assignment not supported")

        target_types = map(lambda target: type(target), node.targets)
        if tuple in target_types or list in target_types:
            raise SyntaxError("GPUMAP: No unpacking allowed")


        target = node.targets[0]
        # assignment into object field
        output = ""

        value = self.visit(node.value)

        # assignment into variable
        if isinstance(target, _ast.Name):
            # assignment into new variable
            # not sure about the type just yet..

            # see if it's a primitive
            if target.id not in self.local_vars:
                # binops and boolops return primitives
                if isinstance(node.value, _ast.Num) or isinstance(node.value, _ast.Compare) or isinstance(node.value, _ast.BinOp) \
                        or isinstance(node.value, _ast.BoolOp) or isinstance(node.value, _ast.NameConstant):
                    output += "auto "

                # check if referenced list contains primitives
                elif isinstance(node.value, _ast.Subscript):
                    list_name = value[:value.find("[")]
                    try:
                        idx = self.func_repr.args.index(list_name)
                        t = self.func_repr.arg_types[idx]
                        item_type = t[t.find("<") + 1: t.find(">")]
                        if item_type in map(lambda t: t.__name__, primitive_map.keys()):
                            output += "auto "
                        else:
                            output += "auto&& "
                    except:
                        raise RuntimeError("THIS SHOULD NEVER HAPPEN")
                else:
                    # check if it's an existing variable
                    try:
                        idx = self.func_repr.args.index(value)
                        t = self.func_repr.arg_types[idx]
                        if t in primitive_map:
                            output += "auto "
                        else:
                            output += "auto&& "
                    except ValueError:
                        output += "auto&& "
                self.local_vars[target.id] = None
        output += self.visit(target)
        output += " = " + value
        return output
astpeephole.py 文件源码 项目:wuye.vim 作者: zhaoyingnan911 项目源码 文件源码 阅读 22 收藏 0 点赞 0 评论 0
def optimize_binop(self, node):
        """Optimize BinOps with string Const nodes on the lhs.

        This fixes an infinite recursion crash, where multiple
        strings are joined using the addition operator. With a
        sufficient number of such strings, astroid will fail
        with a maximum recursion limit exceeded. The
        function will return a Const node with all the strings
        already joined.
        Return ``None`` if no AST node can be obtained
        through optimization.
        """
        ast_nodes = []
        current = node
        while isinstance(current, _ast.BinOp):
            # lhs must be a BinOp with the addition operand.
            if not isinstance(current.left, _ast.BinOp):
                return
            if (not isinstance(current.left.op, _ast.Add)
                    or not isinstance(current.op, _ast.Add)):
                return

            # rhs must a str / bytes.
            if not isinstance(current.right, _TYPES):
                return

            ast_nodes.append(current.right.s)
            current = current.left

            if (isinstance(current, _ast.BinOp)
                    and isinstance(current.left, _TYPES)
                    and isinstance(current.right, _TYPES)):
                # Stop early if we are at the last BinOp in
                # the operation
                ast_nodes.append(current.right.s)
                ast_nodes.append(current.left.s)
                break

        if not ast_nodes:
            return

        # If we have inconsistent types, bail out.
        known = type(ast_nodes[0])
        if any(type(element) is not known
               for element in ast_nodes[1:]):
            return

        value = known().join(reversed(ast_nodes))
        newnode = nodes.Const(value)
        return newnode


问题


面经


文章

微信
公众号

扫码关注公众号