metafunctions 1.1.2__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.
@@ -0,0 +1,228 @@
1
+ import operator
2
+ import platform
3
+ import unittest
4
+ from unittest import mock
5
+
6
+ from metafunctions import operators
7
+ from metafunctions import util
8
+ from metafunctions.api import bind_call_state
9
+ from metafunctions.api import concurrent
10
+ from metafunctions.api import locate_error
11
+ from metafunctions.api import mmap
12
+ from metafunctions.api import node
13
+ from metafunctions.api import star
14
+ from metafunctions.api import store
15
+ from metafunctions.core import CallState
16
+ from metafunctions.core.concurrent import ConcurrentMerge
17
+ from metafunctions.exceptions import CallError
18
+ from metafunctions.exceptions import CompositionError
19
+ from metafunctions.exceptions import ConcurrentException
20
+ from metafunctions.tests.simple_nodes import *
21
+ from metafunctions.tests.util import BaseTestCase
22
+
23
+
24
+ @unittest.skipIf(
25
+ platform.system() == "Windows", "Concurrent isn't supported on windows"
26
+ )
27
+ class TestIntegration(BaseTestCase):
28
+ def test_basic(self):
29
+ ab = a + b
30
+ cab = ConcurrentMerge(ab)
31
+ self.assertEqual(cab("_"), "_a_b")
32
+
33
+ def test_exceptions(self):
34
+ @node
35
+ def fail(x):
36
+ if not x:
37
+ raise ZeroDivisionError("division by zero")
38
+ return x - 1
39
+
40
+ cmp = locate_error(0 | ConcurrentMerge(fail - fail), use_color=True)
41
+
42
+ with self.assertRaises(ConcurrentException) as e:
43
+ cmp()
44
+ self.assertIsInstance(e.exception.__cause__, ZeroDivisionError)
45
+ self.assertEqual(
46
+ str(e.exception),
47
+ "Caught exception in child process \n\nOccured in the following function: "
48
+ "(0 | concurrent({} - fail))".format(util.red("->fail<-")),
49
+ )
50
+ self.assertIsInstance(e.exception.__cause__, ZeroDivisionError)
51
+
52
+ def test_consistent_meta(self):
53
+ """
54
+ Every function in the pipeline recieves the same meta.
55
+ """
56
+
57
+ @node
58
+ @bind_call_state
59
+ def f(call_state, x):
60
+ self.assertIs(call_state._meta_entry.function, cmp)
61
+ return 1
62
+
63
+ @node()
64
+ @bind_call_state
65
+ def g(call_state, x):
66
+ self.assertIs(call_state._meta_entry.function, cmp)
67
+ return 1
68
+
69
+ @node
70
+ @bind_call_state
71
+ def h(call_state, x):
72
+ self.assertIs(call_state._meta_entry.function, cmp)
73
+ return 1
74
+
75
+ @node
76
+ @bind_call_state
77
+ def i(call_state, x):
78
+ self.assertIs(call_state._meta_entry.function, cmp)
79
+ return 1
80
+
81
+ cmp = ConcurrentMerge(h + f + f / h + i - g)
82
+ self.assertEqual(cmp(1), 3)
83
+
84
+ self.assertEqual(cmp(1, call_state=cmp.new_call_state()), 3)
85
+
86
+ # how do pretty tracebacks work in multiprocessing?
87
+
88
+ def test_call(self):
89
+ c = concurrent(a + b)
90
+ self.assertEqual(c("_"), "_a_b")
91
+ self.assertEqual(c("-", "_"), "-a_b")
92
+ with self.assertRaises(CallError):
93
+ c("_", "_", "_")
94
+
95
+ @node
96
+ def d():
97
+ return "d"
98
+
99
+ abd = concurrent(a & b & d)
100
+ self.assertEqual(abd("-", "_"), ("-a", "_b", "d"))
101
+
102
+ def test_concurrent(self):
103
+ c = concurrent(a + b)
104
+ self.assertIsInstance(c, ConcurrentMerge)
105
+ self.assertEqual(c("_"), "_a_b")
106
+
107
+ def test_not_concurrent(self):
108
+ # can only upgrade FunctionMerges
109
+
110
+ with self.assertRaises(CompositionError):
111
+ concurrent(a)
112
+ with self.assertRaises(CompositionError):
113
+ concurrent(a | b)
114
+
115
+ def test_str_repr(self):
116
+ cab = ConcurrentMerge(a + b)
117
+ cmap = concurrent(mmap(a))
118
+
119
+ self.assertEqual(repr(cab), f"ConcurrentMerge({operator.add}, ({a!r}, {b!r}))")
120
+ self.assertEqual(str(cab), "concurrent(a + b)")
121
+ self.assertEqual(str(cmap), "concurrent(mmap(a))")
122
+
123
+ def test_basic_map(self):
124
+ # We can upgrade maps to run in parallel
125
+ banana = "bnn" | concurrent(mmap(a)) | "".join
126
+ str_concat = operators.concat | node("".join)
127
+ batman = concurrent(mmap(a, operator=str_concat))
128
+ self.assertEqual(banana(), "banana")
129
+ self.assertEqual(batman("nnnn"), "nananana")
130
+
131
+ def test_multi_arg_map(self):
132
+ @node
133
+ def f(*args):
134
+ return args
135
+
136
+ m = concurrent(mmap(f))
137
+
138
+ with self.assertRaises(CompositionError):
139
+ # Because star returns a simple function, we can't upgrade it.
140
+ starmap = concurrent(star(mmap(f)))
141
+ # we have to wrap concurrent in star instead.
142
+ starmap = star(concurrent(mmap(f)))
143
+
144
+ mapstar = concurrent(mmap(star(f)))
145
+
146
+ self.assertEqual(m([1, 2, 3], [4, 5, 6]), ((1, 4), (2, 5), (3, 6)))
147
+ self.assertEqual(m([1, 2, 3]), ((1,), (2,), (3,)))
148
+
149
+ with self.assertRaises(TypeError):
150
+ starmap([1, 2, 3])
151
+ self.assertEqual(starmap([[1, 2, 3]]), m([1, 2, 3]))
152
+
153
+ cmp = ([1, 2, 3], [4, 5, 6]) | starmap
154
+ self.assertEqual(cmp(), ((1, 4), (2, 5), (3, 6)))
155
+
156
+ cmp = ([1, 2, 3], [4, 5, 6]) | mapstar
157
+ self.assertEqual(cmp(), ((1, 2, 3), (4, 5, 6)))
158
+
159
+ def test_call_state(self):
160
+ # Call state should be usable in concurrent chains
161
+ chain_a = a | b | store("ab")
162
+ chain_b = b | a | store("ba")
163
+ cmp = concurrent(chain_a & chain_b)
164
+ state = CallState()
165
+
166
+ self.assertEqual(cmp("_", call_state=state), ("_ab", "_ba"))
167
+ self.assertDictEqual(state.data, {"ab": "_ab", "ba": "_ba"})
168
+
169
+ # If call_state.data contains something that isn't pickleable, fail gracefully
170
+ bad = [lambda: None] | store("o")
171
+ cmp = concurrent(bad & bad)
172
+ with self.assertRaises(ConcurrentException):
173
+ cmp()
174
+
175
+ def test_unpicklable_return(self):
176
+ # Concurrent can't handle functions that return unpicklable objects. Raise a descriptive
177
+ # exception
178
+ @node
179
+ def f():
180
+ return lambda: None
181
+
182
+ cmp = concurrent(f & f)
183
+ with self.assertRaises(ConcurrentException):
184
+ cmp()
185
+
186
+ def test_unpicklable_exception(self):
187
+ # Don't let child processes crash, even if they do weird things like raise unpickleable
188
+ # exceptions
189
+ @node
190
+ def f():
191
+ class BadException(Exception):
192
+ pass
193
+
194
+ raise BadException()
195
+
196
+ cmp = concurrent(f + f)
197
+ with self.assertRaises(ConcurrentException):
198
+ cmp()
199
+
200
+ @mock.patch("metafunctions.core.concurrent.os.fork", return_value=0)
201
+ @mock.patch("metafunctions.core.concurrent.os._exit")
202
+ @mock.patch("multiprocessing.queues.Queue.close")
203
+ @mock.patch("multiprocessing.queues.Queue.join_thread")
204
+ @mock.patch("metafunctions.core.concurrent.os.waitpid")
205
+ def test_no_fork(self, mock_wait, mock_join, mock_close, mock_exit, mock_fork):
206
+ # This test re-runs concurrent tests with forking disabled. Partially this is to
207
+ # address my inability to get coverage.py to recognize the code covered by forked
208
+ # processes, but it's also useful to have single process coverage of _process_in_fork to
209
+ # detect errors that may be squelched by the interactions of multiple processes
210
+
211
+ # Re-run all tests with fork patched
212
+ this_test = self.id().split(".")[-1]
213
+ for test_name in (
214
+ t for t in dir(self) if t.startswith("test_") and t != this_test
215
+ ):
216
+ method = getattr(self, test_name)
217
+ print("calling, ", method)
218
+ with self.subTest(name=test_name):
219
+ method()
220
+
221
+ @mock.patch("metafunctions.core.concurrent.hasattr", return_value=False)
222
+ def test_windows(self, mock_hasattr):
223
+ with self.assertRaises(CompositionError) as e:
224
+ concurrent(a + a)
225
+ self.assertEqual(
226
+ str(e.exception),
227
+ "ConcurrentMerge requires os.fork, and thus is only available on unix",
228
+ )
@@ -0,0 +1,24 @@
1
+ from metafunctions.core import DeferredValue
2
+ from metafunctions.tests.util import BaseTestCase
3
+
4
+
5
+ class TestUnit(BaseTestCase):
6
+ def test_call(self):
7
+ self.assertEqual(DeferredValue(5)(), 5)
8
+ a = object()
9
+ b = DeferredValue(a)
10
+ self.assertIs(b(), a)
11
+
12
+ def test_str_repr(self):
13
+ a = object()
14
+ b = DeferredValue(a)
15
+ self.assertEqual(repr(b), f"DeferredValue({a!r})")
16
+ self.assertEqual(str(DeferredValue(5)), "5")
17
+ self.assertEqual(repr(DeferredValue("a")), "DeferredValue('a')")
18
+
19
+ def test_functions(self):
20
+ # To fulfil the metafunction interface, DeferredValue.functions is a tuple containing self
21
+ # This lets you access the deffered value with (f() for f in x.functions)
22
+ a = object()
23
+ b = DeferredValue(a)
24
+ self.assertIs(b, b.functions[0])
@@ -0,0 +1,45 @@
1
+ import operator
2
+
3
+ from metafunctions.core import FunctionChain
4
+ from metafunctions.core import FunctionMerge
5
+ from metafunctions.core import SimpleFunction
6
+ from metafunctions.tests.util import BaseTestCase
7
+
8
+
9
+ class TestUnit(BaseTestCase):
10
+ def test_str(self):
11
+ c = FunctionChain(a, b, l)
12
+ self.assertEqual(str(c), "(a | b | <lambda>)")
13
+ self.assertEqual(repr(c), f"FunctionChain{(a, b, l)}")
14
+
15
+ def test_call(self):
16
+ c = FunctionChain(a, b, l)
17
+ self.assertEqual(c("_"), "_abl")
18
+
19
+ def test_combine(self):
20
+ """Avoid nesting chains"""
21
+ chain = FunctionChain(a, b, l)
22
+ merge = FunctionMerge(operator.add, (a, b))
23
+
24
+ self.assertEqual(
25
+ str(FunctionChain.combine(chain, chain)),
26
+ "(a | b | <lambda> | a | b | <lambda>)",
27
+ )
28
+ self.assertEqual(str(chain | chain), "(a | b | <lambda> | a | b | <lambda>)")
29
+ self.assertEqual(
30
+ str(chain | merge | chain),
31
+ "(a | b | <lambda> | (a + b) | a | b | <lambda>)",
32
+ )
33
+
34
+
35
+ @SimpleFunction
36
+ def a(x):
37
+ return x + "a"
38
+
39
+
40
+ @SimpleFunction
41
+ def b(x):
42
+ return x + "b"
43
+
44
+
45
+ l = SimpleFunction(lambda x: x + "l")
@@ -0,0 +1,154 @@
1
+ import operator
2
+ import unittest
3
+
4
+ from metafunctions import exceptions
5
+ from metafunctions.api import node
6
+ from metafunctions.api import star
7
+ from metafunctions.core import FunctionMerge
8
+ from metafunctions.core import SimpleFunction
9
+ from metafunctions.operators import concat
10
+ from metafunctions.tests.util import BaseTestCase
11
+
12
+
13
+ class TestUnit(BaseTestCase):
14
+ def test_str(self):
15
+ cmp = FunctionMerge(operator.add, (a, b))
16
+ self.assertEqual(str(cmp), "(a + b)")
17
+ self.assertEqual(repr(cmp), f"FunctionMerge({operator.add}, {(a, b)})")
18
+
19
+ def test_call(self):
20
+ cmp = FunctionMerge(operator.add, (a, b))
21
+ self.assertEqual(cmp("_"), "_a_b")
22
+ self.assertEqual(cmp("-", "_"), "-a_b")
23
+ with self.assertRaises(exceptions.CallError):
24
+ cmp("_", "_", "_")
25
+
26
+ @SimpleFunction
27
+ def d():
28
+ return "d"
29
+
30
+ abd = a & b & d
31
+ self.assertEqual(abd("-", "_"), ("-a", "_b", "d"))
32
+
33
+ def test_format(self):
34
+ cmp = FunctionMerge(operator.add, (a, b), function_join_str="tacos")
35
+ self.assertEqual(str(cmp), "(a tacos b)")
36
+
37
+ def test_non_binary(self):
38
+ def my_concat(*args):
39
+ return "".join(args)
40
+
41
+ cmp = FunctionMerge(my_concat, (a, a, a, a))
42
+ self.assertEqual(cmp("_"), "_a_a_a_a")
43
+
44
+ self.assertEqual(str(cmp), f"(a {my_concat} a {my_concat} a {my_concat} a)")
45
+
46
+ d = FunctionMerge(my_concat, (b, b), function_join_str="q")
47
+ self.assertEqual(str(d), "(b q b)")
48
+
49
+ def test_join(self):
50
+ # The first real non-binary function! Like the example above.
51
+
52
+ cmp = a & a & "sweet as"
53
+
54
+ self.assertTupleEqual(cmp("_"), ("_a", "_a", "sweet as"))
55
+ self.assertEqual(str(cmp), "(a & a & 'sweet as')")
56
+ self.assertEqual(
57
+ repr(cmp),
58
+ f"FunctionMerge({concat}, {(a, a, cmp._functions[-1])})",
59
+ )
60
+
61
+ # __rand__ works too
62
+ a_ = "sweet as" & a
63
+ self.assertEqual(a_("+"), ("sweet as", "+a"))
64
+
65
+ abc = (a & (b & c)) | "".join
66
+ self.assertEqual(abc("_"), "_a_b_c")
67
+
68
+ def test_combine(self):
69
+ # Only combine FunctionMerges that have the same MergeFunc
70
+ add = a + b
71
+ also_add = b + a
72
+ div = a / b
73
+
74
+ # This combined FunctionMerge will fail if called (because addition is binary,
75
+ # operator.add only takes two args). I'm just using to combine for test purposes.
76
+ abba = FunctionMerge.combine(operator.add, add, also_add)
77
+ self.assertEqual(str(abba), "(a + b + b + a)")
78
+ self.assertEqual(repr(abba), f"FunctionMerge({operator.add}, {(a, b, b, a)})")
79
+
80
+ ab_ba = FunctionMerge.combine(operator.sub, add, also_add)
81
+ self.assertEqual(str(ab_ba), "((a + b) - (b + a))")
82
+ self.assertEqual(
83
+ repr(ab_ba), f"FunctionMerge({operator.sub}, {(add, also_add)})"
84
+ )
85
+
86
+ abab = FunctionMerge.combine(operator.add, add, div)
87
+ self.assertEqual(str(abab), "(a + b + (a / b))")
88
+ self.assertEqual(repr(abab), f"FunctionMerge({operator.add}, {(a, b, div)})")
89
+
90
+ def custom():
91
+ pass
92
+
93
+ abba_ = FunctionMerge.combine(custom, add, also_add, function_join_str="<>")
94
+ self.assertEqual(str(abba_), "((a + b) <> (b + a))")
95
+ self.assertEqual(repr(abba_), f"FunctionMerge({custom}, {(add, also_add)})")
96
+
97
+ def my_concat(*args):
98
+ return "".join(args)
99
+
100
+ bb = FunctionMerge(my_concat, (b, b), function_join_str="q")
101
+ aa = FunctionMerge(my_concat, (a, a), function_join_str="q")
102
+ bbaa = FunctionMerge.combine(my_concat, bb, aa, function_join_str="q")
103
+ self.assertEqual(str(bbaa), "(b q b q a q a)")
104
+ self.assertEqual(repr(bbaa), f"FunctionMerge({my_concat}, {(b, b, a, a)})")
105
+
106
+ def test_len_mismatch(self):
107
+ # If len(inputs) <= len(functions), call remaining functions with no args.
108
+ @node
109
+ def f(x=None):
110
+ if x:
111
+ return x + "f"
112
+ return "F"
113
+
114
+ cmp = (a & b) | star(f & f & f & f)
115
+ self.assertEqual(cmp("_"), ("_af", "_bf", "F", "F"))
116
+
117
+ # if len(inputs) > len(functions), fail.
118
+ cmp = (a & b & c) | star(f + f)
119
+ with self.assertRaises(exceptions.CallError):
120
+ cmp("_")
121
+
122
+ @unittest.skip("TODO")
123
+ def test_binary_functions(self):
124
+ # The issue here is that f + f + f + f is not converted to a single FunctionMerge. Rather
125
+ # it becomes nested FunctionMerges: (((f + f) + f) + f). Ideally we would be able to
126
+ # handle this. One potential solution is to 'flatten' the FunctionMerge, but this doesn't
127
+ # work for functions that aren't commutative. E.g., (a / b / c) != (a / (b / c)). I'm
128
+ # leaving this test for now as a todo.
129
+ @node
130
+ def f(x=None):
131
+ if x:
132
+ return x + "f"
133
+ return "F"
134
+
135
+ cmp = (a & b) | star(f + f + f + f)
136
+ self.assertEqual(cmp("_"), "_af_bfFF")
137
+
138
+
139
+ @SimpleFunction
140
+ def a(x):
141
+ return x + "a"
142
+
143
+
144
+ @SimpleFunction
145
+ def b(x):
146
+ return x + "b"
147
+
148
+
149
+ @node
150
+ def c(x):
151
+ return x + "c"
152
+
153
+
154
+ l = SimpleFunction(lambda x: x + "l")
@@ -0,0 +1,34 @@
1
+ import importlib
2
+ import importlib.metadata
3
+ import random
4
+ import unittest
5
+ from unittest import mock
6
+
7
+ import metafunctions
8
+
9
+
10
+ class TestUnit(unittest.TestCase):
11
+ def test_api_imports(self):
12
+ expected_names = [
13
+ "node",
14
+ "bind_call_state",
15
+ "star",
16
+ "store",
17
+ "recall",
18
+ "concurrent",
19
+ "mmap",
20
+ "locate_error",
21
+ ]
22
+ random.shuffle(expected_names)
23
+ for name in expected_names:
24
+ exec(f"from metafunctions import {name}") # noqa: S102
25
+
26
+ def test_version_not_installed(self):
27
+ # Reload again afterwards so later tests see the real version
28
+ self.addCleanup(importlib.reload, metafunctions)
29
+ with mock.patch(
30
+ "importlib.metadata.version",
31
+ side_effect=importlib.metadata.PackageNotFoundError("metafunctions"),
32
+ ):
33
+ importlib.reload(metafunctions)
34
+ self.assertEqual(metafunctions.__version__, "unknown")
@@ -0,0 +1,56 @@
1
+ from metafunctions import operators
2
+ from metafunctions.api import mmap
3
+ from metafunctions.api import node
4
+ from metafunctions.api import star
5
+ from metafunctions.map import MergeMap
6
+ from metafunctions.tests.simple_nodes import *
7
+ from metafunctions.tests.util import BaseTestCase
8
+
9
+
10
+ class TestIntegration(BaseTestCase):
11
+ def test_basic(self):
12
+ banana = "bnn" | MergeMap(a) | "".join
13
+ str_concat = operators.concat | node("".join)
14
+ batman = MergeMap(a, merge_function=str_concat)
15
+ self.assertEqual(banana(), "banana")
16
+ self.assertEqual(batman("nnnn"), "nananana")
17
+
18
+ def test_multi_arg(self):
19
+ @node
20
+ def f(*args):
21
+ return args
22
+
23
+ m = mmap(f)
24
+ starmap = star(mmap(f))
25
+ mapstar = mmap(star(f))
26
+
27
+ self.assertEqual(m([1, 2, 3], [4, 5, 6]), ((1, 4), (2, 5), (3, 6)))
28
+ self.assertEqual(m([1, 2, 3]), ((1,), (2,), (3,)))
29
+
30
+ with self.assertRaises(TypeError):
31
+ starmap([1, 2, 3])
32
+ self.assertEqual(starmap([[1, 2, 3]]), m([1, 2, 3]))
33
+
34
+ cmp = ([1, 2, 3], [4, 5, 6]) | starmap
35
+ self.assertEqual(cmp(), ((1, 4), (2, 5), (3, 6)))
36
+
37
+ cmp = ([1, 2, 3], [4, 5, 6]) | mapstar
38
+ self.assertEqual(cmp(), ((1, 2, 3), (4, 5, 6)))
39
+
40
+ def test_auto_meta(self):
41
+ mapsum = mmap(sum)
42
+ self.assertEqual(mapsum([[1, 2], [3, 4]]), (3, 7))
43
+ self.assertEqual(str(mapsum), "mmap(sum)")
44
+
45
+ def test_str_repr(self):
46
+ m = MergeMap(a)
47
+ self.assertEqual(str(m), "mmap(a)")
48
+ self.assertEqual(repr(m), f"MergeMap(a, merge_function={operators.concat})")
49
+
50
+ def test_loop(self):
51
+ cmp = (b & c & "stoke") | mmap(a)
52
+ self.assertEqual(cmp("_"), ("_ba", "_ca", "stokea"))
53
+
54
+ def test_loop_with_non_meta(self):
55
+ cmp = (b & c & "stoke") | mmap(len)
56
+ self.assertEqual(cmp("_"), (2, 2, 5))