python类In()的实例源码

rewrite.py 文件源码 项目:hostapd-mana 作者: adde88 项目源码 文件源码 阅读 28 收藏 0 点赞 0 评论 0
def load_module(self, name):
        # If there is an existing module object named 'fullname' in
        # sys.modules, the loader must use that existing module. (Otherwise,
        # the reload() builtin will not work correctly.)
        if name in sys.modules:
            return sys.modules[name]

        co, pyc = self.modules.pop(name)
        # I wish I could just call imp.load_compiled here, but __file__ has to
        # be set properly. In Python 3.2+, this all would be handled correctly
        # by load_compiled.
        mod = sys.modules[name] = imp.new_module(name)
        try:
            mod.__file__ = co.co_filename
            # Normally, this attribute is 3.2+.
            mod.__cached__ = pyc
            mod.__loader__ = self
            py.builtin.exec_(co, mod.__dict__)
        except:
            del sys.modules[name]
            raise
        return sys.modules[name]
astTools.py 文件源码 项目:ITAP-django 作者: krivers 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
def doCompare(op, left, right):
    """Perform the given AST comparison on the values"""
    top = type(op)
    if top == ast.Eq:
        return left == right
    elif top == ast.NotEq:
        return left != right
    elif top == ast.Lt:
        return left < right
    elif top == ast.LtE:
        return left <= right
    elif top == ast.Gt:
        return left > right
    elif top == ast.GtE:
        return left >= right
    elif top == ast.Is:
        return left is right
    elif top == ast.IsNot:
        return left is not right
    elif top == ast.In:
        return left in right
    elif top == ast.NotIn:
        return left not in right
rewrite.py 文件源码 项目:sslstrip-hsts-openwrt 作者: adde88 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def load_module(self, name):
        # If there is an existing module object named 'fullname' in
        # sys.modules, the loader must use that existing module. (Otherwise,
        # the reload() builtin will not work correctly.)
        if name in sys.modules:
            return sys.modules[name]

        co, pyc = self.modules.pop(name)
        # I wish I could just call imp.load_compiled here, but __file__ has to
        # be set properly. In Python 3.2+, this all would be handled correctly
        # by load_compiled.
        mod = sys.modules[name] = imp.new_module(name)
        try:
            mod.__file__ = co.co_filename
            # Normally, this attribute is 3.2+.
            mod.__cached__ = pyc
            mod.__loader__ = self
            py.builtin.exec_(co, mod.__dict__)
        except:
            del sys.modules[name]
            raise
        return sys.modules[name]
rewrite.py 文件源码 项目:godot-python 作者: touilleMan 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def load_module(self, name):
        # If there is an existing module object named 'fullname' in
        # sys.modules, the loader must use that existing module. (Otherwise,
        # the reload() builtin will not work correctly.)
        if name in sys.modules:
            return sys.modules[name]

        co, pyc = self.modules.pop(name)
        # I wish I could just call imp.load_compiled here, but __file__ has to
        # be set properly. In Python 3.2+, this all would be handled correctly
        # by load_compiled.
        mod = sys.modules[name] = imp.new_module(name)
        try:
            mod.__file__ = co.co_filename
            # Normally, this attribute is 3.2+.
            mod.__cached__ = pyc
            mod.__loader__ = self
            py.builtin.exec_(co, mod.__dict__)
        except:
            del sys.modules[name]
            raise
        return sys.modules[name]
rewrite.py 文件源码 项目:godot-python 作者: touilleMan 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def load_module(self, name):
        # If there is an existing module object named 'fullname' in
        # sys.modules, the loader must use that existing module. (Otherwise,
        # the reload() builtin will not work correctly.)
        if name in sys.modules:
            return sys.modules[name]

        co, pyc = self.modules.pop(name)
        # I wish I could just call imp.load_compiled here, but __file__ has to
        # be set properly. In Python 3.2+, this all would be handled correctly
        # by load_compiled.
        mod = sys.modules[name] = imp.new_module(name)
        try:
            mod.__file__ = co.co_filename
            # Normally, this attribute is 3.2+.
            mod.__cached__ = pyc
            mod.__loader__ = self
            py.builtin.exec_(co, mod.__dict__)
        except:
            del sys.modules[name]
            raise
        return sys.modules[name]
