Python ast.UAdd() Examples

The following are 30 code examples of ast.UAdd(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module ast , or try the search function .
Example #1
Source File: pytables.py    From Splunking-Crime with GNU Affero General Public License v3.0 5 votes vote down vote up
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') 
Example #2
Source File: _recompute.py    From icontract with MIT License 5 votes vote down vote up
def visit_UnaryOp(self, node: ast.UnaryOp) -> Any:
        """Visit the node operand and apply the operation on the result."""
        if isinstance(node.op, ast.UAdd):
            result = +self.visit(node=node.operand)
        elif isinstance(node.op, ast.USub):
            result = -self.visit(node=node.operand)
        elif isinstance(node.op, ast.Not):
            result = not self.visit(node=node.operand)
        elif isinstance(node.op, ast.Invert):
            result = ~self.visit(node=node.operand)
        else:
            raise NotImplementedError("Unhandled op of {}: {}".format(node, node.op))

        self.recomputed_values[node] = result
        return result 
Example #3
Source File: utils.py    From mxnet-lambda with Apache License 2.0 5 votes vote down vote up
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) 
Example #4
Source File: annotation.py    From vyper with Apache License 2.0 5 votes vote down vote up
def visit_UnaryOp(self, node):
        """
        Adjust operand value and discard unary operations, where possible.

        This is done so that negative decimal literals are accurately represented.
        """
        self.generic_visit(node)

        # TODO once grammar is updated, remove this
        # UAdd has no effect on the value of it's operand, so it is discarded
        if isinstance(node.op, python_ast.UAdd):
            return node.operand

        is_sub = isinstance(node.op, python_ast.USub)
        is_num = (
            hasattr(node.operand, "n")
            and not isinstance(node.operand.n, bool)
            and isinstance(node.operand.n, (int, Decimal))
        )
        if is_sub and is_num:
            node.operand.n = 0 - node.operand.n
            node.operand.col_offset = node.col_offset
            node.operand.node_source_code = node.node_source_code
            return node.operand
        else:
            return node 
Example #5
Source File: parser.py    From vecpy with MIT License 5 votes vote down vote up
def unaryop(self, block, node):
    operand = self.expression(block, node.operand)
    if isinstance(node.op, ast.UAdd):
      #No-Op
      var = operand
    elif isinstance(node.op, ast.USub):
      if operand.value is not None:
        #It's a literal, return the negation
        var = self.add_literal(-operand.value)
      else:
        #It's an expression, subtract from zero (faster than multiplying by -1)
        var = self.add_variable(None)
        zero = self.add_literal(0, 'ZERO')
        operation = BinaryOperation(zero, Operator.subtract, operand)
        assignment = Assignment(var, operation)
        block.add(assignment)
    elif isinstance(node.op, ast.Invert):
      #Make sure it's numeric
      if operand.is_mask:
        raise Exception('Unary invert requires a numeric operand')
      #Bit-flip
      var = self.add_variable(None)
      operation = UnaryOperation(Operator.bit_not, operand)
      assignment = Assignment(var, operation)
      block.add(assignment)
    elif isinstance(node.op, ast.Not):
      #Make sure it's boolean
      if not operand.is_mask:
        raise Exception('Unary not requires a boolean operand')
      #Invert the mask
      var = self.add_variable(None, is_mask=True)
      operation = UnaryOperation(Operator.bool_not, operand)
      assignment = Assignment(var, operation)
      block.add(assignment)
    else:
      raise Exception('Unexpected UnaryOp (%s)'%(node.op.__class__))
    return var

  #Parses a comparison operator (AST CmpOp) 
Example #6
Source File: bird.py    From executing with MIT License 5 votes vote down vote up
def is_interesting_expression(node):
    # type: (ast.AST) -> bool
    """
    If this expression has a value that may not be exactly what it looks like,
    return True. Put differently, return False if this is just a literal.
    """
    return (isinstance(node, ast.expr) and
            not (isinstance(node, (ast.Num, ast.Str, getattr(ast, 'NameConstant', ()))) or
                 isinstance(getattr(node, 'ctx', None),
                            (ast.Store, ast.Del)) or
                 (isinstance(node, ast.UnaryOp) and
                  isinstance(node.op, (ast.UAdd, ast.USub)) and
                  isinstance(node.operand, ast.Num)) or
                 (isinstance(node, (ast.List, ast.Tuple, ast.Dict)) and
                  not any(is_interesting_expression(n) for n in ast.iter_child_nodes(node))))) 
