• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Python context.context函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Python中xoutil.context.context函数的典型用法代码示例。如果您正苦于以下问题:Python context函数的具体用法?Python context怎么用?Python context使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了context函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。

示例1: test_data_is_an_opendict

def test_data_is_an_opendict():
    c1 = object()
    with context(c1, a=1, b=1) as cc1:
        with context(c1, a=2) as cc2:
            assert cc2 is cc1
            assert cc2.a == 2
            assert cc2.b == 1   # Given by the upper enclosing level
            cc2.b = 'jaile!d'
        assert cc1.a == 1
        assert cc1['b'] == 1
开发者ID:merchise-autrement,项目名称:xoutil,代码行数:10,代码来源:test_context.py


示例2: test_simple_contexts

 def test_simple_contexts(self):
     with context('CONTEXT-1'):
         self.assertIsNot(None, context['CONTEXT-1'])
         with context('CONTEXT-1'):
             with context('context-2'):
                 self.assertIsNot(None, context['CONTEXT-1'])
                 self.assertIsNot(None, context['context-2'])
             self.assertEquals(False, bool(context['context-2']))
         self.assertIsNot(None, context['CONTEXT-1'])
     self.assertEquals(False, bool(context['CONTEXT-1']))
开发者ID:merchise-autrement,项目名称:xoutil,代码行数:10,代码来源:test_context.py


示例3: test_20120822_reversed_eq_and_ne_should_compare_equal

    def test_20120822_reversed_eq_and_ne_should_compare_equal(self):
        expr = 1 == q("2")
        expr2 = q(1) == "2"
        with context(UNPROXIFING_CONTEXT):
            self.assertEqual(expr, expr2)

        # But we have not a reversing equality stuff.
        expr = 1 < q(2)
        expr2 = q(2) > 1
        with context(UNPROXIFING_CONTEXT):
            self.assertNotEqual(expr, expr2)
开发者ID:mvaled,项目名称:xotl.ql,代码行数:11,代码来源:test_expressions.py


示例4: test_with_objects

 def test_with_objects(self):
     CONTEXT1 = object()
     CONTEXT2 = object()
     with context(CONTEXT1):
         self.assertIsNot(None, context[CONTEXT1])
         with context(CONTEXT1):
             with context(CONTEXT2):
                 self.assertIsNot(None, context[CONTEXT1])
                 self.assertIsNot(None, context[CONTEXT2])
             self.assertEquals(False, bool(context[CONTEXT2]))
         self.assertIsNot(None, context[CONTEXT1])
     self.assertEquals(False, bool(context[CONTEXT1]))
开发者ID:merchise-autrement,项目名称:xoutil,代码行数:12,代码来源:test_context.py


示例5: __iter__

    def __iter__(self):
        '''Yields a single instance of a :class:`bound term
        <xotl.ql.interfaces.IBoundTerm>` that is a clone of the current.

        This allows an idiomatic way to express queries::

            query_expression = ((parent, child)
                                for parent in this('parent')
                                for child in parent.children)

        See :class:`these`.

        '''
        with context(UNPROXIFING_CONTEXT):
            name = self.name
            parent = self.parent
            if not name:
                # When iterating an instance without a name (i.e the `this`
                # object), we should generate a new name (of those simple
                # mortals can't use)
                with context('_INVALID_THESE_NAME'):
                    name = self._newname()
                    bound_term = Term(name, parent=parent, binding=None)
            else:
                bound_term = Term(name, parent=parent, binding=None)
            token = GeneratorToken(expression=bound_term)
            bound_term.binding = token
            # XXX: Since bound_terms might span accross subqueries, there might
            # be several stacked bubbles around. For instance::
            #
            #    (parent
            #        for parent in this('parent')
            #        if any_(child for child in parent.children if child.age < 6))
            #
            # In this case, the enclosed parent.children emits to the top-most
            # bubble, but when the iter is invoked by `any_` to resolve its
            # argument a second bubble is in place and the hack below (XXX)
            # won't work. Furthermore, the token's expression is the bound-term
            # while the emmited part was the original one. So we keep the
            # original_term so that operations that resolver their arguments as
            # subquery may remove the escaped part.
            bound_term.original_term = self
        # XXX: When emiting a token in the context of query, if this token's
        # expression is also a part, then it was emitted without binding, so we
        # need to manually check this case here
        bubble = context[EXPRESSION_CAPTURING].get('bubble', None)
        if bubble:
            parts = bubble._parts
            if parts and self is parts[-1]:
                parts.pop(-1)
            _emit_token(token)
        yield bound_term