test_fatoptimizer.py 文件源码 项目:fatoptimizer 作者: vstinner 项目源码 文件源码 阅读 29 收藏 0 点赞 0 评论 0
def test_repr_global(self):
        # In func()/method(), repr() builtin cannot be copied to constant,
        # because the call to __fat__.replace_consts(func.__code__, {'...': repr}) would
        # load the local repr() function instead of the builtin repr()
        # function.
        self.config._copy_builtin_to_constant.add('repr')

        self.check_dont_optimize("""
            def repr(obj):
                return 'local'

            def func(obj):
                return repr(obj)
        """)

        self.check_dont_optimize("""
            class MyClass:
                @staticmethod
                def repr(obj):
                    return 'local'

                def method(self, obj):
                    return repr(obj)
        """)
rewrite.py 文件源码 项目:GSM-scanner 作者: yosriayed 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def load_module(self, name):
        # If there is an existing module object named 'fullname' in
        # sys.modules, the loader must use that existing module. (Otherwise,
        # the reload() builtin will not work correctly.)
        if name in sys.modules:
            return sys.modules[name]

        co, pyc = self.modules.pop(name)
        # I wish I could just call imp.load_compiled here, but __file__ has to
        # be set properly. In Python 3.2+, this all would be handled correctly
        # by load_compiled.
        mod = sys.modules[name] = imp.new_module(name)
        try:
            mod.__file__ = co.co_filename
            # Normally, this attribute is 3.2+.
            mod.__cached__ = pyc
            mod.__loader__ = self
            py.builtin.exec_(co, mod.__dict__)
        except:
            del sys.modules[name]
            raise
        return sys.modules[name]
expr.py 文件源码 项目:viper 作者: ethereum 项目源码 文件源码 阅读 46 收藏 0 点赞 0 评论 0
def compare(self):
        left = Expr.parse_value_expr(self.expr.left, self.context)
        right = Expr.parse_value_expr(self.expr.comparators[0], self.context)
        if isinstance(self.expr.ops[0], ast.In) and \
           isinstance(right.typ, ListType):
            if not are_units_compatible(left.typ, right.typ.subtype) and not are_units_compatible(right.typ.subtype, left.typ):
                raise TypeMismatchException("Can't use IN comparison with different types!", self.expr)
            return self.build_in_comparator()
        else:
            if not are_units_compatible(left.typ, right.typ) and not are_units_compatible(right.typ, left.typ):
                raise TypeMismatchException("Can't compare values with different units!", self.expr)
        if len(self.expr.ops) != 1:
            raise StructureException("Cannot have a comparison with more than two elements", self.expr)
        if isinstance(self.expr.ops[0], ast.Gt):
            op = 'sgt'
        elif isinstance(self.expr.ops[0], ast.GtE):
            op = 'sge'
        elif isinstance(self.expr.ops[0], ast.LtE):
            op = 'sle'
        elif isinstance(self.expr.ops[0], ast.Lt):
            op = 'slt'
        elif isinstance(self.expr.ops[0], ast.Eq):
            op = 'eq'
        elif isinstance(self.expr.ops[0], ast.NotEq):
            op = 'ne'
        else:
            raise Exception("Unsupported comparison operator")
        if not is_numeric_type(left.typ) or not is_numeric_type(right.typ):
            if op not in ('eq', 'ne'):
                raise TypeMismatchException("Invalid type for comparison op", self.expr)
        ltyp, rtyp = left.typ.typ, right.typ.typ
        if ltyp == rtyp:
            return LLLnode.from_list([op, left, right], typ='bool', pos=getpos(self.expr))
        elif ltyp == 'decimal' and rtyp == 'num':
            return LLLnode.from_list([op, left, ['mul', right, DECIMAL_DIVISOR]], typ='bool', pos=getpos(self.expr))
        elif ltyp == 'num' and rtyp == 'decimal':
            return LLLnode.from_list([op, ['mul', left, DECIMAL_DIVISOR], right], typ='bool', pos=getpos(self.expr))
        else:
            raise TypeMismatchException("Unsupported types for comparison: %r %r" % (ltyp, rtyp), self.expr)
