本文整理匯總了Python中ast.operator方法的典型用法代碼示例。如果您正苦於以下問題:Python ast.operator方法的具體用法?Python ast.operator怎麽用?Python ast.operator使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類ast
的用法示例。
在下文中一共展示了ast.operator方法的14個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: _rewrite_assign
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import operator [as 別名]
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
示例2: _op_maker
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import operator [as 別名]
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
示例3: _check_useless_math_operator
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import operator [as 別名]
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),
)
示例4: _check_string_concat
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import operator [as 別名]
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
示例5: _rewrite_membership_op
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import operator [as 別名]
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
示例6: _rewrite_assign
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import operator [as 別名]
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)
示例7: _rewrite_membership_op
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import operator [as 別名]
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
示例8: visit_UnaryOp
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import operator [as 別名]
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))
示例9: visit_BinOp
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import operator [as 別名]
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))
示例10: visit_Compare
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import operator [as 別名]
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]))
示例11: _check_zero_division
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import operator [as 別名]
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))
示例12: _check_negation
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import operator [as 別名]
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),
)
示例13: visit_NamedExpr
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import operator [as 別名]
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)
示例14: __init__
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import operator [as 別名]
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))