开发者ID:mvaled,项目名称:xotl.ql,代码行数:52,代码来源:core.py


示例6: test_worst_case_must_have_3_filters_and_3_tokens

    def test_worst_case_must_have_3_filters_and_3_tokens(self):
        from itertools import izip
        query = these(person
                      for person, partner in izip(this('person'),
                                                  this('partner'))
                      for rel in this('relation')
                      if rel.type == 'partnership'
                      if rel.subject == person
                      if rel.object == partner
                      if partner.age > 32)
        filters = list(query.filters)
        tokens = list(query.tokens)
        person, partner, rel = this('person'), this('partner'), this('relation')
        expected_rel_type_filter = rel.type == 'partnership'
        expected_rel_subject_filter = rel.subject == person
        expected_rel_obj_filter = rel.object == partner
        expected_partner_age = partner.age > 32
        with context(UNPROXIFING_CONTEXT):
            self.assertIs(4, len(filters))
            self.assertIn(expected_rel_type_filter, filters)
            self.assertIn(expected_rel_subject_filter, filters)
            self.assertIn(expected_rel_obj_filter, filters)
            self.assertIn(expected_partner_age, filters)

            self.assertIn(person, tokens)
            self.assertIn(rel, tokens)
            self.assertIs(3, len(tokens))
            self.assertIn(partner, tokens)
开发者ID:pvergain,项目名称:xotl.ql,代码行数:28,代码来源:test_this_queries.py


示例7: test_named_terms_matches_a_token

    def test_named_terms_matches_a_token(self):
        '''
        Ensures that all terms are named, and they are bound to a token that is
        in the query.
        '''
        from itertools import izip
        from xotl.ql.core import thesefy
        from xotl.ql.translate import cofind_tokens

        @thesefy
        class Person(object):
            pass

        @thesefy
        class Partnership(object):
            pass

        query = these((person, partner)
                      for person, partner in izip(Person, Person)
                      for rel in Partnership
                      if (rel.subject == person) & (rel.obj == partner)
                      if person.age > 35)

        tokens = query.tokens
        matches_token = lambda term: (term.name and (
                                      term.binding.expression in tokens or
                                      matches_token(term.parent)))
        with context(UNPROXIFING_CONTEXT):
            self.assertTrue(all(matches_token(term)
                                for term in cofind_tokens(*query.filters)))
开发者ID:pvergain,项目名称:xotl.ql,代码行数:30,代码来源:test_this_queries.py


示例8: test_basic_queries

    def test_basic_queries(self):
        from xotl.ql.expressions import count
        query = these((parent.title + parent.name, count(child.toys))
                        for parent in this('parent')
                        if parent.age < 40
                        for child in parent.children
                        if child.age > 5)
        self.assertTrue(provides_any(query, IQueryObject))

        (parent_full_name, child_toys) = query.selection
        full_name_expectation = this('parent').title + this('parent').name
#        child_name_expectation = this('parent').children.name
        child_toys_expectation = count(this('parent').children.toys)

        parent_age_test = this('parent').age < 40
        children_age_test = this('parent').children.age > 5

        parent_token = this('parent')
        children_token = this('parent').children

        with context(UNPROXIFING_CONTEXT):
            self.assertEqual(parent_full_name, full_name_expectation)