rewrite.py 文件源码 项目:hostapd-mana 作者: adde88 项目源码 文件源码 阅读 26 收藏 0 点赞 0 评论 0
def push_format_context(self):
        """Create a new formatting context.

        The format context is used for when an explanation wants to
        have a variable value formatted in the assertion message.  In
        this case the value required can be added using
        .explanation_param().  Finally .pop_format_context() is used
        to format a string of %-formatted values as added by
        .explanation_param().

        """
        self.explanation_specifiers = {}
        self.stack.append(self.explanation_specifiers)
rewrite.py 文件源码 项目:hostapd-mana 作者: adde88 项目源码 文件源码 阅读 35 收藏 0 点赞 0 评论 0
def visit_Name(self, name):
        # Display the repr of the name if it's a local variable or
        # _should_repr_global_name() thinks it's acceptable.
        locs = ast_Call(self.builtin("locals"), [], [])
        inlocs = ast.Compare(ast.Str(name.id), [ast.In()], [locs])
        dorepr = self.helper("should_repr_global_name", name)
        test = ast.BoolOp(ast.Or(), [inlocs, dorepr])
        expr = ast.IfExp(test, self.display(name), ast.Str(name.id))
        return name, self.explanation_param(expr)
transformations.py 文件源码 项目:ITAP-django 作者: krivers 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def allVariableNamesUsed(a):
    """Gathers all the variable names used in the ast"""
    if not isinstance(a, ast.AST):
        return []
    elif type(a) == ast.Name:
        return [a.id]
    elif type(a) == ast.Assign:
        """In assignments, ignore all pure names used- they're being assigned to, not used"""
        variables = allVariableNamesUsed(a.value)
        for target in a.targets:
            if type(target) == ast.Name:
                pass
            elif type(target) in [ast.Tuple, ast.List]:
                for elt in target.elts:
                    if type(elt) != ast.Name:
                        variables += allVariableNamesUsed(elt)
            else:
                variables += allVariableNamesUsed(target)
        return variables
    elif type(a) == ast.AugAssign:
        variables = allVariableNamesUsed(a.value)
        variables += allVariableNamesUsed(a.target)
        return variables
    variables = []
    for child in ast.iter_child_nodes(a):
        variables += allVariableNamesUsed(child)
    return variables
__init__.py 文件源码 项目:py2rb 作者: naitoh 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
def visit_Compare(self, node):
        """
        Compare(expr left, cmpop* ops, expr* comparators)
        """
        assert len(node.ops) == len(node.comparators)

        def compare_pair(left, comp, op):
            if (left == '__name__') and (comp == '"__main__"') or \
               (left == '"__main__"') and (comp == '__name__'):
                """ <Python>  __name__ == '__main__':
                    <Ruby>    __FILE__ == $0          """
                left = '__FILE__'
                comp = '$0'
            if isinstance(op, ast.In):
                return "%s.include?(%s)" % (comp, left)
            elif isinstance(op, ast.NotIn):
                return "!%s.include?(%s)" % (comp, left)
            elif isinstance(op, ast.Eq):
                return "%s == %s" % (left, comp)
            elif isinstance(op, ast.NotEq):
                return "%s != %s" % (left, comp)
            elif isinstance(op, ast.IsNot):
                return "!%s.equal?(%s)" % (left, comp)
            else:
                return "%s %s %s" % (left, self.get_comparison_op(op), comp)

        compare_list = []
        for i in range(len(node.ops)):
            if i == 0:
                left = self.visit(node.left)
            else:
                left = comp
            comp = self.visit(node.comparators[i])
            op = node.ops[i]
            pair = compare_pair(left, comp, op)
            if len(node.ops) == 1:
                return pair
            compare_list.append('(' + pair + ')')
        return ' and '.join(compare_list)

    # python 3
