当前位置: 首页>>代码示例>>Python>>正文


Python ast.USub方法代码示例

本文整理汇总了Python中ast.USub方法的典型用法代码示例。如果您正苦于以下问题:Python ast.USub方法的具体用法?Python ast.USub怎么用?Python ast.USub使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在ast的用法示例。


在下文中一共展示了ast.USub方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: visit_UnaryOp

# 需要导入模块: import ast [as 别名]
# 或者: from ast import USub [as 别名]
def visit_UnaryOp(self, node):
        # in python3 a negative Num is represented as an USub of a positive Num
        # Fix: restore python2 behavior by a shortcut: invert value of Num, inherit
        # vhdl type from UnaryOp node, and visit the modified operand
        if isinstance(node.op, ast.USub) and isinstance(node.operand, ast.Num):
            node.operand.n = -node.operand.n
            node.operand.vhd = node.vhd
            self.visit(node.operand)
            return
        pre, suf = self.inferCast(node.vhd, node.vhdOri)
        self.write(pre)
        self.write("(")
        self.write(opmap[type(node.op)])
        self.visit(node.operand)
        self.write(")")
        self.write(suf) 
开发者ID:myhdl,项目名称:myhdl,代码行数:18,代码来源:_toVHDL.py

示例2: visit_UnaryOp

# 需要导入模块: import ast [as 别名]
# 或者: from ast import USub [as 别名]
def visit_UnaryOp(self, node):
        'Change -x to (-1)*x'
        self.generic_visit(node)
        if isinstance(node.op, ast.USub):
            ope = node.operand
            cond_mult = (isinstance(ope, ast.BinOp) and
                         isinstance(ope.op, ast.Mult))
            if cond_mult:
                if isinstance(ope.left, ast.Num):
                    node = ast.BinOp(ast.Num(-ope.left.n), ast.Mult(),
                                     ope.right)
                elif isinstance(ope.right, ast.Num):
                    node = ast.BinOp(ope.left, ast.Mult(),
                                     ast.Num(-ope.right.n))
                else:
                    node = ast.BinOp(ast.Num(-1), ast.Mult(), ope)
            else:
                node = ast.BinOp(ast.Num(-1), ast.Mult(), ope)
        return node 
开发者ID:quarkslab,项目名称:sspam,代码行数:21,代码来源:pre_processing.py

示例3: unary_minus_expression_ast_to_ir2

# 需要导入模块: import ast [as 别名]
# 或者: from ast import USub [as 别名]
def unary_minus_expression_ast_to_ir2(ast_node: ast.UnaryOp,
                                      compilation_context: CompilationContext,
                                      in_match_pattern: bool,
                                      check_var_reference: Callable[[ast.Name], None],
                                      match_lambda_argument_names: Set[str],
                                      current_stmt_line: int):
    assert isinstance(ast_node.op, ast.USub)

    if in_match_pattern:
        raise CompilationError(compilation_context, ast_node,
                               'The "-" operator is not allowed in match patterns')

    expr = expression_ast_to_ir2(ast_node.operand,
                                 compilation_context,
                                 in_match_pattern,
                                 check_var_reference,
                                 match_lambda_argument_names,
                                 current_stmt_line)

    if expr.expr_type != ir2.IntType():
        raise CompilationError(compilation_context, ast_node.operand,
                               'The "-" operator is only supported for ints, but this value has type %s.' % str(expr.expr_type))

    return ir2.IntUnaryMinusExpr(expr=expr) 
开发者ID:google,项目名称:tmppy,代码行数:26,代码来源:_ast_to_ir2.py

示例4: visit_Hook

# 需要导入模块: import ast [as 别名]
# 或者: from ast import USub [as 别名]
def visit_Hook(self, node: parsing.Hook) -> ast.expr:
        """Generates python code calling a hook.

        self.evalHook('hookname', self.ruleNodes[-1])
        """
        return ast.Call(
            ast.Attribute(
                ast.Name('self', ast.Load()), 'evalHook', ast.Load()),
            [
                ast.Str(node.name),
                ast.Subscript(
                    ast.Attribute(
                        ast.Name('self', ast.Load()), 'ruleNodes', ast.Load()),
                    ast.Index(ast.UnaryOp(ast.USub(), ast.Num(1))),
                    ast.Load())],
            [],
            None,
            None) 
