Example #1
0
def test_pathologic_inputs():
    exprs = [
        "(true)",
        "(1)",
        "(1",
        "(bar spam)",
    ]
    for expr in exprs:
        with pytest.raises(Exception):
            AST.parse(expr).eval()
Example #2
0
def test_with_frame():
    frm = Frame(schema, values)
    env = {"frm": frm, "floor": floor}
    res = AST.parse("(floor frm.timestamp 'Y')").eval(env)
    expect = asarray(["2020", "2020", "2020"], dtype="datetime64[Y]")
    assert all(res == expect)

    res = AST.parse("(floor frm.timestamp 'h')").eval(env)
    expect = asarray(["2020-01-01T11", "2020-01-02T12", "2020-01-03T13"],
                     dtype="datetime64")
    assert all(res == expect)
Example #3
0
def test_only_litterals():
    res = AST.parse("(list 1 2 3)").eval()
    assert res == [1, 2, 3]

    res = AST.parse("1").eval()
    assert res == 1

    res = AST.parse('"spam"').eval()
    assert res == "spam"

    res = AST.parse('(dict "ham" 1 "spam" 2)').eval()
    assert res["ham"] == 1
    assert res["spam"] == 2
Example #4
0
def test_numpy_fun():
    res = AST.parse("(asarray (list 1 2 3))").eval()
    assert all(res == asarray([1, 2, 3]))

    res = AST.parse("(max (list 1 2 3))").eval()
    assert res == 3

    arr = asarray([1, 2, 1, 2])
    # First arg of unique must be an array, the second one is "return_index"
    res = AST.parse("(unique arr true)").eval({"arr": arr})
    assert all(res[0] == [1, 2])
    assert all(res[1] == [0, 1])

    res = AST.parse("(unique arr (kw 'return_counts' true))").eval(
        {"arr": arr})
    assert all(res[0] == [1, 2])
    assert all(res[1] == [2, 2])
Example #5
0
def test_alias():
    res = AST.parse("(as (asarray (list 1 2 3)) 'new_name')").eval()
    arr = res.value
    alias = res.name
    assert all(arr == asarray([1, 2, 3]))
    assert alias == "new_name"

    frm = Frame(schema, values)
    frm = frm.reduce("(as self.timestamp 'ts')")
    assert all(frm["ts"] == asarray(values["timestamp"], "M"))
Example #6
0
def test_some_expr_with_env():
    env = {
        "a": asarray([1, 1]),
        "b": asarray([2, 2]),
        "x_x": 1,
        "y_y": 2,
        "frame": {
            "u": 1,
            "v": 2,
        },
    }
    res = AST.parse("(+ a b)").eval(env)
    expected = asarray([3, 3])
    assert all(res == expected)

    ast = AST.parse("(+ x_x y_y)")
    assert ast.eval(env) == 3

    ast = AST.parse("(+ frame.u frame.v)")
    assert ast.eval(env) == 3
Example #7
0
def test_env():
    res = AST.parse("hello").eval(env={"hello": "world"})
    assert res == "world"
Example #8
0
def test_kw():
    res = AST.parse("(kw 'return_counts' true)").eval()
    assert isinstance(res, KWargs)
Example #9
0
def test_trueish_expr():
    for expr in trueish_expr:
        ast = AST.parse(expr)
        res = ast.eval()
        assert res is True