rewrite.py 文件源码 项目:sslstrip-hsts-openwrt 作者: adde88 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def push_format_context(self):
        """Create a new formatting context.

        The format context is used for when an explanation wants to
        have a variable value formatted in the assertion message.  In
        this case the value required can be added using
        .explanation_param().  Finally .pop_format_context() is used
        to format a string of %-formatted values as added by
        .explanation_param().

        """
        self.explanation_specifiers = {}
        self.stack.append(self.explanation_specifiers)
rewrite.py 文件源码 项目:sslstrip-hsts-openwrt 作者: adde88 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def visit_Name(self, name):
        # Display the repr of the name if it's a local variable or
        # _should_repr_global_name() thinks it's acceptable.
        locs = ast_Call(self.builtin("locals"), [], [])
        inlocs = ast.Compare(ast.Str(name.id), [ast.In()], [locs])
        dorepr = self.helper("should_repr_global_name", name)
        test = ast.BoolOp(ast.Or(), [inlocs, dorepr])
        expr = ast.IfExp(test, self.display(name), ast.Str(name.id))
        return name, self.explanation_param(expr)
test_ast.py 文件源码 项目:web_ctp 作者: molebot 项目源码 文件源码 阅读 26 收藏 0 点赞 0 评论 0
def test_compare(self):
        left = ast.Name("x", ast.Load())
        comp = ast.Compare(left, [ast.In()], [])
        self.expr(comp, "no comparators")
        comp = ast.Compare(left, [ast.In()], [ast.Num(4), ast.Num(5)])
        self.expr(comp, "different number of comparators and operands")
        comp = ast.Compare(ast.Num("blah"), [ast.In()], [left])
        self.expr(comp, "non-numeric", exc=TypeError)
        comp = ast.Compare(left, [ast.In()], [ast.Num("blah")])
        self.expr(comp, "non-numeric", exc=TypeError)
rewrite.py 文件源码 项目:godot-python 作者: touilleMan 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def push_format_context(self):
        """Create a new formatting context.

        The format context is used for when an explanation wants to
        have a variable value formatted in the assertion message.  In
        this case the value required can be added using
        .explanation_param().  Finally .pop_format_context() is used
        to format a string of %-formatted values as added by
        .explanation_param().

        """
        self.explanation_specifiers = {}
        self.stack.append(self.explanation_specifiers)
rewrite.py 文件源码 项目:godot-python 作者: touilleMan 项目源码 文件源码 阅读 22 收藏 0 点赞 0 评论 0
def visit_Name(self, name):
        # Display the repr of the name if it's a local variable or
        # _should_repr_global_name() thinks it's acceptable.
        locs = ast_Call(self.builtin("locals"), [], [])
        inlocs = ast.Compare(ast.Str(name.id), [ast.In()], [locs])
        dorepr = self.helper("should_repr_global_name", name)
        test = ast.BoolOp(ast.Or(), [inlocs, dorepr])
        expr = ast.IfExp(test, self.display(name), ast.Str(name.id))
        return name, self.explanation_param(expr)
rewrite.py 文件源码 项目:godot-python 作者: touilleMan 项目源码 文件源码 阅读 35 收藏 0 点赞 0 评论 0
def push_format_context(self):
        """Create a new formatting context.

        The format context is used for when an explanation wants to
        have a variable value formatted in the assertion message.  In
        this case the value required can be added using
        .explanation_param().  Finally .pop_format_context() is used
        to format a string of %-formatted values as added by
        .explanation_param().

        """
        self.explanation_specifiers = {}
        self.stack.append(self.explanation_specifiers)
