def test_evaluate_simple_expression(): expr = milisp.parse('(+ 1 2)') env = { '+': lambda e, a: milisp.evaluate(e, a[0]) + milisp.evaluate(e, a[1]) } r = milisp.evaluate(env, expr) assert r == 3
def main_calc_one_vector_with_pure_python(): env = { # setup operations 'vector': pure_vector_op, 'and': pure_and_op, 'in': pure_in_op, 'exec': pure_exec_op, 'set': pure_set_op, } mi.evaluate(env, mi.parse(CODE_INIT)) # run init code to setup constants prog = mi.parse(CODE_CALCULATE) # prepere features calculator env.update({ # one raw data vector 'phoneCountryCode': '+972', 'phoneAreaCode': '3', }) res = mi.evaluate(env, prog) # calculate features assert res == [0.0, 1.0, 0.0] print(res)
def test_evaluate_nontrivial(): expr = milisp.parse('((op "+") x 2)') env = { '+': lambda e, a: milisp.evaluate(e, a[0]) + milisp.evaluate(e, a[1]), 'op': lambda e, a: e[milisp.evaluate(e, a[0])], 'x': 1, } r = milisp.evaluate(env, expr) assert r == 3
def test_evaluate_simple_with_strings(): expr = milisp.parse('(concat x "World")') env = { 'concat': lambda e, a: milisp.evaluate(e, a[0]) + milisp.evaluate(e, a[1]), 'x': 'Hello', } r = milisp.evaluate(env, expr) assert r == 'HelloWorld'
def main_calc_using_numpy(): text = """\ CountryCode AreaCode +972 3 +7 095 +44 020 +44 023 +34 976 """ data = np.genfromtxt(io.StringIO(text), skip_header=1, dtype='|U4', autostrip=True) env = { 'vector': np_vector_op, 'and': np_and_op, 'in': np_in_op, 'exec': pure_exec_op, 'set': np_set_op, } mi.evaluate(env, mi.parse(CODE_INIT)) env.update(zip(('phoneCountryCode', 'phoneAreaCode'), data.T)) res = mi.evaluate( env, mi.parse(CODE_CALCULATE)) # process all data in one run using NumPy np.testing.assert_equal( res, [ # LDN / TLV / MSK [0., 1., 0.], [0., 0., 1.], [1., 0., 0.], [0., 0., 0.], [0., 0., 0.] ]) print(res)
def main_prepare_sql_request(): table_alias = 'log' env = { 'vector': SQLVectorOp('hive."default".events', table_alias), 'and': sql_and_op, 'in': sql_in_op, 'exec': pure_exec_op, 'set': pure_set_op, } mi.evaluate(env, mi.parse(CODE_INIT)) prog = mi.parse(CODE_CALCULATE) env.update({ 'phoneCountryCode': f'{table_alias}.phone_country', 'phoneAreaCode': f'{table_alias}.phone_area', }) res = mi.evaluate(env, prog) assert res == """\ select (log.phone_country in ('+44')) and (log.phone_area in ('020')), (log.phone_country in ('+972')) and (log.phone_area in ('3')), (log.phone_country in ('+7')) and (log.phone_area in ('095', '495')) from hive."default".events as log;""" print(res)
def main(): text = """ (prog # execute all following expressions and return result of last (set "x" 1) # x = 1 (loop "i" 1 N # for i = 1; i <= N; i++ (set "x" (* x i)) # x = x * i ) x # return x ) """ env = { 'prog': prog_op, 'set': set_op, 'loop': loop_op, '*': mul_op, 'N': 5., } expr = mi.parse(text) res = mi.evaluate(env, expr) assert res == 120. print(res)
def main(): text = """ (prog (def "F" "x" (if_gt_one # if x > 1 then F(x-1) else 1 x (* x (call "F" (+ x -1))) 1 )) (call "F" N) ) """ env = { 'prog': prog_op, '*': mul_op, '+': plus_op, 'def': def_op, 'call': call_op, 'if_gt_one': if_gt_one_op, 'N': 5., } expr = mi.parse(text) res = mi.evaluate(env, expr) assert res == 120. print(res)
def test_evaluate_string(): r = milisp.evaluate(None, milisp.parse('"ok"')) assert r == 'ok'
def test_evaluate_number(): r = milisp.evaluate(None, milisp.parse('1')) assert r == 1
def test_evaluate_var(): r = milisp.evaluate({'x': 1}, milisp.parse('x')) assert r == 1
def test_evaluate_none(): r = milisp.evaluate(None, milisp.parse('()')) assert r is None