Exemple #1
0
def test_short_circuit():
    from xotl.ql import thesefy
    from xotl.ql.expressions import call
    from xotl.ql.translation.py import naive_translation
    from xoutil.compat import integer
    flag = [0]   # A list to allow non-global non-local in Py2k
    def inc_flag(by=1):
        flag[0] += 1
        return flag[0]

    @thesefy
    class Universe(integer):
        pass
    Universe.this_instances = [Universe(1780917517912941696167)]

    query = these(atom for atom in Universe if (call(inc_flag) > 1) & call(inc_flag))
    plan = naive_translation(query)
    list(plan())
    assert flag[0] == 1

    flag[0] = 0
    query = these(atom for atom in Universe if (call(inc_flag) > 0) | call(inc_flag))
    plan = naive_translation(query)
    list(plan())
    assert flag[0] == 1
Exemple #2
0
def test_translation_with_call_of_a_function():
    from xoutil.iterators import zip
    from xotl.ql.expressions import call
    from xotl.ql.translation.py import naive_translation

    @thesefy
    class Universe(int):
        pass
    Universe.this_instances = [Universe(i) for i in range(2, 10)] + ['invalid']

    def gcd(a, b):
        while a % b != 0:
            a, b = b, a % b
        return b

    expected = set((a, b) for a in range(2, 10) for b in range(2, 10) if a > b and gcd(a, b) == 1)
    assert expected == set([(3, 2),
                            (4, 3),
                            (5, 2), (5, 3), (5, 4),
                            (6, 5),
                            (7, 2), (7, 3), (7, 4), (7, 5), (7, 6),
                            (8, 3), (8, 5), (8, 7),
                            (9, 2), (9, 4), (9, 5), (9, 7), (9, 8)])

    query = these((a, b) for a, b in zip(Universe, Universe) if (a > b) & (call(gcd, a, b) == 1))
    plan = naive_translation(query)
    assert set(plan()) == set([(3, 2),
                               (4, 3),
                               (5, 2), (5, 3), (5, 4),
                               (6, 5),
                               (7, 2), (7, 3), (7, 4), (7, 5), (7, 6),
                               (8, 3), (8, 5), (8, 7),
                               (9, 2), (9, 4), (9, 5), (9, 7), (9, 8)])


    query = these(((a, b) for a, b in zip(Universe, Universe) if (a > b) & (call(gcd, a, b) == 1)), offset=100)
    plan = naive_translation(query)
    assert len(list(plan())) == 0
Exemple #3
0
    def test_calling_functions(self):
        from xotl.ql.expressions import call
        expression = this.startswith('manu')
        self.assertIsInstance(expression, ExpressionTree)
        self.assertEqual("call(this.startswith, manu)",
                         str(expression))
        equiv_expr = call(this.startswith, 'manu')
        with context(UNPROXIFING_CONTEXT):
            self.assertEqual(equiv_expr, expression)

        # But the calling a these instance directly is not supported
        # (I think is not pretty)
        with self.assertRaises(TypeError):
            this('someone')('cannot', 'call', 'me')
Exemple #4
0
    def test_calling_functions(self):
        from xotl.ql.expressions import call
        expression = this.startswith('manu')
        self.assertIsInstance(expression, ExpressionTree)
        self.assertEqual("call(this.startswith, manu)",
                         str(expression))
        equiv_expr = call(this.startswith, 'manu')
        with context(UNPROXIFING_CONTEXT):
            self.assertEqual(equiv_expr, expression)

        # But the calling a these instance directly is not supported
        # (I think is not pretty)
        with self.assertRaises(TypeError):
            this('someone')('cannot', 'call', 'me')