本文整理汇总了Python中xoutil.context.context函数的典型用法代码示例。如果您正苦于以下问题:Python context函数的具体用法?Python context怎么用?Python context使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了context函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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
示例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']))
示例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)
示例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]))
示例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
示例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)
示例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)))
示例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)
示例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)
示例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))
示例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)
示例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])
示例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
示例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
示例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