开发者ID:LionelAuroux,项目名称:pyrser,代码行数:20,代码来源:topython.py

示例5: _get_non_negative_nodes

# 需要导入模块: import ast [as 别名]
# 或者: from ast import USub [as 别名]
def _get_non_negative_nodes(
        self,
        left: Optional[ast.AST],
        right: Optional[ast.AST] = None,
    ):
        non_negative_numbers = []
        for node in filter(None, (left, right)):
            real_node = unwrap_unary_node(node)
            correct_node = (
                isinstance(real_node, ast.Num) and
                real_node.n in self._meaningless_operations and
                not (real_node.n == 1 and walk.is_contained(node, ast.USub))
            )
            if correct_node:
                non_negative_numbers.append(real_node)
        return non_negative_numbers 
开发者ID:wemake-services,项目名称:wemake-python-styleguide,代码行数:18,代码来源:operators.py

示例6: visit_UnaryOp

# 需要导入模块: import ast [as 别名]
# 或者: from ast import USub [as 别名]
def visit_UnaryOp(self, node):
        self.visit(node.operand)
        node.signed = node.operand.signed
        if isinstance(node.op, ast.USub):
            node.obj = int(-1)
            if isinstance(node.operand, ast.Num):
                node.signed = True 
开发者ID:myhdl,项目名称:myhdl,代码行数:9,代码来源:_toVerilog.py

示例7: visit_UnaryOp

# 需要导入模块: import ast [as 别名]
# 或者: from ast import USub [as 别名]
def visit_UnaryOp(self, node):
        self.visit(node.operand)
        op = node.op
        node.obj = node.operand.obj
        if isinstance(op, ast.Not):
            node.obj = bool()
        elif isinstance(op, ast.UAdd):
            node.obj = int(-1)
        elif isinstance(op, ast.USub):
            node.obj = int(-1) 
开发者ID:myhdl,项目名称:myhdl,代码行数:12,代码来源:_analyze.py

示例8: factor_rewrite

# 需要导入模块: import ast [as 别名]
# 或者: from ast import USub [as 别名]
def factor_rewrite(mark: Tokenizer, factor, power):
    return power if power else ast.UnaryOp(
        **(loc @ mark),
        op={
            '~': ast.Invert,
            '+': ast.UAdd,
            '-': ast.USub
        }[mark.value](),
        operand=factor,
    ) 
开发者ID:Xython,项目名称:YAPyPy,代码行数:12,代码来源:helper.py

示例9: visitUnaryOp

# 需要导入模块: import ast [as 别名]
# 或者: from ast import USub [as 别名]
def visitUnaryOp(self, node):
        import ast
        if isinstance(node.op, ast.UAdd):
            return +self.visit(node.operand)
        elif isinstance(node.op, ast.USub):
            return -self.visit(node.operand)
        else:
            raise SyntaxError("Unknown unary op: %r" % node.op) 
开发者ID:Frank-qlu,项目名称:recruit,代码行数:10,代码来源:utils.py

示例10: visit_UnaryOp

# 需要导入模块: import ast [as 别名]
# 或者: from ast import USub [as 别名]
def visit_UnaryOp(self, node, **kwargs):
        if isinstance(node.op, (ast.Not, ast.Invert)):
            return UnaryOp('~', self.visit(node.operand))
        elif isinstance(node.op, ast.USub):
            return self.const_type(-self.visit(node.operand).value, self.env)
        elif isinstance(node.op, ast.UAdd):
            raise NotImplementedError('Unary addition not supported') 
开发者ID:Frank-qlu,项目名称:recruit,代码行数:9,代码来源:pytables.py

示例11: visit_UnaryOp

# 需要导入模块: import ast [as 别名]
# 或者: from ast import USub [as 别名]
def visit_UnaryOp(self, node):
        'Change USub and Invert'
        operand = self.visit(node.operand)
        if isinstance(node.op, ast.UAdd):
            return operand
        if isinstance(node.op, ast.USub):
            return ast.BinOp(ast.Num(-1), ast.Mult(), operand)
        if isinstance(node.op, ast.Invert):
            return ast.BinOp(ast.Num(-1), ast.BitXor(), operand)
        assert False, 'unhandled node type: ' + ast.dump(node) 
