Python ast.operator() Examples

The following are 27 code examples of ast.operator(). 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: expr.py    From recruit with Apache License 2.0 6 votes vote down vote up
def _op_maker(op_class, op_symbol):
    """Return a function to create an op class with its symbol already passed.

    Returns
    -------
    f : callable
    """

    def f(self, node, *args, **kwargs):
        """Return a partial function with an Op subclass with an operator
        already passed.

        Returns
        -------
        f : callable
        """
        return partial(op_class, op_symbol, *args, **kwargs)
    return f 
Example #2
Source File: operators.py    From wemake-python-styleguide with MIT License 6 votes vote down vote up
def _check_string_concat(
        self,
        left: ast.AST,
        op: ast.operator,
        right: Optional[ast.AST] = None,
    ) -> None:
        if not isinstance(op, ast.Add):
            return

        left_line = getattr(left, 'lineno', 0)
        if left_line != getattr(right, 'lineno', left_line):
            # By default we treat nodes that do not have lineno
            # as nodes on the same line.
            return

        for node in (left, right):
            if isinstance(node, self._string_nodes):
                self.add_violation(
                    consistency.ExplicitStringConcatViolation(node),
                )
                return 
Example #3
Source File: operators.py    From wemake-python-styleguide with MIT License 6 votes vote down vote up
def _check_useless_math_operator(
        self,
        op: ast.operator,
        left: Optional[ast.AST],
        right: Optional[ast.AST] = None,
    ) -> None:
        if isinstance(left, ast.Num) and left.n in self._left_special_cases:
            if right and isinstance(op, self._left_special_cases[left.n]):
                left = None

        non_negative_numbers = self._get_non_negative_nodes(left, right)

        for number in non_negative_numbers:
            forbidden = self._meaningless_operations.get(number.n, None)
            if forbidden and isinstance(op, forbidden):
                self.add_violation(
                    consistency.MeaninglessNumberOperationViolation(number),
                ) 
Example #4
Source File: expr.py    From elasticintel with GNU General Public License v3.0 6 votes vote down vote up
def _op_maker(op_class, op_symbol):
    """Return a function to create an op class with its symbol already passed.

    Returns
    -------
    f : callable
    """

    def f(self, node, *args, **kwargs):
        """Return a partial function with an Op subclass with an operator
        already passed.

        Returns
        -------
        f : callable
        """
        return partial(op_class, op_symbol, *args, **kwargs)
    return f 
Example #5
Source File: expr.py    From elasticintel with GNU General Public License v3.0 6 votes vote down vote up
def _rewrite_assign(tok):
    """Rewrite the assignment operator for PyTables expressions that use ``=``
    as a substitute for ``==``.

    Parameters
    ----------
    tok : tuple of int, str
        ints correspond to the all caps constants in the tokenize module

    Returns
    -------
    t : tuple of int, str
        Either the input or token or the replacement values
    """
    toknum, tokval = tok
    return toknum, '==' if tokval == '=' else tokval 
Example #6
Source File: expr.py    From Splunking-Crime with GNU Affero General Public License v3.0 6 votes vote down vote up
def _op_maker(op_class, op_symbol):
    """Return a function to create an op class with its symbol already passed.

    Returns
    -------
    f : callable
    """

    def f(self, node, *args, **kwargs):
        """Return a partial function with an Op subclass with an operator
        already passed.

        Returns
        -------
        f : callable
        """
        return partial(op_class, op_symbol, *args, **kwargs)
    return f 
Example #7
Source File: expr.py    From Splunking-Crime with GNU Affero General Public License v3.0 6 votes vote down vote up
def _rewrite_assign(tok):
    """Rewrite the assignment operator for PyTables expressions that use ``=``
    as a substitute for ``==``.

    Parameters
    ----------
    tok : tuple of int, str
        ints correspond to the all caps constants in the tokenize module

    Returns
    -------
    t : tuple of int, str
        Either the input or token or the replacement values
    """
    toknum, tokval = tok
    return toknum, '==' if tokval == '=' else tokval 
Example #8
Source File: expr.py    From predictive-maintenance-using-machine-learning with Apache License 2.0 6 votes vote down vote up
def _op_maker(op_class, op_symbol):
    """Return a function to create an op class with its symbol already passed.

    Returns
    -------
    f : callable
    """

    def f(self, node, *args, **kwargs):
        """Return a partial function with an Op subclass with an operator
        already passed.

        Returns
        -------
        f : callable
        """
        return partial(op_class, op_symbol, *args, **kwargs)
    return f 
