Ejemplo n.º 1
0
def test_quote():
    literals = [[1, 2, 3], (add, 1, 2), [1, [2, 3]], (add, 1, (add, 2, 3)), {
        "x": "x"
    }]

    for le in literals:
        assert get({"x": quote(le)}, "x") == le
Ejemplo n.º 2
0
def test_local_parents_of_reduction(abcde):
    """

            c1
            |
        b1  c2
        |  /|
    a1  b2  c3
    |  /|
    a2  b3
    |
    a3

    Prefer to finish a1 stack before proceeding to b2
    """
    a, b, c, d, e = abcde
    a1, a2, a3 = [a + i for i in "123"]
    b1, b2, b3 = [b + i for i in "123"]
    c1, c2, c3 = [c + i for i in "123"]

    expected = [a3, a2, a1, b3, b2, b1, c3, c2, c1]

    log = []

    def f(x):
        def _(*args):
            log.append(x)

        return _

    dsk = {
        a3: (f(a3), ),
        a2: (f(a2), a3),
        a1: (f(a1), a2),
        b3: (f(b3), ),
        b2: (f(b2), b3, a2),
        b1: (f(b1), b2),
        c3: (f(c3), ),
        c2: (f(c2), c3, b2),
        c1: (f(c1), c2),
    }

    order(dsk)
    get(dsk, [a1, b1, c1])  # trigger computation

    assert log == expected
Ejemplo n.º 3
0
 def endpoint_fn(body: request_model):
     session = body.session if body.session else str(uuid.uuid4())
     _res = get(
         dsk_composition.dsk,
         dsk_composition.get_keys,
         cache=body.payload.dict(),
         sortkeys=dsk_composition.sortkeys,
     )
     return {
         "result": dict(zip(dsk_composition.ep_dsk_output_keys, _res)),
         "session": session,
     }
Ejemplo n.º 4
0
def test_order_cycle():
    with pytest.raises(RuntimeError, match="Cycle detected"):
        get({"a": (f, "a")}, "a")  # we encounter this in `get`
    with pytest.raises(RuntimeError, match="Cycle detected"):
        order({"a": (f, "a")})  # trivial self-loop
    with pytest.raises(RuntimeError, match="Cycle detected"):
        order({("a", 0): (f, ("a", 0))})  # non-string
    with pytest.raises(RuntimeError, match="Cycle detected"):
        order({
            "a": (f, "b"),
            "b": (f, "c"),
            "c": (f, "a")
        })  # non-trivial loop
    with pytest.raises(RuntimeError, match="Cycle detected"):
        order({"a": (f, "b"), "b": (f, "c"), "c": (f, "a", "d"), "d": 1})
    with pytest.raises(RuntimeError, match="Cycle detected"):
        order({
            "a": (f, "b"),
            "b": (f, "c"),
            "c": (f, "a", "d"),
            "d": (f, "b")
        })
def test_SubgraphCallable():
    non_hashable = [1, 2, 3]

    dsk = {
        "a": (apply, add, ["in1", 2]),
        "b": (
            apply,
            partial_by_order,
            ["in2"],
            {
                "function": func_with_kwargs,
                "other": [(1, 20)],
                "c": 4
            },
        ),
        "c": (
            apply,
            partial_by_order,
            ["in2", "in1"],
            {
                "function": func_with_kwargs,
                "other": [(1, 20)]
            },
        ),
        "d": (inc, "a"),
        "e": (add, "c", "d"),
        "f": ["a", 2, "b", (add, "b", (sum, non_hashable))],
        "h": (add, (sum, "f"), (sum, ["a", "b"])),
    }

    f = SubgraphCallable(dsk, "h", ["in1", "in2"], name="test")
    assert f.name == "test"
    assert repr(f) == "test"

    f2 = SubgraphCallable(dsk, "h", ["in1", "in2"], name="test")
    assert f == f2

    f3 = SubgraphCallable(dsk, "g", ["in1", "in2"], name="test")
    assert f != f3

    assert dict(f=None)
    assert hash(SubgraphCallable(None, None, [None]))
    assert hash(f3) != hash(f2)

    dsk2 = dsk.copy()
    dsk2.update({"in1": 1, "in2": 2})
    assert f(1, 2) == get(cull(dsk2, ["h"])[0], ["h"])[0]
    assert f(1, 2) == f(1, 2)

    f2 = pickle.loads(pickle.dumps(f))
    assert f2(1, 2) == f(1, 2)
Ejemplo n.º 6
0
def test_run_smaller_sections(abcde):
    r"""
            aa
           / |
      b   d  bb dd
     / \ /|  | /
    a   c e  cc

    Prefer to run acb first because then we can get that out of the way
    """
    a, b, c, d, e = abcde
    aa, bb, cc, dd = [x * 2 for x in [a, b, c, d]]

    expected = [a, c, b, e, d, cc, bb, aa, dd]

    log = []

    def f(x):
        def _(*args):
            log.append(x)

        return _

    dsk = {
        a: (f(a), ),
        c: (f(c), ),
        e: (f(e), ),
        cc: (f(cc), ),
        b: (f(b), a, c),
        d: (f(d), c, e),
        bb: (f(bb), cc),
        aa: (f(aa), d, bb),
        dd: (f(dd), cc),
    }

    get(dsk, [aa, b, dd])

    assert log == expected
Ejemplo n.º 7
0
 def __call__(self, *args):
     if not len(args) == len(self.inkeys):
         raise ValueError("Expected %d args, got %d" %
                          (len(self.inkeys), len(args)))
     return get(self.dsk, self.outkey, dict(zip(self.inkeys, args)))