#            self.assertEqual(child_name, child_name_expectation)
            self.assertEqual(child_toys, child_toys_expectation)

            filters = query.filters
            self.assertEqual(2, len(filters))
            self.assertIn(parent_age_test, filters)
            self.assertIn(children_age_test, filters)

            tokens = query.tokens
            self.assertEqual(2, len(tokens))
            self.assertIn(parent_token, tokens)
            self.assertIn(children_token, tokens)
开发者ID:pvergain,项目名称:xotl.ql,代码行数:34,代码来源:test_this_queries.py


示例9: test_complex_query_with_3_tokens

    def test_complex_query_with_3_tokens(self):
        query = these((parent.title + parent.name,
                       child.name + child.nick, toy.name)
                      for parent in this('parent')
                      if parent.children
                      for child in parent.children
                      if child.toys
                      for toy in child.toys
                      if (parent.age > 32) & (child.age < 5) |
                         (toy.type == 'laptop'))
        p = this('parent')
        c = p.children
        t = c.toys

        filters = query.filters
        expected_filters = [((p.age > 32) & (c.age < 5) | (t.type == 'laptop')),
                            p.children, c.toys]

        tokens = query.tokens
        expected_tokens = [p, c, t]

        selection = query.selection
        expected_selection = (p.title + p.name, c.name + c.nick, t.name)
        with context(UNPROXIFING_CONTEXT):
            self.assertEqual(selection, expected_selection)

            self.assertEqual(len(expected_filters), len(filters))
            for f in expected_filters:
                self.assertIn(f, filters)

            self.assertEqual(len(expected_tokens), len(tokens))
            for t in expected_tokens:
                self.assertIn(t, tokens)
开发者ID:pvergain,项目名称:xotl.ql,代码行数:33,代码来源:test_this_queries.py


示例10: test_named_terms_matches_a_token

def test_named_terms_matches_a_token():
    '''
    Ensures that all terms are named, and they are bound to a token that is
    in the query.
    '''
    from xoutil.iterators import zip
    from xotl.ql.core import thesefy
    from xotl.ql.translation import cotraverse_expression

    @thesefy
    class Person(object):
        pass

    @thesefy
    class Partnership(object):
        pass

    query = these((person, partner)
                  for person, partner in zip(Person, Person)
                  for rel in Partnership
                  if (rel.subject == person) & (rel.obj == partner)
                  if person.age > 35)

    tokens = [tk.expression for tk in query.tokens]
    matches_token = lambda term: (term.name and (
                                  term.binding.expression in tokens or
                                  matches_token(term.parent)))
    with context(UNPROXIFING_CONTEXT):
        assert all(matches_token(term)
                   for term in cotraverse_expression(*query.filters))
开发者ID:mvaled,项目名称:xotl.ql,代码行数:30,代码来源:test_this_queries.py


示例11: get_term_path

def get_term_path(term):
    '''Returns a tuple of all the names in the path to a term.

    For example::

       >>> from xotl.ql import this
       >>> get_term_path(this('p').a.b.c)
       ('p', 'a', 'b', 'c')

    The unnamed term ``this`` is treated specially by returning None. For
    example::

        >>> get_term_path(this.a)
        (None, 'a')

    '''
    with context(UNPROXIFING_CONTEXT):
        from xotl.ql.core import this
        res = []
        current = term
        while current:
            if current is not this:
                res.insert(0, current.name)
            else:
                res.insert(0, None)
            current = current.parent
        return tuple(res)
开发者ID:mvaled,项目名称:xotl.ql,代码行数:27,代码来源:__init__.py


示例12: test

 def test(self):
     operator = getattr(op, '_python_operator', op)
     query = these(parent for parent in this('p') if operator(parent.age, parent.check, parent.names))
     expected = operator(this('p').age, this('p').check, this('p').names)
     self.assertIs(1, len(query.filters))
     with context(UNPROXIFING_CONTEXT):
         self.assertEqual(expected, query.filters[0])