开发者ID:quarkslab,项目名称:sspam,代码行数:12,代码来源:cse.py

示例12: test_docexample

# 需要导入模块: import ast [as 别名]
# 或者: from ast import USub [as 别名]
def test_docexample(self):
        # used to fail on ironpython for various reason
        node = ast.UnaryOp(ast.USub(), ast.Num(5, lineno=0, col_offset=0),
                            lineno=0, col_offset=0)

        # the same with zero argument constructors
        node = ast.UnaryOp()
        node.op = ast.USub()
        node.operand = ast.Num()
        node.operand.n = 5
        node.operand.lineno = 0
        node.operand.col_offset = 0
        node.lineno = 0
        node.col_offset = 0 
开发者ID:IronLanguages,项目名称:ironpython2,代码行数:16,代码来源:test_ast.py

示例13: onReturnPressed

# 需要导入模块: import ast [as 别名]
# 或者: from ast import USub [as 别名]
def onReturnPressed(self):

        expr = str(self.ui.lineEdit.text())


        import ast
        import operator as op
        # supported operators
        operators = {ast.Add: op.add, ast.Sub: op.sub, ast.Mult: op.mul,
                     ast.Div: op.floordiv, ast.Pow: op.pow, ast.USub: op.neg}

        def eval_expr(expr):
            return eval_(ast.parse(expr, mode='eval').body)

        def eval_(node):
            if isinstance(node, ast.Num):
                return node.n
            elif isinstance(node, ast.BinOp):
                return operators[type(node.op)](eval_(node.left), eval_(node.right))
            elif isinstance(node, ast.UnaryOp):
                return operators[type(node.op)](eval_(node.operand))
            elif isinstance(node, object):
                # handle constants
                k = str(node.id).upper()
                if k in self.konstants:
                    return self.konstants[k](k)
                else:
                    raise TypeError(node)
            else:
                raise TypeError(node)

        try:
            result = eval_expr(expr)
        except Exception as e:
            self.ui.label.setText('error.')
            return
        

        self.ui.label.setText('{0} ({1})'.format(hex(result), result))

        self.onResult(result) 
开发者ID:mtivadar,项目名称:qiew,代码行数:43,代码来源:FileFormat.py

示例14: visit_UnaryOp

# 需要导入模块: import ast [as 别名]
# 或者: from ast import USub [as 别名]
def visit_UnaryOp(self, node):
        assert isinstance(node.op, ast.USub), \
            'only unary minus is supported, not {}'.format(node.op.__class__.__name__)
        self.visit(ast.Num(n=0))
        self.visit(node.operand)
        self.visit(ast.Sub()) 
开发者ID:benhoyt,项目名称:pyast64,代码行数:8,代码来源:pyast64.py

示例15: visit_For

# 需要导入模块: import ast [as 别名]
# 或者: from ast import USub [as 别名]
def visit_For(self, node):
        # Turn for+range loop into a while loop:
        #   i = start
        #   while i < stop:
        #       body
        #       i = i + step
        assert isinstance(node.iter, ast.Call) and \
            node.iter.func.id == 'range', \
            'for can only be used with range()'
        range_args = node.iter.args
        if len(range_args) == 1:
            start = ast.Num(n=0)
            stop = range_args[0]
            step = ast.Num(n=1)
        elif len(range_args) == 2:
            start, stop = range_args
            step = ast.Num(n=1)
        else:
            start, stop, step = range_args
            if (isinstance(step, ast.UnaryOp) and
                    isinstance(step.op, ast.USub) and
                    isinstance(step.operand, ast.Num)):
                # Handle negative step
                step = ast.Num(n=-step.operand.n)
            assert isinstance(step, ast.Num) and step.n != 0, \
                'range() step must be a nonzero integer constant'
        self.visit(ast.Assign(targets=[node.target], value=start))
        test = ast.Compare(
            left=node.target,
            ops=[ast.Lt() if step.n > 0 else ast.Gt()],
            comparators=[stop],
        )
        incr = ast.Assign(
            targets=[node.target],
            value=ast.BinOp(left=node.target, op=ast.Add(), right=step),
        )
        self.visit(ast.While(test=test, body=node.body + [incr])) 
开发者ID:benhoyt,项目名称:pyast64,代码行数:39,代码来源:pyast64.py


注:本文中的ast.USub方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。