本文整理匯總了Python中ast.cmpop方法的典型用法代碼示例。如果您正苦於以下問題:Python ast.cmpop方法的具體用法?Python ast.cmpop怎麽用?Python ast.cmpop使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類ast
的用法示例。
在下文中一共展示了ast.cmpop方法的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: _compare_to_3
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import cmpop [as 別名]
def _compare_to_3(
test: ast.Compare,
op: Union[Type[ast.cmpop], Tuple[Type[ast.cmpop], ...]],
) -> bool:
if not (
isinstance(test.ops[0], op) and
isinstance(test.comparators[0], ast.Tuple) and
len(test.comparators[0].elts) >= 1 and
all(isinstance(n, ast.Num) for n in test.comparators[0].elts)
):
return False
# checked above but mypy needs help
elts = cast('List[ast.Num]', test.comparators[0].elts)
return elts[0].n == 3 and all(n.n == 0 for n in elts[1:])
示例2: _is_correct_len
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import cmpop [as 別名]
def _is_correct_len(sign: ast.cmpop, comparator: ast.AST) -> bool:
"""This is a helper function to tell what calls to ``len()`` are valid."""
if isinstance(operators.unwrap_unary_node(comparator), ast.Num):
numeric_value = ast.literal_eval(comparator)
if numeric_value == 0:
return False
if numeric_value == 1:
return not isinstance(sign, (ast.GtE, ast.Lt))
return True
示例3: _check_constant
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import cmpop [as 別名]
def _check_constant(self, op: ast.cmpop, comparator: ast.expr) -> None:
if not isinstance(op, (ast.Eq, ast.NotEq, ast.Is, ast.IsNot)):
return
real = get_assigned_expr(comparator)
if not isinstance(real, (ast.List, ast.Dict, ast.Tuple)):
return
length = len(real.keys) if isinstance(
real, ast.Dict,
) else len(real.elts)
if not length:
self.add_violation(FalsyConstantCompareViolation(comparator))
示例4: _check_is_constant_comprare
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import cmpop [as 別名]
def _check_is_constant_comprare(
self,
op: ast.cmpop,
comparator: ast.expr,
) -> None:
if not isinstance(op, (ast.Is, ast.IsNot)):
return
unwrapped = operators.unwrap_unary_node(
get_assigned_expr(comparator),
)
if isinstance(unwrapped, self._forbidden_for_is):
self.add_violation(WrongIsCompareViolation(comparator))
示例5: get_similar_operators
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import cmpop [as 別名]
def get_similar_operators(
operator: ast.cmpop,
) -> Union[Type[ast.cmpop], _MultipleCompareOperators]:
"""Returns similar operators types for the given operator."""
operator_type = operator.__class__
return SIMILAR_OPERATORS.get(operator_type, operator_type)
示例6: _mutate
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import cmpop [as 別名]
def _mutate(
self,
comparison_node: ast.Compare,
operator: ast.cmpop,
name: str,
is_left: bool,
) -> None:
key_name = None
if isinstance(operator, (ast.Lt, ast.LtE)):
key_name = 'lower_bound' if is_left else 'upper_bound'
elif isinstance(operator, (ast.Gt, ast.GtE)):
key_name = 'upper_bound' if is_left else 'lower_bound'
if key_name:
getattr(self._uses[name], key_name).add(comparison_node)