开发者ID:mvaled,项目名称:xotl.ql,代码行数:7,代码来源:test_this_queries.py


示例13: __call__

 def __call__(self, *args, **kwargs):
     with context(UNPROXIFING_CONTEXT):
         instance = self.expression
         token = self.token
     result = QueryPart(expression=instance(*args, **kwargs),
                        token=token)
     return result
开发者ID:pvergain,项目名称:xotl.ql,代码行数:7,代码来源:core.py


示例14: test_basic_queries

def test_basic_queries():
    from xotl.ql.expressions import count
    query = these((parent.title + parent.name, count(child.toys))
                    for parent in this('parent')
                    if parent.age < 40
                    for child in parent.children
                    if child.age > 5)
    assert provides_any(query, IQueryObject)

    (parent_full_name, child_toys) = query.selection
    full_name_expectation = this('parent').title + this('parent').name
    child_toys_expectation = count(this('parent').children.toys)

    parent_age_test = this('parent').age < 40
    children_age_test = this('parent').children.age > 5

    parent_token = this('parent')
    children_token = this('parent').children

    with context(UNPROXIFING_CONTEXT):
        assert parent_full_name == full_name_expectation
        assert child_toys == child_toys_expectation

        filters = query.filters
        assert len(filters) == 2
        assert parent_age_test in filters
        assert children_age_test in filters

        tokens = [tk.expression for tk in query.tokens]
        assert len(tokens) == 2
        assert parent_token in tokens
        assert children_token in tokens
开发者ID:mvaled,项目名称:xotl.ql,代码行数:32,代码来源:test_this_queries.py


示例15: test_cotraverse_expression

def test_cotraverse_expression():
    from xoutil.compat import zip
    from xotl.ql.expressions import is_a
    from xotl.ql.translation import cotraverse_expression

    @thesefy
    class Person(object):
        pass

    @thesefy
    class Partnership(object):
        pass

    query = these((person, partner)
                  for person, partner in zip(Person, Person)
                  for rel in Partnership
                  if (rel.subject == person) & (rel.obj == partner))
    filters = list(query.filters)
    person, partner = query.selection
    person_is_a_person = is_a(person, Person)
    partner_is_a_person = is_a(partner, Person)
    rel_token = query.tokens[-1]
    rel_subject = rel_token.expression.subject
    rel_obj = rel_token.expression.obj
    with context(UNPROXIFING_CONTEXT):
        assert person != partner
        assert person_is_a_person in filters
        assert partner_is_a_person in filters
        expected_terms_order = [person, partner, rel_token.expression, rel_subject, person, rel_obj, partner]
        assert expected_terms_order == list(cotraverse_expression(query))

    assert UNPROXIFING_CONTEXT not in context
开发者ID:mvaled,项目名称:xotl.ql,代码行数:32,代码来源:test_translation_utils.py


示例16: capture_token

    def capture_token(self, token):
        '''Captures an emitted token.

        When a token is emitted if the last previously created part is a term
        that *is* the same as the :attr:`IGeneratorToken.expression`, then this
        last term should be removed from the particles collection.

        This is because in a query like::

            these((parent, child)
                  for parent in this
                  for child in parent.children)

        The `parent.children` emits itself as a query part and immediately it
        is transformed to a token.

        :param token: The emitted token
        :type token: :class:`IGeneratorToken`
        '''
        tokens = self._tokens
        with context(UNPROXIFING_CONTEXT):
            assert IGeneratorToken.providedBy(token)
            parts = self._parts
            if parts:
                top = parts.pop(-1)
                if token.expression is not top:
                    parts.append(top)
        if token not in tokens:
            tokens.append(token)
            self._particles.append(token)
开发者ID:pvergain,项目名称:xotl.ql,代码行数:30,代码来源:core.py