Example #7
Source File: bugbear.py    From flake8-bugbear with MIT License 5 votes vote down vote up
def visit_UAdd(self, node):
        trailing_nodes = list(map(type, self.node_window[-4:]))
        if trailing_nodes == [ast.UnaryOp, ast.UAdd, ast.UnaryOp, ast.UAdd]:
            originator = self.node_window[-4]
            self.errors.append(B002(originator.lineno, originator.col_offset))
        self.generic_visit(node) 
Example #8
Source File: utils.py    From ImageFusion with MIT License 5 votes vote down vote up
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) 
Example #9
Source File: hgawk_grammar.py    From histogrammar-python with Apache License 2.0 5 votes vote down vote up
def p_factor_1(p):
    '''factor : PLUS factor'''
    #              1      2
    op = ast.UAdd(rule=inspect.currentframe().f_code.co_name, **p[1][1])
    p[0] = ast.UnaryOp(op, p[2], rule=inspect.currentframe().f_code.co_name)
    inherit_lineno(p[0], op) 
Example #10
Source File: utils.py    From Splunking-Crime with GNU Affero General Public License v3.0 5 votes vote down vote up
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) 
Example #11
Source File: utils.py    From pySINDy with MIT License 5 votes vote down vote up
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) 
Example #12
Source File: utils.py    From elasticintel with GNU General Public License v3.0 5 votes vote down vote up
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) 
Example #13
Source File: pytables.py    From elasticintel with GNU General Public License v3.0 5 votes vote down vote up
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') 
Example #14
Source File: utils.py    From coffeegrindsize with MIT License 5 votes vote down vote up
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) 
Example #15
Source File: utils.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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) 
Example #16
Source File: safe_eval.py    From wemake-python-styleguide with MIT License 5 votes vote down vote up
def _convert_signed_num(node: Optional[ast.AST]):
    unary_operators = (ast.UAdd, ast.USub)
    if isinstance(node, ast.UnaryOp) and isinstance(node.op, unary_operators):
        operand = _convert_num(node.operand)
        return +operand if isinstance(node.op, ast.UAdd) else -operand
    return _convert_num(node) 
Example #17
Source File: utils.py    From Serverless-Deep-Learning-with-TensorFlow-and-AWS-Lambda with MIT License 5 votes vote down vote up
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) 
Example #18
Source File: utils.py    From twitter-stock-recommendation with MIT License 5 votes vote down vote up
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) 
Example #19
Source File: utils.py    From keras-lambda with MIT License 5 votes vote down vote up
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) 
Example #20
Source File: pytables.py    From vnpy_crypto with MIT License 5 votes vote down vote up
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') 
Example #21
Source File: helper.py    From YAPyPy with MIT License 5 votes vote down vote up
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,
    ) 
Example #22
Source File: utils.py    From recruit with Apache License 2.0 5 votes vote down vote up
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) 
Example #23
Source File: pytables.py    From recruit with Apache License 2.0 5 votes vote down vote up
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') 
Example #24
Source File: cse.py    From sspam with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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) 
Example #25
Source File: utils.py    From lambda-packs with MIT License 5 votes vote down vote up
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) 
Example #26
Source File: utils.py    From lambda-packs with MIT License 5 votes vote down vote up
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) 
Example #27
Source File: utils.py    From auto-alt-text-lambda-api with MIT License 5 votes vote down vote up
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) 
Example #28
Source File: utils.py    From vnpy_crypto with MIT License 5 votes vote down vote up
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) 
Example #29
Source File: _analyze.py    From myhdl with GNU Lesser General Public License v2.1 5 votes vote down vote up
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) 
Example #30
Source File: utils.py    From Computable with MIT License 5 votes vote down vote up
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)