Example #9
Source File: expr.py    From predictive-maintenance-using-machine-learning with Apache License 2.0 6 votes vote down vote up
def _rewrite_assign(tok):
    """Rewrite the assignment operator for PyTables expressions that use ``=``
    as a substitute for ``==``.

    Parameters
    ----------
    tok : tuple of int, str
        ints correspond to the all caps constants in the tokenize module

    Returns
    -------
    t : tuple of int, str
        Either the input or token or the replacement values
    """
    toknum, tokval = tok
    return toknum, '==' if tokval == '=' else tokval 
Example #10
Source File: expr.py    From Computable with MIT License 6 votes vote down vote up
def _op_maker(op_class, op_symbol):
    """Return a function to create an op class with its symbol already passed.

    Returns
    -------
    f : callable
    """

    def f(self, node, *args, **kwargs):
        """Return a partial function with an Op subclass with an operator
        already passed.

        Returns
        -------
        f : callable
        """
        return partial(op_class, op_symbol, *args, **kwargs)
    return f 
Example #11
Source File: expr.py    From recruit with Apache License 2.0 6 votes vote down vote up
def _rewrite_assign(tok):
    """Rewrite the assignment operator for PyTables expressions that use ``=``
    as a substitute for ``==``.

    Parameters
    ----------
    tok : tuple of int, str
        ints correspond to the all caps constants in the tokenize module

    Returns
    -------
    t : tuple of int, str
        Either the input or token or the replacement values
    """
    toknum, tokval = tok
    return toknum, '==' if tokval == '=' else tokval 
Example #12
Source File: expr.py    From vnpy_crypto with MIT License 6 votes vote down vote up
def _op_maker(op_class, op_symbol):
    """Return a function to create an op class with its symbol already passed.

    Returns
    -------
    f : callable
    """

    def f(self, node, *args, **kwargs):
        """Return a partial function with an Op subclass with an operator
        already passed.

        Returns
        -------
        f : callable
        """
        return partial(op_class, op_symbol, *args, **kwargs)
    return f 
Example #13
Source File: expr.py    From vnpy_crypto with MIT License 6 votes vote down vote up
def _rewrite_assign(tok):
    """Rewrite the assignment operator for PyTables expressions that use ``=``
    as a substitute for ``==``.

    Parameters
    ----------
    tok : tuple of int, str
        ints correspond to the all caps constants in the tokenize module

    Returns
    -------
    t : tuple of int, str
        Either the input or token or the replacement values
    """
    toknum, tokval = tok
    return toknum, '==' if tokval == '=' else tokval 
Example #14
Source File: operators.py    From wemake-python-styleguide with MIT License 5 votes vote down vote up
def visit_NamedExpr(
        self,
        node: NamedExpr,
    ) -> None:  # pragma: py-lt-38
        """
        Disallows walrus ``:=`` operator.

        Raises:
            WalrusViolation

        """
        self.add_violation(consistency.WalrusViolation(node))
        self.generic_visit(node) 
Example #15
Source File: expr.py    From Computable with MIT License 5 votes vote down vote up
def _rewrite_membership_op(self, node, left, right):
        # the kind of the operator (is actually an instance)
        op_instance = node.op
        op_type = type(op_instance)

        # must be two terms and the comparison operator must be ==/!=/in/not in
        if is_term(left) and is_term(right) and op_type in self.rewrite_map:

            left_list, right_list = map(_is_list, (left, right))
            left_str, right_str = map(_is_str, (left, right))

            # if there are any strings or lists in the expression
            if left_list or right_list or left_str or right_str:
                op_instance = self.rewrite_map[op_type]()

            # pop the string variable out of locals and replace it with a list
            # of one string, kind of a hack
            if right_str:
                self.env.remove_tmp(right.name)
                name = self.env.add_tmp([right.value])
                right = self.term_type(name, self.env)

            if left_str:
                self.env.remove_tmp(left.name)
                name = self.env.add_tmp([left.value])
                left = self.term_type(name, self.env)

        op = self.visit(op_instance)
        return op, op_instance, left, right 
Example #16
Source File: operators.py    From wemake-python-styleguide with MIT License 5 votes vote down vote up
def _check_negation(self, op: ast.operator, right: ast.AST) -> None:
        is_double_minus = (
            isinstance(op, (ast.Add, ast.Sub)) and
            isinstance(right, ast.UnaryOp) and
            isinstance(right.op, ast.USub)
        )
        if is_double_minus:
            self.add_violation(
                consistency.OperationSignNegationViolation(right),
            ) 
