klongpy 0.6.9__py3-none-any.whl → 0.7.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- klongpy/__init__.py +19 -1
- klongpy/adverbs.py +5 -5
- klongpy/autograd.py +308 -0
- klongpy/backend.py +167 -99
- klongpy/backends/__init__.py +94 -0
- klongpy/backends/base.py +320 -0
- klongpy/backends/numpy_backend.py +122 -0
- klongpy/backends/torch_backend.py +995 -0
- klongpy-0.6.9.data/scripts/kgpy → klongpy/cli.py +65 -88
- klongpy/core.py +228 -108
- klongpy/db/sys_fn_db.py +4 -3
- klongpy/dyads.py +159 -28
- klongpy/interpreter.py +31 -3
- klongpy/monads.py +39 -3
- klongpy/repl.py +21 -3
- klongpy/sys_fn.py +128 -17
- klongpy/sys_fn_autograd.py +290 -0
- klongpy/sys_fn_ipc.py +18 -6
- klongpy/sys_fn_timer.py +13 -3
- klongpy/web/sys_fn_web.py +14 -4
- klongpy-0.7.0.dist-info/METADATA +493 -0
- klongpy-0.7.0.dist-info/RECORD +48 -0
- {klongpy-0.6.9.dist-info → klongpy-0.7.0.dist-info}/WHEEL +1 -1
- klongpy-0.7.0.dist-info/entry_points.txt +2 -0
- {klongpy-0.6.9.dist-info → klongpy-0.7.0.dist-info}/top_level.txt +0 -1
- klongpy-0.6.9.dist-info/METADATA +0 -448
- klongpy-0.6.9.dist-info/RECORD +0 -77
- tests/__init__.py +0 -6
- tests/gen_join_over.py +0 -119
- tests/gen_py_suite.py +0 -77
- tests/gen_test_fn.py +0 -259
- tests/perf_async.py +0 -25
- tests/perf_avg.py +0 -18
- tests/perf_duckdb.py +0 -32
- tests/perf_gen.py +0 -38
- tests/perf_ipc_overhead.py +0 -34
- tests/perf_join.py +0 -53
- tests/perf_load.py +0 -17
- tests/perf_prog.py +0 -18
- tests/perf_serdes.py +0 -52
- tests/perf_sys_fn_db.py +0 -263
- tests/perf_vector.py +0 -40
- tests/test_accel.py +0 -227
- tests/test_df_cache.py +0 -85
- tests/test_eval_monad_list.py +0 -34
- tests/test_examples.py +0 -64
- tests/test_extra_suite.py +0 -382
- tests/test_file_cache.py +0 -185
- tests/test_interop.py +0 -180
- tests/test_kg_asarray.py +0 -94
- tests/test_kgtests.py +0 -65
- tests/test_known_bugs.py +0 -206
- tests/test_prog.py +0 -107
- tests/test_reshape_strings.py +0 -33
- tests/test_suite.py +0 -1480
- tests/test_suite_file.py +0 -153
- tests/test_sys_fn.py +0 -420
- tests/test_sys_fn_db.py +0 -88
- tests/test_sys_fn_ipc.py +0 -587
- tests/test_sys_fn_timer.py +0 -133
- tests/test_sys_fn_web.py +0 -50
- tests/test_util.py +0 -233
- tests/utils.py +0 -126
- {klongpy-0.6.9.dist-info → klongpy-0.7.0.dist-info}/licenses/LICENSE +0 -0
tests/test_suite_file.py
DELETED
|
@@ -1,153 +0,0 @@
|
|
|
1
|
-
import unittest
|
|
2
|
-
from klongpy import KlongInterpreter
|
|
3
|
-
from utils import create_test_klong
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
class FailedUnitTest(Exception):
|
|
7
|
-
pass
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
def die(x,y):
|
|
11
|
-
raise FailedUnitTest(f"expected {x} got {y}")
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
class TestSuiteFile(unittest.TestCase):
|
|
15
|
-
|
|
16
|
-
def test_fail(self):
|
|
17
|
-
klong = create_test_klong()
|
|
18
|
-
klong('t("success";1;1)')
|
|
19
|
-
with self.assertRaises(RuntimeError):
|
|
20
|
-
klong('t("fail";0;1)')
|
|
21
|
-
|
|
22
|
-
def test_call_lambda(self):
|
|
23
|
-
klong = KlongInterpreter()
|
|
24
|
-
klong['die'] = die
|
|
25
|
-
with self.assertRaises(FailedUnitTest):
|
|
26
|
-
klong('foo::{die(x;y)};foo("a";"b")')
|
|
27
|
-
|
|
28
|
-
def test_simple_script(self):
|
|
29
|
-
t = """
|
|
30
|
-
wl::{.w(x);.p("");x}
|
|
31
|
-
wl("hello")
|
|
32
|
-
"""
|
|
33
|
-
klong = KlongInterpreter()
|
|
34
|
-
r = klong(t)
|
|
35
|
-
self.assertEqual(r, 'hello')
|
|
36
|
-
|
|
37
|
-
def test_suite_head(self):
|
|
38
|
-
t = """
|
|
39
|
-
:"Klong test suite; nmh 2015--2020; public domain"
|
|
40
|
-
|
|
41
|
-
.comment("end-of-comment")
|
|
42
|
-
abcdefghijklmnopqrstuvwxyz
|
|
43
|
-
ABCDEFGHIJKLMNOPQRSTUVWXYZ
|
|
44
|
-
0123456789
|
|
45
|
-
~!@#$%^&*()_+-=`[]{}:;"'<,>.
|
|
46
|
-
end-of-comment
|
|
47
|
-
|
|
48
|
-
err::0
|
|
49
|
-
wl::{.w(x);.p("")}
|
|
50
|
-
fail::{err::1;.d("failed: ");.p(x);.d("expected: ");wl(z);.d("got: ");wl(y);die(x;y)}
|
|
51
|
-
t::{:[~y~z;fail(x;y;z);[]]}
|
|
52
|
-
|
|
53
|
-
rnd::{:[x<0;-1;1]*_0.5+#x}
|
|
54
|
-
rndn::{rnd(x*10^y)%10^y}
|
|
55
|
-
|
|
56
|
-
:" Atom "
|
|
57
|
-
t("@:foo" ; @:foo ; 1)
|
|
58
|
-
t("@0" ; @0 ; 1)
|
|
59
|
-
t("@123" ; @123 ; 1)
|
|
60
|
-
t("@-1" ; @-1 ; 1)
|
|
61
|
-
t("@1.23" ; @1.23 ; 1)
|
|
62
|
-
t("@1e5" ; @1e5 ; 1)
|
|
63
|
-
"""
|
|
64
|
-
klong = KlongInterpreter()
|
|
65
|
-
klong['die'] = die
|
|
66
|
-
klong(t)
|
|
67
|
-
|
|
68
|
-
def test_dict_in_situ_mutation(self):
|
|
69
|
-
klong = create_test_klong()
|
|
70
|
-
t = """
|
|
71
|
-
:" Dictionaries: in situ mutation (pre-assign s)"
|
|
72
|
-
s::!100
|
|
73
|
-
D:::{};{D,x,x}'!5
|
|
74
|
-
t("D:::{};{D,x,x}'!5;D" ; s@<s::*'D ; [0 1 2 3 4])
|
|
75
|
-
"""
|
|
76
|
-
klong(t)
|
|
77
|
-
|
|
78
|
-
def test_dyad_contexts(self):
|
|
79
|
-
"""
|
|
80
|
-
Verify that we properly evaluate (KGCall) the dyadic function
|
|
81
|
-
"""
|
|
82
|
-
klong = create_test_klong()
|
|
83
|
-
klong['die'] = die
|
|
84
|
-
t = """
|
|
85
|
-
:" Dyad Contexts "
|
|
86
|
-
f::{x,y}
|
|
87
|
-
g::{x,y,z}
|
|
88
|
-
t("1,2" ; 1,2 ; [1 2])
|
|
89
|
-
t("0,:\[1 2 3]" ; 0,:\[1 2 3] ; [[0 1] [0 2] [0 3]])
|
|
90
|
-
t("0{x,y}[1 2 3]" ; 0{x,y}[1 2 3] ; [0 1 2 3])
|
|
91
|
-
t("0{x,y}:/[1 2 3]" ; 0{x,y}:/[1 2 3] ; [[1 0] [2 0] [3 0]])
|
|
92
|
-
t("0f[1 2 3]" ; 0f[1 2 3] ; [0 1 2 3])
|
|
93
|
-
t("9f:/[1 2 3]" ; 9f:/[1 2 3] ; [[1 9] [2 9] [3 9]])
|
|
94
|
-
t("[1 2 3]g(;0;)[4 5 6]" ; [1 2 3]g(;0;)[4 5 6] ; [1 2 3 0 4 5 6])
|
|
95
|
-
t("[1 2 3]g(;0;)'[4 5 6]" ; [1 2 3]g(;0;)'[4 5 6] ; [[1 0 4] [2 0 5] [3 0 6]])
|
|
96
|
-
"""
|
|
97
|
-
klong(t)
|
|
98
|
-
|
|
99
|
-
def test_module(self):
|
|
100
|
-
t = """
|
|
101
|
-
:" Modules "
|
|
102
|
-
.module(:test)
|
|
103
|
-
a::1
|
|
104
|
-
g::{a}
|
|
105
|
-
f::{g()}
|
|
106
|
-
s::{a::x}
|
|
107
|
-
.module(0)
|
|
108
|
-
|
|
109
|
-
t("a" ; a ; 1)
|
|
110
|
-
t("g()" ; g() ; 1)
|
|
111
|
-
t("s(2)" ; s(2) ; 2)
|
|
112
|
-
t("g()" ; g() ; 2)
|
|
113
|
-
a::0 ; t("g()" ; g() ; 2)
|
|
114
|
-
g::0 ; t("f()" ; f() ; 2)
|
|
115
|
-
"""
|
|
116
|
-
klong = create_test_klong()
|
|
117
|
-
klong(t)
|
|
118
|
-
|
|
119
|
-
def test_file_by_lines(self):
|
|
120
|
-
"""
|
|
121
|
-
Test the suite file line by line using our own t()
|
|
122
|
-
"""
|
|
123
|
-
klong = create_test_klong()
|
|
124
|
-
with open("tests/kgtests/language/test_suite.kg", "r") as f:
|
|
125
|
-
skip_header = True
|
|
126
|
-
i = 0
|
|
127
|
-
for r in f.readlines():
|
|
128
|
-
if skip_header:
|
|
129
|
-
if r.startswith("rnd::"):
|
|
130
|
-
skip_header = False
|
|
131
|
-
else:
|
|
132
|
-
continue
|
|
133
|
-
r = r.strip()
|
|
134
|
-
if len(r) == 0:
|
|
135
|
-
continue
|
|
136
|
-
i += 1
|
|
137
|
-
klong.exec(r)
|
|
138
|
-
print(f"executed {i} lines")
|
|
139
|
-
|
|
140
|
-
def test_file_custom_test(self):
|
|
141
|
-
"""
|
|
142
|
-
Test the suite file in one go using our own t()
|
|
143
|
-
"""
|
|
144
|
-
klong = create_test_klong()
|
|
145
|
-
with open("tests/kgtests/language/test_suite.kg", "r") as f:
|
|
146
|
-
r = f.read()
|
|
147
|
-
i = r.index('rnd::')
|
|
148
|
-
r = r[i:]
|
|
149
|
-
klong(r)
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
if __name__ == '__main__':
|
|
153
|
-
unittest.main(failfast=True, exit=False)
|
tests/test_sys_fn.py
DELETED
|
@@ -1,420 +0,0 @@
|
|
|
1
|
-
import os
|
|
2
|
-
import tempfile
|
|
3
|
-
import unittest
|
|
4
|
-
|
|
5
|
-
from utils import kg_equal
|
|
6
|
-
|
|
7
|
-
from klongpy import KlongInterpreter, KlongException
|
|
8
|
-
from klongpy.sys_fn import *
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
class TestSysFn(unittest.TestCase):
|
|
12
|
-
|
|
13
|
-
def test_autoclose_channel(self):
|
|
14
|
-
with tempfile.TemporaryDirectory() as td:
|
|
15
|
-
fname = os.path.join(td, "data.txt")
|
|
16
|
-
f = open(fname, "w")
|
|
17
|
-
c = KGChannel(f, channel_dir=KGChannelDir.OUTPUT)
|
|
18
|
-
self.assertFalse(f.closed)
|
|
19
|
-
del c
|
|
20
|
-
self.assertTrue(f.closed)
|
|
21
|
-
|
|
22
|
-
def test_channel_integration(self):
|
|
23
|
-
data = '"hello"'
|
|
24
|
-
klong = KlongInterpreter()
|
|
25
|
-
with tempfile.TemporaryDirectory() as td:
|
|
26
|
-
fname = os.path.join(td, "data.txt")
|
|
27
|
-
with eval_sys_output_channel(fname) as f:
|
|
28
|
-
o = eval_sys_to_channel(klong, f)
|
|
29
|
-
self.assertEqual(o, klong['.cout'])
|
|
30
|
-
self.assertEqual(klong['.sys.cout'], f)
|
|
31
|
-
eval_sys_display(klong, data)
|
|
32
|
-
with eval_sys_input_channel(fname) as f:
|
|
33
|
-
o = eval_sys_from_channel(klong, f)
|
|
34
|
-
self.assertEqual(o, klong['.cin'])
|
|
35
|
-
self.assertEqual(klong['.sys.cin'], f)
|
|
36
|
-
r = f.raw.read()
|
|
37
|
-
self.assertEqual(r, data)
|
|
38
|
-
f.raw.seek(0,0)
|
|
39
|
-
r = eval_sys_read_line(klong)
|
|
40
|
-
self.assertEqual(r, data)
|
|
41
|
-
a = eval_sys_read_string(klong, r)
|
|
42
|
-
self.assertEqual(a, klong(data))
|
|
43
|
-
f.raw.seek(0,0)
|
|
44
|
-
r = eval_sys_read(klong)
|
|
45
|
-
self.assertEqual(r, klong(data))
|
|
46
|
-
|
|
47
|
-
def test_eval_sys_append_channel(self):
|
|
48
|
-
with tempfile.TemporaryDirectory() as td:
|
|
49
|
-
fname = os.path.join(td, "data.txt")
|
|
50
|
-
with open(fname, "w") as f:
|
|
51
|
-
f.write("123")
|
|
52
|
-
with eval_sys_append_channel(fname) as f:
|
|
53
|
-
f.raw.write("456")
|
|
54
|
-
with open(fname, "r") as f:
|
|
55
|
-
r = f.read()
|
|
56
|
-
self.assertEqual(r, "123456")
|
|
57
|
-
|
|
58
|
-
def test_eval_sys_close_channel(self):
|
|
59
|
-
with tempfile.TemporaryDirectory() as td:
|
|
60
|
-
fname = os.path.join(td, "data.txt")
|
|
61
|
-
f = eval_sys_append_channel(fname)
|
|
62
|
-
self.assertFalse(f.raw.closed)
|
|
63
|
-
eval_sys_close_channel(f)
|
|
64
|
-
self.assertTrue(f.raw.closed)
|
|
65
|
-
|
|
66
|
-
def test_eval_sys_display(self):
|
|
67
|
-
klong = KlongInterpreter()
|
|
68
|
-
with tempfile.TemporaryDirectory() as td:
|
|
69
|
-
fname = os.path.join(td, "data.txt")
|
|
70
|
-
r = "hello"
|
|
71
|
-
with eval_sys_output_channel(fname) as f:
|
|
72
|
-
eval_sys_to_channel(klong, f)
|
|
73
|
-
self.assertEqual(eval_sys_display(klong, r), r)
|
|
74
|
-
with eval_sys_input_channel(fname) as f:
|
|
75
|
-
r = f.raw.read()
|
|
76
|
-
self.assertEqual(r, "hello")
|
|
77
|
-
|
|
78
|
-
def test_sys_display_result(self):
|
|
79
|
-
klong = KlongInterpreter()
|
|
80
|
-
r = klong(".d(1)")
|
|
81
|
-
self.assertEqual(r,"1")
|
|
82
|
-
|
|
83
|
-
def test_eval_sys_delete_file(self):
|
|
84
|
-
klong = KlongInterpreter()
|
|
85
|
-
with tempfile.TemporaryDirectory() as td:
|
|
86
|
-
fname = os.path.join(td, "data.txt")
|
|
87
|
-
with eval_sys_output_channel(fname) as f:
|
|
88
|
-
eval_sys_to_channel(klong, f)
|
|
89
|
-
eval_sys_display(klong, "hello")
|
|
90
|
-
self.assertTrue(os.path.exists(fname))
|
|
91
|
-
eval_sys_delete_file(fname)
|
|
92
|
-
self.assertFalse(os.path.exists(fname))
|
|
93
|
-
|
|
94
|
-
with self.assertRaises(RuntimeError):
|
|
95
|
-
eval_sys_delete_file(fname)
|
|
96
|
-
|
|
97
|
-
def test_eval_sys_evaluate(self):
|
|
98
|
-
klong = KlongInterpreter()
|
|
99
|
-
r = eval_sys_evaluate(klong, "A::1+1;A+2")
|
|
100
|
-
self.assertEqual(r, 4)
|
|
101
|
-
|
|
102
|
-
def test_eval_sys_from_channel(self):
|
|
103
|
-
data = '1+1'
|
|
104
|
-
klong = KlongInterpreter()
|
|
105
|
-
with tempfile.TemporaryDirectory() as td:
|
|
106
|
-
fname = os.path.join(td, "data.txt")
|
|
107
|
-
with open(fname, "w") as f:
|
|
108
|
-
f.write(data)
|
|
109
|
-
with eval_sys_input_channel(fname) as f:
|
|
110
|
-
self.assertEqual(klong['.sys.cin'], klong['.cin'])
|
|
111
|
-
o = eval_sys_from_channel(klong, f)
|
|
112
|
-
self.assertEqual(o, klong['.cin'])
|
|
113
|
-
self.assertEqual(klong['.sys.cin'], f)
|
|
114
|
-
o = eval_sys_from_channel(klong, 0)
|
|
115
|
-
self.assertEqual(o, f)
|
|
116
|
-
self.assertEqual(klong['.sys.cin'], klong['.cin'])
|
|
117
|
-
|
|
118
|
-
def test_eval_sys_flush(self):
|
|
119
|
-
pass
|
|
120
|
-
|
|
121
|
-
def test_eval_sys_input_channel(self):
|
|
122
|
-
data = '1+1'
|
|
123
|
-
with tempfile.TemporaryDirectory() as td:
|
|
124
|
-
fname = os.path.join(td, "data.txt")
|
|
125
|
-
with open(fname, "w") as f:
|
|
126
|
-
f.write(data)
|
|
127
|
-
with eval_sys_input_channel(fname) as f:
|
|
128
|
-
r = f.raw.read()
|
|
129
|
-
self.assertEqual(r, data)
|
|
130
|
-
with self.assertRaises(FileNotFoundError):
|
|
131
|
-
with eval_sys_input_channel("doesntexist"):
|
|
132
|
-
pass
|
|
133
|
-
|
|
134
|
-
def test_eval_sys_load(self):
|
|
135
|
-
data = '1+1'
|
|
136
|
-
with tempfile.TemporaryDirectory() as td:
|
|
137
|
-
fname = os.path.join(td, "data.txt")
|
|
138
|
-
with open(fname, "w") as f:
|
|
139
|
-
f.write(data)
|
|
140
|
-
klong = KlongInterpreter()
|
|
141
|
-
r = eval_sys_load(klong, fname)
|
|
142
|
-
self.assertEqual(r, 2)
|
|
143
|
-
klong = KlongInterpreter()
|
|
144
|
-
r = klong(f'.l("{fname}")')
|
|
145
|
-
self.assertEqual(r, 2)
|
|
146
|
-
|
|
147
|
-
def test_eval_sys_load_fn(self):
|
|
148
|
-
data = 'fn::{1+1};fn()'
|
|
149
|
-
with tempfile.TemporaryDirectory() as td:
|
|
150
|
-
fname = os.path.join(td, "data.txt")
|
|
151
|
-
with open(fname, "w") as f:
|
|
152
|
-
f.write(data)
|
|
153
|
-
klong = KlongInterpreter()
|
|
154
|
-
r = eval_sys_load(klong, fname)
|
|
155
|
-
self.assertEqual(r, 2)
|
|
156
|
-
self.assertEqual(klong('fn()'), 2)
|
|
157
|
-
klong = KlongInterpreter()
|
|
158
|
-
r = klong(f'.l("{fname}")')
|
|
159
|
-
self.assertEqual(r, 2)
|
|
160
|
-
self.assertEqual(klong('fn()'), 2)
|
|
161
|
-
|
|
162
|
-
def test_eval_sys_more_input(self):
|
|
163
|
-
data = ' ' * 100
|
|
164
|
-
with tempfile.TemporaryDirectory() as td:
|
|
165
|
-
fname = os.path.join(td, "data.txt")
|
|
166
|
-
with open(fname, "w") as f:
|
|
167
|
-
f.write(data)
|
|
168
|
-
with eval_sys_input_channel(fname) as f:
|
|
169
|
-
klong = KlongInterpreter()
|
|
170
|
-
eval_sys_from_channel(klong, f)
|
|
171
|
-
self.assertTrue(eval_sys_more_input(klong))
|
|
172
|
-
eval_sys_read_line(klong)
|
|
173
|
-
self.assertTrue(eval_sys_more_input(klong))
|
|
174
|
-
eval_sys_read_line(klong)
|
|
175
|
-
self.assertFalse(eval_sys_more_input(klong))
|
|
176
|
-
|
|
177
|
-
def test_eval_sys_module(self):
|
|
178
|
-
pass
|
|
179
|
-
|
|
180
|
-
def test_eval_sys_output_channel(self):
|
|
181
|
-
data = '"hello"'
|
|
182
|
-
with tempfile.TemporaryDirectory() as td:
|
|
183
|
-
fname = os.path.join(td, "data.txt")
|
|
184
|
-
with open(fname, "w") as f:
|
|
185
|
-
f.write("123")
|
|
186
|
-
with eval_sys_output_channel(fname) as f:
|
|
187
|
-
f.raw.write(data)
|
|
188
|
-
with open(fname, "r") as f:
|
|
189
|
-
s = f.read()
|
|
190
|
-
self.assertFalse(s.startswith("123"))
|
|
191
|
-
self.assertNotEqual(f.read(), data)
|
|
192
|
-
|
|
193
|
-
def test_eval_sys_process_clock(self):
|
|
194
|
-
pass
|
|
195
|
-
|
|
196
|
-
def test_eval_sys_print(self):
|
|
197
|
-
klong = KlongInterpreter()
|
|
198
|
-
with tempfile.TemporaryDirectory() as td:
|
|
199
|
-
fname = os.path.join(td, "data.txt")
|
|
200
|
-
expected = "hello"
|
|
201
|
-
with eval_sys_output_channel(fname) as f:
|
|
202
|
-
eval_sys_to_channel(klong, f)
|
|
203
|
-
self.assertEqual(eval_sys_print(klong, expected), expected)
|
|
204
|
-
with eval_sys_input_channel(fname) as f:
|
|
205
|
-
r = f.raw.read()
|
|
206
|
-
self.assertEqual(r, f"{expected}\n")
|
|
207
|
-
|
|
208
|
-
def test_sys_print_result(self):
|
|
209
|
-
klong = KlongInterpreter()
|
|
210
|
-
r = klong(".p(1)")
|
|
211
|
-
self.assertEqual(r,"1")
|
|
212
|
-
|
|
213
|
-
def test_sys_python_load_module(self):
|
|
214
|
-
klong = KlongInterpreter()
|
|
215
|
-
r = klong(f'.py("math")')
|
|
216
|
-
self.assertEqual(r,1)
|
|
217
|
-
r = klong('sqrt(64)')
|
|
218
|
-
self.assertEqual(r,8)
|
|
219
|
-
r = klong('fsum(1+!10)')
|
|
220
|
-
self.assertEqual(r,55)
|
|
221
|
-
|
|
222
|
-
def test_sys_python_load_sub_module(self):
|
|
223
|
-
klong = KlongInterpreter()
|
|
224
|
-
with self.assertRaises(KlongException):
|
|
225
|
-
r = klong('sqrt(64)')
|
|
226
|
-
r = klong(f'.pyf("math";"sqrt")')
|
|
227
|
-
self.assertEqual(r,1)
|
|
228
|
-
r = klong('sqrt(64)')
|
|
229
|
-
self.assertEqual(r,8)
|
|
230
|
-
with self.assertRaises(KlongException):
|
|
231
|
-
r = klong('fsum(1+!10)')
|
|
232
|
-
r = klong(f'.pyf("math";"fsum")')
|
|
233
|
-
self.assertEqual(r,1)
|
|
234
|
-
r = klong('fsum(1+!10)')
|
|
235
|
-
self.assertEqual(r,55)
|
|
236
|
-
|
|
237
|
-
def test_sys_python_load_multiple_sub_modules(self):
|
|
238
|
-
klong = KlongInterpreter()
|
|
239
|
-
with self.assertRaises(KlongException):
|
|
240
|
-
r = klong('sqrt(64)')
|
|
241
|
-
with self.assertRaises(KlongException):
|
|
242
|
-
r = klong('fsum(1+!10)')
|
|
243
|
-
r = klong(f'.pyf("math";["sqrt" "fsum"])')
|
|
244
|
-
self.assertEqual(r,1)
|
|
245
|
-
r = klong('sqrt(64)')
|
|
246
|
-
self.assertEqual(r,8)
|
|
247
|
-
r = klong('fsum(1+!10)')
|
|
248
|
-
self.assertEqual(r,55)
|
|
249
|
-
|
|
250
|
-
def test_sys_python_load_custom_module(self):
|
|
251
|
-
tests_dir = os.path.dirname(os.path.abspath(__file__))
|
|
252
|
-
plugins_dir = os.path.join(tests_dir, "plugins")
|
|
253
|
-
tests = []
|
|
254
|
-
tests.append(os.path.join("tests", os.path.join("plugins", "greetings")))
|
|
255
|
-
tests.append(os.path.join(plugins_dir, "greetings"))
|
|
256
|
-
tests.append(os.path.join(plugins_dir, "greetings/__init__.py"))
|
|
257
|
-
tests.append(os.path.join(plugins_dir, "greetings/hello_world.py"))
|
|
258
|
-
tests.append("greetings")
|
|
259
|
-
try:
|
|
260
|
-
sys.path.append(plugins_dir)
|
|
261
|
-
for fpath in tests:
|
|
262
|
-
klong = KlongInterpreter()
|
|
263
|
-
self.assertEqual(klong(f'.py("{fpath}")'),1)
|
|
264
|
-
|
|
265
|
-
self.assertEqual(klong('nilad()'),"hello, world!")
|
|
266
|
-
self.assertEqual(klong('monad(1)'),"1+1")
|
|
267
|
-
self.assertEqual(klong('dyad(1;2)'),"1*2+1")
|
|
268
|
-
self.assertEqual(klong('triad(1;2;3)'),"1*2+3+1")
|
|
269
|
-
self.assertEqual(klong('knilad()'),4)
|
|
270
|
-
self.assertEqual(klong('kmonad(1)'),2)
|
|
271
|
-
self.assertEqual(klong('kdyad(1;2)'),1*2+1)
|
|
272
|
-
self.assertEqual(klong('ktriad(1;2;3)'),1*2+3+1)
|
|
273
|
-
|
|
274
|
-
if fpath.endswith("hello_world.py"):
|
|
275
|
-
r = klong['not_exported']
|
|
276
|
-
self.assertTrue(r is not None)
|
|
277
|
-
else:
|
|
278
|
-
with self.assertRaises(KeyError):
|
|
279
|
-
klong['not_exported']
|
|
280
|
-
finally:
|
|
281
|
-
sys.path.pop()
|
|
282
|
-
|
|
283
|
-
def test_sys_python_load_custom_export(self):
|
|
284
|
-
tests_dir = os.path.dirname(os.path.abspath(__file__))
|
|
285
|
-
plugins_dir = os.path.join(tests_dir, "plugins")
|
|
286
|
-
fpath = os.path.join(plugins_dir, "custom_export")
|
|
287
|
-
klong = KlongInterpreter()
|
|
288
|
-
self.assertEqual(klong(f'.py("{fpath}")'),1)
|
|
289
|
-
self.assertEqual(klong('.hello()'),"hello, world!")
|
|
290
|
-
|
|
291
|
-
def test_eval_sys_random_number(self):
|
|
292
|
-
r = eval_sys_random_number()
|
|
293
|
-
r2 = eval_sys_random_number()
|
|
294
|
-
i = 0
|
|
295
|
-
while r == r2 and i < 3:
|
|
296
|
-
i += 1
|
|
297
|
-
r2 = eval_sys_random_number()
|
|
298
|
-
self.assertNotEqual(r, r2)
|
|
299
|
-
|
|
300
|
-
def test_eval_sys_read(self):
|
|
301
|
-
# TODO: test all variants of kg_read
|
|
302
|
-
data = '[1 2 3 4];"hello"'
|
|
303
|
-
klong = KlongInterpreter()
|
|
304
|
-
with tempfile.TemporaryDirectory() as td:
|
|
305
|
-
fname = os.path.join(td, "data.txt")
|
|
306
|
-
with open(fname, "w") as f:
|
|
307
|
-
f.write(data)
|
|
308
|
-
with eval_sys_input_channel(fname) as f:
|
|
309
|
-
eval_sys_from_channel(klong, f)
|
|
310
|
-
r = eval_sys_read(klong)
|
|
311
|
-
self.assertTrue(kg_equal(r, [1,2,3,4]))
|
|
312
|
-
c = f.raw.read(1)
|
|
313
|
-
self.assertEqual(c,';')
|
|
314
|
-
|
|
315
|
-
def test_eval_sys_read_line(self):
|
|
316
|
-
data = """line 1
|
|
317
|
-
line 2
|
|
318
|
-
line 3
|
|
319
|
-
"""
|
|
320
|
-
klong = KlongInterpreter()
|
|
321
|
-
with tempfile.TemporaryDirectory() as td:
|
|
322
|
-
fname = os.path.join(td, "data.txt")
|
|
323
|
-
with open(fname, "w") as f:
|
|
324
|
-
f.write(data)
|
|
325
|
-
with eval_sys_input_channel(fname) as f:
|
|
326
|
-
eval_sys_from_channel(klong, f)
|
|
327
|
-
r = eval_sys_read_line(klong)
|
|
328
|
-
self.assertEqual(r, "line 1")
|
|
329
|
-
r = eval_sys_read_line(klong)
|
|
330
|
-
self.assertEqual(r.strip(), "line 2")
|
|
331
|
-
r = eval_sys_read_line(klong)
|
|
332
|
-
self.assertEqual(r.strip(), "line 3")
|
|
333
|
-
r = eval_sys_read_line(klong)
|
|
334
|
-
self.assertEqual(len(r), 0)
|
|
335
|
-
|
|
336
|
-
def test_eval_sys_read_string(self):
|
|
337
|
-
# TODO:
|
|
338
|
-
data = '[1 2 3 4]'
|
|
339
|
-
klong = KlongInterpreter()
|
|
340
|
-
self.assertTrue(kg_equal(eval_sys_read_string(klong, data), [1,2,3,4]))
|
|
341
|
-
|
|
342
|
-
def test_eval_sys_system(self):
|
|
343
|
-
pass
|
|
344
|
-
|
|
345
|
-
def test_eval_sys_to_channel(self):
|
|
346
|
-
klong = KlongInterpreter()
|
|
347
|
-
with tempfile.TemporaryDirectory() as td:
|
|
348
|
-
fname = os.path.join(td, "data.txt")
|
|
349
|
-
with eval_sys_output_channel(fname) as f:
|
|
350
|
-
self.assertEqual(klong['.sys.cout'], klong['.cout'])
|
|
351
|
-
o = eval_sys_to_channel(klong, f)
|
|
352
|
-
self.assertEqual(o, klong['.cout'])
|
|
353
|
-
self.assertEqual(f, klong['.sys.cout'])
|
|
354
|
-
o = eval_sys_to_channel(klong, 0)
|
|
355
|
-
self.assertEqual(o, f)
|
|
356
|
-
self.assertEqual(klong['.sys.cout'], klong['.cout'])
|
|
357
|
-
|
|
358
|
-
def test_eval_sys_write(self):
|
|
359
|
-
# TODO: test all variants of kg_write
|
|
360
|
-
klong = KlongInterpreter()
|
|
361
|
-
with tempfile.TemporaryDirectory() as td:
|
|
362
|
-
fname = os.path.join(td, "data.txt")
|
|
363
|
-
expected = "hello"
|
|
364
|
-
with eval_sys_output_channel(fname) as f:
|
|
365
|
-
eval_sys_to_channel(klong, f)
|
|
366
|
-
self.assertEqual(eval_sys_write(klong, expected), expected)
|
|
367
|
-
with eval_sys_input_channel(fname) as f:
|
|
368
|
-
r = f.raw.read()
|
|
369
|
-
self.assertEqual(r, f'"{expected}"')
|
|
370
|
-
|
|
371
|
-
def test_sys_write_result(self):
|
|
372
|
-
klong = KlongInterpreter()
|
|
373
|
-
r = klong(".w(1)")
|
|
374
|
-
self.assertEqual(r,1)
|
|
375
|
-
|
|
376
|
-
def test_sys_write_dict(self):
|
|
377
|
-
klong = KlongInterpreter()
|
|
378
|
-
r = klong(".w(:{[1 2]})")
|
|
379
|
-
self.assertEqual(r,{1:2})
|
|
380
|
-
|
|
381
|
-
def test_eval_sys_exit(self):
|
|
382
|
-
pass
|
|
383
|
-
|
|
384
|
-
def test_simple_io(self):
|
|
385
|
-
t = """
|
|
386
|
-
foo::{.tc(T::.oc(x));.p("hello!");.cc(T)}
|
|
387
|
-
bar::{.fc(T::.ic(x));R::.rl();.cc(T);R}
|
|
388
|
-
"""
|
|
389
|
-
klong = KlongInterpreter()
|
|
390
|
-
klong(t)
|
|
391
|
-
with tempfile.TemporaryDirectory() as td:
|
|
392
|
-
fname = os.path.join(td, "data.txt")
|
|
393
|
-
klong['fname'] = fname
|
|
394
|
-
klong('foo(fname)')
|
|
395
|
-
with open(fname, 'r') as f:
|
|
396
|
-
r = f.read()
|
|
397
|
-
self.assertEqual(r, 'hello!\n')
|
|
398
|
-
r = klong('bar(fname)')
|
|
399
|
-
self.assertEqual(r, 'hello!')
|
|
400
|
-
|
|
401
|
-
def test_simple_cat(self):
|
|
402
|
-
t = """
|
|
403
|
-
cat::{.mi{.p(x);.rl()}:~.rl()}
|
|
404
|
-
type::{.fc(.ic(x));cat()}
|
|
405
|
-
copy::{[of];.tc(of::.oc(y));type(x);.cc(of)}
|
|
406
|
-
"""
|
|
407
|
-
klong = KlongInterpreter()
|
|
408
|
-
klong(t)
|
|
409
|
-
with tempfile.TemporaryDirectory() as td:
|
|
410
|
-
fname_src = os.path.join(td, "source.txt")
|
|
411
|
-
fname_dest = os.path.join(td, "dest.txt")
|
|
412
|
-
klong['src'] = fname_src
|
|
413
|
-
klong['dest'] = fname_dest
|
|
414
|
-
data = "this is a test"
|
|
415
|
-
with open(fname_src, 'w') as f:
|
|
416
|
-
f.write(data)
|
|
417
|
-
klong('copy(src;dest)')
|
|
418
|
-
with open(fname_dest, 'r') as f:
|
|
419
|
-
r = f.read()
|
|
420
|
-
self.assertEqual(r, "this is a test\n")
|
tests/test_sys_fn_db.py
DELETED
|
@@ -1,88 +0,0 @@
|
|
|
1
|
-
import unittest
|
|
2
|
-
|
|
3
|
-
import numpy as np
|
|
4
|
-
import pandas as pd
|
|
5
|
-
|
|
6
|
-
from klongpy import KlongInterpreter
|
|
7
|
-
from klongpy.core import kg_equal
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
class TestLocalScopeBehavior(unittest.TestCase):
|
|
11
|
-
"""
|
|
12
|
-
KlongPy uses the DuckDb's ability to scope in dataframes into table space.
|
|
13
|
-
"""
|
|
14
|
-
|
|
15
|
-
@staticmethod
|
|
16
|
-
def add_to_local_scope(d):
|
|
17
|
-
for k,v in d.items():
|
|
18
|
-
locals()[k] = v
|
|
19
|
-
|
|
20
|
-
def test_locals_scope_behavior(self):
|
|
21
|
-
d = {'hello': "world"}
|
|
22
|
-
TestLocalScopeBehavior.add_to_local_scope(d)
|
|
23
|
-
self.assertTrue(locals().get("hello") is None)
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
class TestTablePrint(unittest.TestCase):
|
|
27
|
-
|
|
28
|
-
def test_table_print(self):
|
|
29
|
-
s = """
|
|
30
|
-
.py("klongpy.db")
|
|
31
|
-
T::.table(,"a",,[1 2 3])
|
|
32
|
-
.p(T)
|
|
33
|
-
"""
|
|
34
|
-
klong = KlongInterpreter()
|
|
35
|
-
r = klong(s)
|
|
36
|
-
self.assertEqual(r, "a\n1\n2\n3")
|
|
37
|
-
|
|
38
|
-
def test_long_table_print(self):
|
|
39
|
-
s = """
|
|
40
|
-
.py("klongpy.db")
|
|
41
|
-
T::.table(,"a",,!100)
|
|
42
|
-
.p(T)
|
|
43
|
-
"""
|
|
44
|
-
klong = KlongInterpreter()
|
|
45
|
-
r = klong(s)
|
|
46
|
-
seq = "".join([str(x)+"\n" for x in range(10)])
|
|
47
|
-
self.assertEqual(r, f"""a\n{seq}...\nrows=100\n""")
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
class TestTableDataFrame(unittest.TestCase):
|
|
51
|
-
|
|
52
|
-
def test_table_from_empty_df(self):
|
|
53
|
-
df = pd.DataFrame()
|
|
54
|
-
klong = KlongInterpreter()
|
|
55
|
-
klong['df'] = df
|
|
56
|
-
klong('.py("klongpy.db")')
|
|
57
|
-
klong('T::.table(df)')
|
|
58
|
-
r = klong('#T')
|
|
59
|
-
self.assertEqual(r, 0)
|
|
60
|
-
|
|
61
|
-
def test_table_from_a_df_with_single_row_and_two_columns(self):
|
|
62
|
-
data = {'col1': [1], 'col2': [3]}
|
|
63
|
-
df = pd.DataFrame(data)
|
|
64
|
-
klong = KlongInterpreter()
|
|
65
|
-
klong['df'] = df
|
|
66
|
-
klong('.py("klongpy.db")')
|
|
67
|
-
klong('T::.table(df)')
|
|
68
|
-
r = klong('#T')
|
|
69
|
-
self.assertEqual(r, 1)
|
|
70
|
-
r = klong('.schema(T)')
|
|
71
|
-
self.assertTrue(kg_equal(r, ["col1", "col2"]))
|
|
72
|
-
|
|
73
|
-
def test_table_from_a_df_with_one_column_many_rows(self):
|
|
74
|
-
data = {'col1': np.arange(10)}
|
|
75
|
-
df = pd.DataFrame(data)
|
|
76
|
-
klong = KlongInterpreter()
|
|
77
|
-
klong['df'] = df
|
|
78
|
-
klong('.py("klongpy.db")')
|
|
79
|
-
klong('T::.table(df)')
|
|
80
|
-
r = klong('#T')
|
|
81
|
-
self.assertEqual(r, 10)
|
|
82
|
-
r = klong('.schema(T)')
|
|
83
|
-
self.assertTrue(kg_equal(r, ["col1"]))
|
|
84
|
-
r = klong('T?"col1"')
|
|
85
|
-
self.assertTrue(kg_equal(r, data['col1']))
|
|
86
|
-
# TODO: @ should work the same as a dictionary
|
|
87
|
-
# r = klong('T@"col1"')
|
|
88
|
-
# self.assertTrue(kg_equal(r, data['col1']))
|