test_fatoptimizer.py 文件源码 项目:fatoptimizer 作者: vstinner 项目源码 文件源码 阅读 26 收藏 0 点赞 0 评论 0
def test_contains_to_const(self):
        # list => tuple
        self.check_optimize_func("x in [1, 2]", "x in (1, 2)")

        # set => frozenset
        const = ast.Constant(value=frozenset({1, 2}))
        node = ast.Compare(left=ast.Name(id='x', ctx=ast.Load()),
                           ops=[ast.In()],
                           comparators=[const])
        self.check_optimize_func("x in {1, 2}", node)

        # [] is not a constant: don't optimize
        self.check_dont_optimize_func("x in [1, [], 2]")
        self.check_dont_optimize_func("x in {1, [], 2}")
const_fold.py 文件源码 项目:fatoptimizer 作者: vstinner 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def compare_cst(self, node):
        node_op = node.ops[0].__class__
        eval_op = EVAL_COMPARE.get(node_op)
        if eval_op is None:
            return

        if node_op in (ast.In, ast.NotIn):
            left_hashable = True
            right_types = ITERABLE_TYPES
        else:
            left_hashable = False
            right_types = None

        if left_hashable:
            left = get_constant(node.left)
        else:
            left = get_literal(node.left)
        if left is UNSET:
            return
        right = get_literal(node.comparators[0], types=right_types)
        if right is UNSET:
            return

        if (node_op in (ast.Eq, ast.NotEq)
           and ((isinstance(left, str) and isinstance(right, bytes))
                or (isinstance(left, bytes) and isinstance(right, str)))):
            # comparison between bytes and str can raise BytesWarning depending
            # on runtime option
            return

        try:
            result = eval_op(left, right)
        except TypeError:
            return
        return self.new_constant(node, result)
const_fold.py 文件源码 项目:fatoptimizer 作者: vstinner 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def visit_Compare(self, node):
        if not self.config.constant_folding:
            return

        if len(node.ops) != 1:
            # FIXME: implement 1 < 2 < 3
            return
        if len(node.comparators) != 1:
            # FIXME: support this case? What's the syntax of this case?
            return

        new_node = self.compare_cst(node)
        if new_node is not None:
            return new_node

        # replace 'None is None' with True
        if (isinstance(node.ops[0], (ast.Is, ast.IsNot))
           and isinstance(node.left, ast.Constant)
           and node.left.value is None
           and isinstance(node.comparators[0], ast.Constant)
           and node.comparators[0].value is None):
            result = isinstance(node.ops[0], ast.Is)
            return self.new_constant(node, result)

        # replace 'x in {1, 2}' with 'x in frozenset({1, 2})'
        # replace 'x in [1, 2]' with 'x in frozenset((1, 2))'
        if isinstance(node.ops[0], ast.In):
            new_node = self.compare_contains(node)
            if new_node is not None:
                return new_node
pytables.py 文件源码 项目:PyDataLondon29-EmbarrassinglyParallelDAWithAWSLambda 作者: SignalMedia 项目源码 文件源码 阅读 29 收藏 0 点赞 0 评论 0
def translate_In(self, op):
        return ast.Eq() if isinstance(op, ast.In) else op
where_expr.py 文件源码 项目:redbiom 作者: biocore 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def In():
    return _in
where_expr.py 文件源码 项目:redbiom 作者: biocore 项目源码 文件源码 阅读 26 收藏 0 点赞 0 评论 0
def whereeval(str_, get=None):
    """Evaluate a set operation string, where each Name is fetched"""
    if get is None:
        import redbiom
        config = redbiom.get_config()
        get = redbiom._requests.make_get(config)

    # Load is subject to indirection to simplify testing
    globals()['Load'] = make_Load(get)

    formed = ast.parse(str_, mode='eval')

    node_types = (ast.Compare, ast.In, ast.NotIn, ast.BoolOp, ast.And,
                  ast.Name, ast.Or, ast.Eq, ast.Lt, ast.LtE, ast.Gt, ast.GtE,
                  ast.NotEq, ast.Str, ast.Num, ast.Load, ast.Expression,
                  ast.Tuple, ast.Is, ast.IsNot)

    for node in ast.walk(formed):
        if not isinstance(node, node_types):
            raise TypeError("Unsupported node type: %s" % ast.dump(node))

    result = eval(ast.dump(formed))

    # clean up
    global Load
    del Load

    return result
