Ejemplo n.º 1
0
    def testSubQuery(self):
        thread_pool = ThreadPoolExecutor(2)
        e = Engine(ThreadsExecutor(thread_pool))

        def query_a(fields, ids):
            data = {1: {'f': 2}}
            return [[data[i][f] for f in fields] for i in ids]

        r = graph.Edge(None, [
            graph.Edge('a', [
                graph.Field('f', query_a),
            ]),
        ])

        # ----------------------------------------------

        @define('[[:f]]', _name='inc_f')
        def inc_f(obj):
            return obj['f'] + 1

        r1 = graph.Edge(None, [
            graph.Edge('a1', subquery_fields(r, 'a', {
                'f1': inc_f(S.this),
            })),
            graph.Link('la1', None, 'a1', lambda: [1], True),
        ])

        self.assertEqual(e.execute(r1, read('[{:la1 [:f1]}]'))['la1'],
                         [{'f1': 3}])
Ejemplo n.º 2
0
    def testSubQuery(self):
        thread_pool = ThreadPoolExecutor(2)
        e = Engine(ThreadsExecutor(thread_pool))

        def query_a(fields, ids):
            data = {1: {'f': 2}}
            return [[data[i][f] for f in fields] for i in ids]

        r = graph.Edge(None, [
            graph.Edge('a', [
                graph.Field('f', query_a),
            ]),
        ])

        # ----------------------------------------------

        @define('[[:f]]', _name='inc_f')
        def inc_f(obj):
            return obj['f'] + 1

        r1 = graph.Edge(None, [
            graph.Edge('a1', subquery_fields(r, 'a', {
                'f1': inc_f(S.this),
            })),
            graph.Link('la1', None, 'a1', lambda: [1], True),
        ])

        self.assertEqual(
            e.execute(r1, read('[{:la1 [:f1]}]'))['la1'], [{
                'f1': 3
            }])
Ejemplo n.º 3
0
@define('[[:b {:y [:d]}]]', _name='bar')
def bar(x):
    return '{x[b]} {x[y][d]}'.format(x=x).upper()


@define('[[:d {:xs [:b]}]]', _name='baz')
def baz(y):
    xs = ', '.join('{x[b]}'.format(x=x) for x in y['xs'])
    return '{y[d]} [{xs}]'.format(y=y, xs=xs).upper()


HIGH_ENV = Edge(None, [
    Edge('x1', subquery_fields(LOW_ENV, 'x', {
        'id': S.this.id,
        'a': S.this.a,
        'foo': foo(S.this, S.this.y),
        'bar': bar(S.this),
        'baz': baz(S.this.y),
        # 'y': S.this.y,
    })),
    Edge('y1', subquery_fields(LOW_ENV, 'y', {
        'id': S.this.id,
        'c': S.this.c,
        'foo': each(S.x, S.this.xs, foo(S.x, S.this)),
        'bar': each(S.x, S.this.xs, bar(S.x)),
        'baz': baz(S.this),
        # 'xs': S.this.xs,
    })),
    # TODO: links reuse
    Link('xs1', None, 'x1', to_x, to_list=True),
    Link('ys2', None, 'y2', to_y, to_list=True),
])
Ejemplo n.º 4
0
def baz(y):
    xs = ", ".join("{x[b]}".format(x=x) for x in y["xs"])
    return "{y[d]} [{xs}]".format(y=y, xs=xs).upper()


HIGH_ENV = Edge(
    None,
    [
        Edge(
            "x1",
            subquery_fields(
                LOW_ENV,
                "x",
                {
                    "id": S.this.id,
                    "a": S.this.a,
                    "foo": foo(S.this, S.this.y),
                    "bar": bar(S.this),
                    "baz": baz(S.this.y),
                    # 'y': S.this.y,
                },
            ),
        ),
        Edge(
            "y1",
            subquery_fields(
                LOW_ENV,
                "y",
                {
                    "id": S.this.id,
                    "c": S.this.c,
                    "foo": each(S.x, S.this.xs, foo(S.x, S.this)),