Пример #1
0
def test_pipe_filter_sort():
    assert (c.this.as_type(list).pipe(c.iter(c.this + 1)).filter(
        c.this > 3).sort(key=lambda x: x,
                         reverse=True).execute(range(7),
                                               debug=False)) == [7, 6, 5, 4]

    assert c.this.sort().execute([3, 1, 2]) == [1, 2, 3]
Пример #2
0
def test_comprehension_where():
    assert (c.generator_comp(
        c.this.neg(),
        where=c.this > 6).as_type(list).filter(c.this > -9).execute(
            range(10), debug=False)) == [-7, -8]
    assert (c.this.iter(c.this.neg(), where=c.this > 6).as_type(list).filter(
        c.this > -9).execute(range(10), debug=False)) == [-7, -8]
    assert (c.iter(c.this.neg(),
                   where=c.this > 6).as_type(list).filter(c.this > -9).execute(
                       range(10), debug=False)) == [-7, -8]

    assert c.iter(c.this, where=c.and_(default=True)).as_type(list).execute(
        range(3)) == [0, 1, 2]
    assert c.iter(c.this, where=True).as_type(list).execute(range(3)) == [
        0,
        1,
        2,
    ]
    assert (c.iter(c.this, where=c.and_(default=False)).as_type(list).execute(
        range(3)) == [])
Пример #3
0
def test_base_zip():
    meta = {1: "a", 2: "b", 3: "c"}
    input_data = {"items": [1, 2, 3], "meta": meta}
    converter = (c.zip(
        c.item("items"),
        c.repeat(c.item("meta")),
    ).as_type(list).gen_converter(debug=False))
    assert converter(input_data) == [
        (1, meta),
        (2, meta),
        (3, meta),
    ]
    converter = (c.zip(
        item=c.item("items"),
        meta=c.repeat(c.item("meta")),
    ).as_type(list).gen_converter(debug=False))
    assert converter(input_data) == [
        {
            "item": 1,
            "meta": meta
        },
        {
            "item": 2,
            "meta": meta
        },
        {
            "item": 3,
            "meta": meta
        },
    ]

    input_data = [
        ([1, 2, 3], {
            1: "a",
            2: "b",
            3: "c"
        }),
        ([4, 5, 6], {
            4: "a",
            5: "b",
            6: "c"
        }),
    ]
    converter = (c.iter(c.zip(c.item(0), c.repeat(c.item(1)))).flatten().iter(
        c.item(1, c.item(0))).pipe(c.call_func(
            ",".join, c.this)).gen_converter(debug=False))
    assert converter(input_data) == "a,b,c,a,b,c"

    with pytest.raises(ValueError):
        c.zip(1, 2, a=1)
Пример #4
0
def test_is_independent():
    assert c(0).is_independent()
    assert c(int).is_independent()
    assert c(int).call().is_independent()
    assert c.label("a").is_independent()
    assert c.inline_expr("{}()").pass_args(int).is_independent()
    assert c.escaped_string("int()").is_independent()
    assert c({"a": c.input_arg("key")}).is_independent()
    assert not c.iter({"a": 1}).is_independent()
    assert not c.this.is_independent()
    assert not c({"a": 1}).item("a").is_independent()
    assert not c({"a": 1}).item(c.item("a")).is_independent()
    assert not c.inline_expr("{}()").pass_args(c.this).is_independent()
    assert not c.aggregate({"a": 1}).is_independent()
    assert not c.this.add_label("a").is_independent()
    assert not c(int).call(c.item(0)).is_independent()
Пример #5
0
def test_iter_mut_method():
    assert c.iter(c.item(0)).as_type(list).execute([[1], [2]]) == [1, 2]
    assert c.iter_mut(c.Mut.custom(c.this.call_method("append", 7))).as_type(
        list
    ).execute([[1], [2]]) == [[1, 7], [2, 7]]
    result = (
        c.this.iter({"a": c.this})
        .iter_mut(
            c.Mut.set_item("b", c.item("a") + 1),
            c.Mut.set_item("c", c.item("a") + 2),
        )
        .iter_mut(
            c.Mut.set_item("d", c.item("a") + 3),
        )
        .as_type(list)
        .execute([1, 2, 3], debug=False)
    )
    assert result == [
        {"a": 1, "b": 2, "c": 3, "d": 4},
        {"a": 2, "b": 3, "c": 4, "d": 5},
        {"a": 3, "b": 4, "c": 5, "d": 6},
    ]

    result = (
        c.group_by(c.item(0))
        .aggregate(
            c(
                [
                    {c.item(0): c.item(1).pipe(c.ReduceFuncs.Max(c.this))},
                    {c.item(1).pipe(c.ReduceFuncs.Max(c.this)): c.item(0)},
                ]
            )
            .iter_mut(
                c.Mut.set_item(
                    "x",
                    c.call_func(sum, c.this.call_method("values"))
                    + c.input_arg("base"),
                )
            )
            .as_type(tuple)
        )
        .execute([(0, 1), (0, 2), (1, 7)], base=100, debug=False)
    )
    assert result == [
        ({0: 2, "x": 102}, {2: 0, "x": 100}),
        ({1: 7, "x": 107}, {7: 1, "x": 101}),
    ]
Пример #6
0
def test_comprehension_where():
    assert (
        c.generator_comp(c.this().neg(), where=c.this() > 6)
        .as_type(list)
        .filter(c.this() > -9)
        .execute(range(10), debug=False)
    ) == [-7, -8]
    assert (
        c.this()
        .iter(c.this().neg(), where=c.this() > 6)
        .as_type(list)
        .filter(c.this() > -9)
        .execute(range(10), debug=False)
    ) == [-7, -8]
    assert (
        c.iter(c.this().neg(), where=c.this() > 6)
        .as_type(list)
        .filter(c.this() > -9)
        .execute(range(10), debug=False)
    ) == [-7, -8]