Example #17
Source File: expr.py    From recruit with Apache License 2.0 5 votes vote down vote up
def _rewrite_membership_op(self, node, left, right):
        # the kind of the operator (is actually an instance)
        op_instance = node.op
        op_type = type(op_instance)

        # must be two terms and the comparison operator must be ==/!=/in/not in
        if is_term(left) and is_term(right) and op_type in self.rewrite_map:

            left_list, right_list = map(_is_list, (left, right))
            left_str, right_str = map(_is_str, (left, right))

            # if there are any strings or lists in the expression
            if left_list or right_list or left_str or right_str:
                op_instance = self.rewrite_map[op_type]()

            # pop the string variable out of locals and replace it with a list
            # of one string, kind of a hack
            if right_str:
                name = self.env.add_tmp([right.value])
                right = self.term_type(name, self.env)

            if left_str:
                name = self.env.add_tmp([left.value])
                left = self.term_type(name, self.env)

        op = self.visit(op_instance)
        return op, op_instance, left, right 
Example #18
Source File: operators.py    From wemake-python-styleguide with MIT License 5 votes vote down vote up
def _check_zero_division(self, op: ast.operator, number: ast.AST) -> None:
        number = unwrap_unary_node(number)

        is_zero_division = (
            isinstance(op, self._zero_divisors) and
            isinstance(number, ast.Num) and
            number.n == 0
        )
        if is_zero_division:
            self.add_violation(consistency.ZeroDivisionViolation(number)) 
Example #19
Source File: expressions.py    From terracotta with MIT License 5 votes vote down vote up
def visit_Compare(self, node: ast.Compare) -> Any:
        if len(node.ops) > 1:
            raise ParseException('chained comparisons are not supported')

        op_type = type(node.ops[0])
        if op_type not in ExpressionParser.NODE_TO_COMPOP:
            raise ParseException(
                f'comparison operator {op_type.__name__} not allowed in expressions'
            )

        op_callable = ExpressionParser.NODE_TO_COMPOP[op_type]
        return op_callable(self.visit(node.left), self.visit(node.comparators[0])) 
Example #20
Source File: expressions.py    From terracotta with MIT License 5 votes vote down vote up
def visit_BinOp(self, node: ast.BinOp) -> Any:
        op_type = type(node.op)
        if op_type not in ExpressionParser.NODE_TO_BINOP:
            raise ParseException(
                f'binary operator {op_type.__name__} not allowed in expressions'
            )

        op_callable = ExpressionParser.NODE_TO_BINOP[op_type]
        return op_callable(self.visit(node.left), self.visit(node.right)) 
Example #21
Source File: expressions.py    From terracotta with MIT License 5 votes vote down vote up
def visit_UnaryOp(self, node: ast.UnaryOp) -> Any:
        op_type = type(node.op)
        if op_type not in ExpressionParser.NODE_TO_UNOP:
            raise ParseException(
                f'unary operator {op_type.__name__} not allowed in expressions'
            )

        op_callable = ExpressionParser.NODE_TO_UNOP[op_type]
        return op_callable(self.visit(node.operand)) 
Example #22
Source File: expr.py    From elasticintel with GNU General Public License v3.0 5 votes vote down vote up
def _rewrite_membership_op(self, node, left, right):
        # the kind of the operator (is actually an instance)
        op_instance = node.op
        op_type = type(op_instance)

        # must be two terms and the comparison operator must be ==/!=/in/not in
        if is_term(left) and is_term(right) and op_type in self.rewrite_map:

            left_list, right_list = map(_is_list, (left, right))
            left_str, right_str = map(_is_str, (left, right))

            # if there are any strings or lists in the expression
            if left_list or right_list or left_str or right_str:
                op_instance = self.rewrite_map[op_type]()

            # pop the string variable out of locals and replace it with a list
            # of one string, kind of a hack
            if right_str:
                name = self.env.add_tmp([right.value])
                right = self.term_type(name, self.env)

            if left_str:
                name = self.env.add_tmp([left.value])
                left = self.term_type(name, self.env)

        op = self.visit(op_instance)
        return op, op_instance, left, right 
