Ejemplo n.º 1
0
def test_naive_plan_no_join(**kwargs):
    from xoutil.iterators import dict_update_new
    from xotl.ql.translation.py import naive_translation
    select_old_entities = these(who
                                for who in Entity
                                if who.name.startswith('Manuel'))
    dict_update_new(kwargs, dict(only='test_translate.*'))
    plan = naive_translation(select_old_entities, **kwargs)
    result = list(plan())
    assert manu in result
    assert manolito in result
    assert yade not in result
Ejemplo n.º 2
0
def smart_getattr(name, *sources, **kwargs):
    '''Gets an attr by `name` for the first source that has it.

    This is roughly that same as::

       get_first_of(sources, name, default=Unset, **kwargs)

    .. warning:: Deprecated since 1.5.1

    '''
    from xoutil.iterators import dict_update_new
    dict_update_new(kwargs, {'default': Unset})
    return get_first_of(sources, name, **kwargs)
Ejemplo n.º 3
0
def test_ridiculous_join(**kwargs):
    from itertools import product
    from xoutil.iterators import dict_update_new
    from xotl.ql.translation.py import naive_translation
    select_old_entities = these((who, who2)
                                for who in Person
                                for who2 in Person)
    dict_update_new(kwargs, dict(only='test_translate.*'))
    plan = naive_translation(select_old_entities, **kwargs)
    result = list(plan())
    source = (elsa, manu, denia, pedro, yade, manolito)
    for pair in product(source, source):
        assert pair in result
Ejemplo n.º 4
0
def test_traversing_by_nonexistent_attribute(**kwargs):
    from xoutil.iterators import dict_update_new
    from xotl.ql.translation.py import naive_translation
    dict_update_new(kwargs, dict(only='test_translate.*'))

    # There's no `childs` attribute in Persons
    query = get_query_object(
        child
        for parent in Person
        if parent.childs and parent.age > 30
        for child in parent.childs
        if child.age < 10
    )
    plan = naive_translation(query, **kwargs)
    assert list(plan()) == []

    # And much less a `foobar`
    query = get_query_object(parent for parent in Person if parent.foobar)
    plan = naive_translation(query, **kwargs)
    assert list(plan()) == []

    # And traversing through a non-existing stuff doesn't make
    # any sense either, but should not fail
    query = get_query_object(
        foos.name
        for person in Person
        for foos in person.foobars
    )
    plan = naive_translation(query, **kwargs)
    assert list(plan()) == []

    # However either trying to traverse to a second level without testing
    # should fail
    query = get_query_object(a for p in this for a in p.b.a)
    plan = naive_translation(query, **kwargs)
    with pytest.raises(AttributeError):
        list(plan())

    # The same query in a safe fashion
    query = get_query_object(a
                  for p in this
                  if p.b and p.b.a
                  for a in p.b.a)
    plan = naive_translation(query, **kwargs)
    assert list(plan()) == []

    # Now let's rerun the plan after we create some object that matches
    x = X()
    assert list(plan()) == x.b.a
Ejemplo n.º 5
0
        def __getitem__(self, key):
            from xoutil import Unset
            from xoutil.iterators import dict_update_new
            from six import PY3
            d = self.d
            res = d.get(key, Unset)
            f = self.f
            if res is Unset and f:
                f_globals = self.f_globals
                if PY3:
                    # FIXME: This modifies f_globals! Use f_builtins of the
                    # frame.

                    # In Py3k (at least Python 3.2) builtins are not directly
                    # in f_globals but inside a __builtins__ key.
                    builtins = f_globals.get('__builtins__', {})
                    dict_update_new(f_globals, builtins)
                while f and res is Unset:
                    dict_update_new(d, f.f_locals)
                    res = d.get(key, Unset)
                    f = self.f = f.f_back
                if res is Unset and f_globals:
                    dict_update_new(d, f_globals)
                    res = d.get(key, Unset)
                    # At this point there's no use to keep the reference to
                    # frames since we have reached back to the global context,
                    # so it's best to clear of reference to the last frame
                    # in order to keep this CPython-friendly.
                    self.f = None
                    self.f_globals = None
            if res:
                return res
            else:
                raise KeyError
Ejemplo n.º 6
0
def test_all_pred(**kwargs):
    from xoutil.iterators import dict_update_new
    from xotl.ql.expressions import all_, sum_
    from xotl.ql.translation.py import naive_translation
    query = these(parent
                  for parent in Person
                  if parent.children
                  if all_((30 < child.age) & (child.age < 36) for child in parent.children))
    dict_update_new(kwargs, dict(only='test_translate.*'))
    plan = naive_translation(query, **kwargs)
    result = list(plan())
    assert elsa in result
    assert papi in result
    assert len(result) == 2

    query = these(parent
                  for parent in Person
                  if parent.children
                  if all_(parent.name.startswith('Manu'), parent.age > 30))

    dict_update_new(kwargs, dict(only='test_translate.*'))
    plan = naive_translation(query, **kwargs)
    with pytest.raises(SyntaxError):
        result = list(plan())


    query = these(parent
                  for parent in Person
                  if parent.children
                  if sum_(child.age for child in parent.children) > 60)

    dict_update_new(kwargs, dict(only='test_translate.*'))
    plan = naive_translation(query, **kwargs)
    result = list(plan())
    assert denia in result
    assert pedro in result
    assert len(result) == 2
Ejemplo n.º 7
0
 def __new__(cls, name, bases, attrs):
     from xoutil.iterators import dict_update_new
     baseattrs = {'__doc__': getattr(bases[0], '__doc__', ''),
                  '__module__': getattr(bases[0], '__module__', '')}
     dict_update_new(attrs, baseattrs)
     return super(new_meta, cls).__new__(cls, name, bases, attrs)