Esempio n. 1
0
def test_filter():
    assert list(c.naive([1, 2, 3]).filter(c.this().gt(2)).execute(None)) == [3]
    assert c.filter(c.this().gt(1), cast=list).execute([1, 2, 3]) == [2, 3]
    assert c.filter(c.this().gt(1), cast=tuple).execute([1, 2, 3]) == (2, 3)
    assert c.filter(c.this().gt(1), cast=set).execute([1, 2, 3]) == {2, 3}
    assert c.filter(c.this().gt(1),
                    cast=lambda x: list(x)).execute([1, 2, 3]) == [2, 3]
    assert c.list_comp(c.this()).filter(c.this().gt(1)).execute([1, 2, 3]) == [
        2,
        3,
    ]
    assert c.this().filter(c.this().gt(1), cast=list).execute([1, 2, 3]) == [
        2,
        3,
    ]
Esempio n. 2
0
def test_complex_labeling():
    conv1 = (c.this().add_label("input").pipe(
        c.filter(c.this() % 3 == 0),
        label_input={
            "input_type": c.call_func(type, c.this())
        },
    ).pipe(
        c.list_comp(c.this().as_type(str)),
        label_output={
            "list_length": c.call_func(len, c.this()),
            "separator": c.if_(c.label("list_length") > 10, ",", ";"),
        },
    ).pipe({
        "result": c.label("separator").call_method("join", c.this()),
        "input_type": c.label("input_type"),
        "input_data": c.label("input"),
    }).gen_converter(debug=False))
    assert conv1(range(30)) == {
        "result": "0;3;6;9;12;15;18;21;24;27",
        "input_type": range,
        "input_data": range(0, 30),
    }
    assert conv1(range(40)) == {
        "result": "0,3,6,9,12,15,18,21,24,27,30,33,36,39",
        "input_type": range,
        "input_data": range(0, 40),
    }
Esempio n. 3
0
def test_filter():
    assert list(c.naive([1, 2, 3]).filter(c.this.gt(2)).execute(None)) == [3]
    assert c.filter(c.this.gt(1), cast=list).execute([1, 2, 3]) == [2, 3]
    assert c.filter(c.this.gt(1), cast=tuple).execute([1, 2, 3]) == (2, 3)
    assert c.filter(c.this.gt(1), cast=set).execute([1, 2, 3]) == {2, 3}
    assert c.filter(c.this.gt(1),
                    cast=lambda x: list(x)).execute([1, 2, 3]) == [2, 3]
    assert c.list_comp(c.this).filter(c.this.gt(1)).execute([1, 2, 3],
                                                            debug=False) == [
                                                                2,
                                                                3,
                                                            ]
    assert c.this.filter(c.this.gt(1), cast=list).execute([1, 2, 3],
                                                          debug=False) == [
                                                              2,
                                                              3,
                                                          ]
    assert c.list_comp(c.this).filter(
        c.this > 1, cast=lambda x: list(x)).execute(range(4)) == [2, 3]
Esempio n. 4
0
def test_group_by_with_pipes():
    # fmt: off
    input_data = [
        {
            "name": "John",
            "started_at": date(2020, 1, 1),
            "stopped_at": None,
            "product": "A"
        },
        {
            "name": "John",
            "started_at": date(2020, 1, 1),
            "stopped_at": date(2020, 1, 2),
            "product": "B"
        },
        {
            "name": "John",
            "started_at": date(2020, 1, 1),
            "stopped_at": None,
            "product": "C"
        },
        {
            "name": "Nick",
            "started_at": date(2020, 1, 1),
            "stopped_at": None,
            "product": "D"
        },
        {
            "name": "Nick",
            "started_at": date(2020, 2, 1),
            "stopped_at": None,
            "product": "D"
        },
        {
            "name": "Nick",
            "started_at": date(2020, 2, 1),
            "stopped_at": None,
            "product": "E"
        },
    ]
    # fmt: on
    output = (c.group_by(
        c.item("name"),
        c.item("started_at"),
    ).aggregate({
        "name":
        c.item("name"),
        "started_at":
        c.item("started_at"),
        "products":
        c.ReduceFuncs.ArrayDistinct(
            c.if_(
                c.item("stopped_at").is_(None),
                c.item("product"),
                None,
            ), ).pipe(c.filter(c.this())).pipe(
                c.call_func(sorted, c.this()).pipe(
                    c(", ").call_method("join", c.this()))).pipe(c.this()),
    }).execute(input_data))
    # fmt: off
    assert output == [{
        'name': 'John',
        'products': 'A, C',
        'started_at': date(2020, 1, 1)
    }, {
        'name': 'Nick',
        'products': 'D',
        'started_at': date(2020, 1, 1)
    }, {
        'name': 'Nick',
        'products': 'D, E',
        'started_at': date(2020, 2, 1)
    }]
    # fmt: on

    reducer = c.ReduceFuncs.Array(c.this(), default=list)
    output = (c.group_by(
        c.this()["name"],
        c.this()["started_at"],
    ).aggregate({
        "name": c.this()["name"],
        "started_at": c.this()["started_at"],
        "products": c.this()["product"].pipe(reducer)[:3],
    }).execute(input_data))
    assert output == [
        {
            "name": "John",
            "products": ["A", "B", "C"],
            "started_at": date(2020, 1, 1),
        },
        {
            "name": "Nick",
            "products": ["D"],
            "started_at": date(2020, 1, 1),
        },
        {
            "name": "Nick",
            "products": ["D", "E"],
            "started_at": date(2020, 2, 1),
        },
    ]