Example #23
Source File: expr.py    From Splunking-Crime with GNU Affero General Public License v3.0 5 votes vote down vote up
def _rewrite_membership_op(self, node, left, right):
        # the kind of the operator (is actually an instance)
        op_instance = node.op
        op_type = type(op_instance)

        # must be two terms and the comparison operator must be ==/!=/in/not in
        if is_term(left) and is_term(right) and op_type in self.rewrite_map:

            left_list, right_list = map(_is_list, (left, right))
            left_str, right_str = map(_is_str, (left, right))

            # if there are any strings or lists in the expression
            if left_list or right_list or left_str or right_str:
                op_instance = self.rewrite_map[op_type]()

            # pop the string variable out of locals and replace it with a list
            # of one string, kind of a hack
            if right_str:
                name = self.env.add_tmp([right.value])
                right = self.term_type(name, self.env)

            if left_str:
                name = self.env.add_tmp([left.value])
                left = self.term_type(name, self.env)

        op = self.visit(op_instance)
        return op, op_instance, left, right 
Example #24
Source File: expr.py    From vnpy_crypto with MIT License 5 votes vote down vote up
def _rewrite_membership_op(self, node, left, right):
        # the kind of the operator (is actually an instance)
        op_instance = node.op
        op_type = type(op_instance)

        # must be two terms and the comparison operator must be ==/!=/in/not in
        if is_term(left) and is_term(right) and op_type in self.rewrite_map:

            left_list, right_list = map(_is_list, (left, right))
            left_str, right_str = map(_is_str, (left, right))

            # if there are any strings or lists in the expression
            if left_list or right_list or left_str or right_str:
                op_instance = self.rewrite_map[op_type]()

            # pop the string variable out of locals and replace it with a list
            # of one string, kind of a hack
            if right_str:
                name = self.env.add_tmp([right.value])
                right = self.term_type(name, self.env)

            if left_str:
                name = self.env.add_tmp([left.value])
                left = self.term_type(name, self.env)

        op = self.visit(op_instance)
        return op, op_instance, left, right 
Example #25
Source File: expr.py    From predictive-maintenance-using-machine-learning with Apache License 2.0 5 votes vote down vote up
def _rewrite_membership_op(self, node, left, right):
        # the kind of the operator (is actually an instance)
        op_instance = node.op
        op_type = type(op_instance)

        # must be two terms and the comparison operator must be ==/!=/in/not in
        if is_term(left) and is_term(right) and op_type in self.rewrite_map:

            left_list, right_list = map(_is_list, (left, right))
            left_str, right_str = map(_is_str, (left, right))

            # if there are any strings or lists in the expression
            if left_list or right_list or left_str or right_str:
                op_instance = self.rewrite_map[op_type]()

            # pop the string variable out of locals and replace it with a list
            # of one string, kind of a hack
            if right_str:
                name = self.env.add_tmp([right.value])
                right = self.term_type(name, self.env)

            if left_str:
                name = self.env.add_tmp([left.value])
                left = self.term_type(name, self.env)

        op = self.visit(op_instance)
        return op, op_instance, left, right 
Example #26
Source File: expr.py    From Computable with MIT License 5 votes vote down vote up
def _rewrite_assign(source):
    """Rewrite the assignment operator for PyTables expression that want to use
    ``=`` as a substitute for ``==``.
    """
    res = []
    g = tokenize.generate_tokens(StringIO(source).readline)
    for toknum, tokval, _, _, _ in g:
        res.append((toknum, '==' if tokval == '=' else tokval))
    return tokenize.untokenize(res) 
Example #27
Source File: expr.py    From Computable with MIT License 4 votes vote down vote up
def __init__(self, gbls=None, lcls=None, level=1, resolvers=None,
                 target=None):
        self.level = level
        self.resolvers = tuple(resolvers or [])
        self.globals = dict()
        self.locals = dict()
        self.target = target
        self.ntemps = 1  # number of temporary variables in this scope

        if isinstance(lcls, Scope):
            ld, lcls = lcls, dict()
            self.locals.update(ld.locals.copy())
            self.globals.update(ld.globals.copy())
            self.resolvers += ld.resolvers
            if ld.target is not None:
                self.target = ld.target
            self.update(ld.level)

        frame = sys._getframe(level)
        try:
            self.globals.update(gbls or frame.f_globals)
            self.locals.update(lcls or frame.f_locals)
        finally:
            del frame

        # add some useful defaults
        self.globals['Timestamp'] = pd.lib.Timestamp
        self.globals['datetime'] = datetime

        # SUCH a hack
        self.globals['True'] = True
        self.globals['False'] = False

        # function defs
        self.globals['list'] = list
        self.globals['tuple'] = tuple

        res_keys = (list(o.keys()) for o in self.resolvers)
        self.resolver_keys = frozenset(reduce(operator.add, res_keys, []))
        self._global_resolvers = self.resolvers + (self.locals, self.globals)
        self._resolver = None

        self.resolver_dict = {}
        for o in self.resolvers:
            self.resolver_dict.update(dict(o))