test_ast.py 文件源码 项目:ouroboros 作者: pybee 项目源码 文件源码 阅读 44 收藏 0 点赞 0 评论 0
def test_compare(self):
        left = ast.Name("x", ast.Load())
        comp = ast.Compare(left, [ast.In()], [])
        self.expr(comp, "no comparators")
        comp = ast.Compare(left, [ast.In()], [ast.Num(4), ast.Num(5)])
        self.expr(comp, "different number of comparators and operands")
        comp = ast.Compare(ast.Num("blah"), [ast.In()], [left])
        self.expr(comp, "non-numeric", exc=TypeError)
        comp = ast.Compare(left, [ast.In()], [ast.Num("blah")])
        self.expr(comp, "non-numeric", exc=TypeError)
criteria.py 文件源码 项目:beval 作者: hyw208 项目源码 文件源码 阅读 28 收藏 0 点赞 0 评论 0
def In(self, key, *right):
        c = criteria_class.instance(Const.In, key, *right)
        self._push(c)
        return self
criteria.py 文件源码 项目:beval 作者: hyw208 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
def __init__(self, key, *right):
        super(In, self).__init__(key, right)
operators.py 文件源码 项目:mutpy 作者: mutpy 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def mutate_NotIn(self, node):
        return ast.In()
test_ast.py 文件源码 项目:kbe_server 作者: xiaohaoppy 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
def _assertTrueorder(self, ast_node, parent_pos, reverse_check = False):
        def should_reverse_check(parent, child):
            # In some situations, the children of nodes occur before
            # their parents, for example in a.b.c, a occurs before b
            # but a is a child of b.
            if isinstance(parent, ast.Call):
                if parent.func == child:
                    return True
            if isinstance(parent, (ast.Attribute, ast.Subscript)):
                return True
            return False

        if not isinstance(ast_node, ast.AST) or ast_node._fields is None:
            return
        if isinstance(ast_node, (ast.expr, ast.stmt, ast.excepthandler)):
            node_pos = (ast_node.lineno, ast_node.col_offset)
            if reverse_check:
                self.assertTrue(node_pos <= parent_pos)
            else:
                self.assertTrue(node_pos >= parent_pos)
            parent_pos = (ast_node.lineno, ast_node.col_offset)
        for name in ast_node._fields:
            value = getattr(ast_node, name)
            if isinstance(value, list):
                for child in value:
                    self._assertTrueorder(child, parent_pos,
                                         should_reverse_check(ast_node, child))
            elif value is not None:
                self._assertTrueorder(value, parent_pos,
                                      should_reverse_check(ast_node, value))
test_ast.py 文件源码 项目:kbe_server 作者: xiaohaoppy 项目源码 文件源码 阅读 29 收藏 0 点赞 0 评论 0
def test_compare(self):
        left = ast.Name("x", ast.Load())
        comp = ast.Compare(left, [ast.In()], [])
        self.expr(comp, "no comparators")
        comp = ast.Compare(left, [ast.In()], [ast.Num(4), ast.Num(5)])
        self.expr(comp, "different number of comparators and operands")
        comp = ast.Compare(ast.Num("blah"), [ast.In()], [left])
        self.expr(comp, "non-numeric", exc=TypeError)
        comp = ast.Compare(left, [ast.In()], [ast.Num("blah")])
        self.expr(comp, "non-numeric", exc=TypeError)


问题


面经


文章

微信
公众号

扫码关注公众号