示例17: __eq__

    def __eq__(self, other):
        '''
            >>> with context(UNPROXIFING_CONTEXT):
            ...    this('parent') == this('parent')
            True

            >>> from xotl.ql.expressions import _true
            >>> (this('parent') == this('parent')) is _true
            True
        '''
        from xotl.ql.expressions import eq
        with context(UNPROXIFING_CONTEXT):
            if isinstance(other, Term):
                res = validate_attrs(self, other, ('name', 'parent'))
            else:
                res = False
        if context[UNPROXIFING_CONTEXT]:
            return res
        else:
            if not res:
                return eq(self, other)
            else:
                # In logic A == A is always true so we don't produce nothing
                # for it.
                return _true
开发者ID:pvergain,项目名称:xotl.ql,代码行数:25,代码来源:core.py


示例18: method

 def method(self):
     with context(UNPROXIFING_CONTEXT):
         instance = self.expression
         token = self.token
     result = QueryPart(expression=operation(instance),
                        token=token)
     return result
开发者ID:pvergain,项目名称:xotl.ql,代码行数:7,代码来源:core.py


示例19: _resolve_arguments

    def _resolve_arguments(cls, *children, **kwargs):
        '''Resolves the first and only positional argument as a sub-query by
        calling :class:`~xotl.ql.core.these`.

        If there is more than one positional argument, or even one keyword
        argument, or the first argument is not a generator object; then it
        leaves all the arguments the same.

        '''
        import types
        first, rest = children[0], children[1:]
        if not first or (rest or kwargs):
            return children, kwargs
        else:
            if isinstance(first, types.GeneratorType):
                from xotl.ql.core import these
                first = these(first)
                # XXX: If this operation itself is enclosed in a
                # EXPRESSION_CAPTURING it might have occurred that a part
                # (actually a token's term) was emitted but then used as the
                # generator, so if the first token's binding original_term *is*
                # the last part emitted it should be removed from the bubble.
                bubble = context[EXPRESSION_CAPTURING].get('bubble', None)
                if bubble:
                    parts = bubble._parts
                    with context(UNPROXIFING_CONTEXT):
                        term = first.tokens[0].expression
                        if getattr(term, 'original_term', None) is parts[-1]:
                            parts.pop(-1)
            return (first, ), {}
开发者ID:mvaled,项目名称:xotl.ql,代码行数:30,代码来源:expressions.py


示例20: test_worst_case_must_have_3_filters_and_3_tokens

def test_worst_case_must_have_3_filters_and_3_tokens():
    from xoutil.iterators import zip
    query = these(person
                  for person, partner in zip(this('person'),
                                             this('partner'))
                  for rel in this('relation')
                  if rel.type == 'partnership'
                  if rel.subject == person
                  if rel.object == partner
                  if partner.age > 32)
    filters = list(query.filters)
    tokens = [tk.expression for tk in query.tokens]
    person, partner, rel = this('person'), this('partner'), this('relation')
    expected_rel_type_filter = rel.type == 'partnership'
    expected_rel_subject_filter = rel.subject == person
    expected_rel_obj_filter = rel.object == partner
    expected_partner_age = partner.age > 32
    with context(UNPROXIFING_CONTEXT):
        assert len(filters) == 4
        assert expected_rel_type_filter in filters
        assert expected_rel_subject_filter in filters
        assert expected_rel_obj_filter in filters
        assert expected_partner_age in filters

        assert len(tokens) == 3
        assert person in tokens
        assert rel in tokens
        assert partner in tokens
开发者ID:mvaled,项目名称:xotl.ql,代码行数:28,代码来源:test_this_queries.py



注:本文中的xoutil.context.context函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Python snapvideo.Video类代码示例发布时间:2022-05-26
下一篇:
Python generator.XOSProcessorArgs类代码示例发布时间:2022-05-26
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap