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,319 @@
1
+ import itertools
2
+
3
+ from metafunctions.api import bind_call_state
4
+ from metafunctions.api import locate_error
5
+ from metafunctions.api import node
6
+ from metafunctions.core import CallState
7
+ from metafunctions.tests.simple_nodes import *
8
+ from metafunctions.tests.util import BaseTestCase
9
+
10
+
11
+ class TestIntegration(BaseTestCase):
12
+ def test_basic_usage(self):
13
+ self.assertEqual(a("_"), "_a")
14
+
15
+ def test_wraps(self):
16
+ @node
17
+ def d():
18
+ "a docstring for d"
19
+
20
+ self.assertEqual(d.__doc__, "a docstring for d")
21
+
22
+ def test_auto_meta(self):
23
+ """If possible, we upgrade functions to meta functions on the fly."""
24
+
25
+ def y(x):
26
+ return x + "y"
27
+
28
+ ay = a | y
29
+ ya = y | a
30
+ ayyyy = a | y | y | y | y
31
+
32
+ # Can't do this
33
+ # ayy = a | y + y
34
+
35
+ # But this should work
36
+ yayy = y | a + y
37
+ yy_ya = y | y + a
38
+
39
+ self.assertEqual(ay("_"), "_ay")
40
+ self.assertEqual(ya("_"), "_ya")
41
+ self.assertEqual(ayyyy("_"), "_ayyyy")
42
+ self.assertEqual(yayy("_"), "_ya_yy")
43
+ self.assertEqual(yy_ya("_"), "_yy_ya")
44
+
45
+ def test_auto_meta_builtins(self):
46
+ """We can upgrade builtin functions too"""
47
+
48
+ mean = node(sum) / len
49
+ self.assertEqual(mean((100, 200, 300)), 200)
50
+
51
+ def test_basic_composition(self):
52
+ composite = a | b | c | d
53
+ self.assertEqual(composite("_"), "_abcd")
54
+
55
+ def test_advanced_str(self):
56
+ cmp = a | b + c + d | e
57
+ self.assertEqual(str(cmp), "(a | ((b + c) + d) | e)")
58
+ self.assertEqual(cmp("_"), "_ab_ac_ade")
59
+
60
+ cmp = a + "f"
61
+ self.assertEqual(str(cmp), "(a + 'f')")
62
+ self.assertEqual(
63
+ repr(cmp),
64
+ "FunctionMerge(<built-in function add>, "
65
+ f"(SimpleFunction({a._function!r}), DeferredValue('f')))",
66
+ )
67
+
68
+ def test_non_callable_composition(self):
69
+ """
70
+ Anything that is not callable in a composition is applied at call time (to the results
71
+ of the composed functions).
72
+ """
73
+
74
+ @node
75
+ def g(x):
76
+ return x
77
+
78
+ cmps_to_expected = (
79
+ (g + 1, 11),
80
+ (g - 1, 9),
81
+ (g * 2, 20),
82
+ (g / 2, 5),
83
+ (1 + g, 11),
84
+ (1 - g, -9),
85
+ (2 * g, 20),
86
+ (2 / g, 0.2),
87
+ )
88
+
89
+ for cmp, expected in cmps_to_expected:
90
+ with self.subTest():
91
+ self.assertEqual(cmp(10), expected)
92
+
93
+ def test_single_calls(self):
94
+ """every function is only called once"""
95
+ call_count = 0
96
+
97
+ @node
98
+ def y(x):
99
+ nonlocal call_count
100
+ call_count += 1
101
+ return x + "y"
102
+
103
+ cmp = y | y * 2 | y + y | y
104
+ self.assertEqual(cmp("_"), "_yy_yyy_yy_yyyy")
105
+ self.assertEqual(call_count, 5)
106
+
107
+ def test_repr(self):
108
+ cmp = a | b | c | (lambda x: None)
109
+ self.assertEqual(str(cmp), "(a | b | c | <lambda>)")
110
+
111
+ def test_called_functions(self):
112
+ # This made more sense back before call_state was a tree. Consider removing.
113
+ @node
114
+ @bind_call_state
115
+ def parent_test(call_state, x):
116
+ return call_state
117
+
118
+ ab = a | b
119
+ abc = ab + c
120
+ abc_ = abc | parent_test
121
+
122
+ call_state = abc_("_")
123
+ self.assertIsInstance(call_state, CallState)
124
+ self.assertListEqual(
125
+ [n.function for n in call_state._children[(abc_, 0)]], [abc, parent_test]
126
+ )
127
+
128
+ def test_pretty_exceptions(self):
129
+ @node
130
+ def f(x):
131
+ raise RuntimeError("Something bad happened!")
132
+
133
+ @node
134
+ def g(x):
135
+ raise RuntimeError("Something bad happened in g!")
136
+
137
+ abf = locate_error((a | b + f), use_color=False)
138
+ abg = a | b + g
139
+
140
+ with self.assertRaises(RuntimeError) as ctx:
141
+ abf("_")
142
+
143
+ self.assertEqual(
144
+ str(ctx.exception),
145
+ "Something bad happened! \n\nOccured in the following function: (a | (b + ->f<-))",
146
+ )
147
+
148
+ # unprettified exceptions work
149
+ with self.assertRaises(RuntimeError):
150
+ abg("_")
151
+
152
+ def test_consistent_meta(self):
153
+ """
154
+ Every function in the pipeline recieves the same meta.
155
+ """
156
+
157
+ @node
158
+ @bind_call_state
159
+ def f(call_state, x):
160
+ self.assertIs(call_state._meta_entry.function, cmp)
161
+ return 1
162
+
163
+ @node()
164
+ @bind_call_state
165
+ def g(call_state, x):
166
+ self.assertIs(call_state._meta_entry.function, cmp)
167
+ return 1
168
+
169
+ @node
170
+ @bind_call_state
171
+ def h(call_state, x):
172
+ self.assertIs(call_state._meta_entry.function, cmp)
173
+ return 1
174
+
175
+ @node
176
+ @bind_call_state
177
+ def i(call_state, x):
178
+ self.assertIs(call_state._meta_entry.function, cmp)
179
+ return 1
180
+
181
+ cmp = f | g | i | h + f + f / h + i - g
182
+ self.assertEqual(cmp(1), 3)
183
+
184
+ # this works if we provide our own call_state too.
185
+ self.assertEqual(cmp(1, call_state=CallState()), 3)
186
+
187
+ def test_consistent_meta_with_shared_state(self):
188
+ @node
189
+ @bind_call_state
190
+ def f(call_state, expected):
191
+ self.assertIs(call_state._meta_entry.function, expected)
192
+ return expected
193
+
194
+ cmpa = f & f
195
+ cmpb = f | f | f
196
+
197
+ cmpa(cmpa)
198
+ cmpb(cmpb)
199
+
200
+ state = CallState()
201
+ cmpa(cmpa, call_state=state)
202
+ self.assertDictEqual(state._parents, {})
203
+ cmpb(cmpb, call_state=state)
204
+ self.assertEqual(state._nodes_visited, 7)
205
+
206
+ def test_defaults(self):
207
+ """
208
+ If you specify defaults in nodes, they are respected.
209
+ """
210
+
211
+ @node
212
+ def f(x="F"):
213
+ return x + "f"
214
+
215
+ @node
216
+ @bind_call_state
217
+ def g(call_state, x="G"):
218
+ return x + "g"
219
+
220
+ cmp = f | g | f + g
221
+ self.assertEqual(cmp(), "FfgfFfgg")
222
+ self.assertEqual(cmp("_"), "_fgf_fgg")
223
+
224
+ cmp2 = g | f | f + g
225
+ self.assertEqual(cmp2(), "GgffGgfg")
226
+ self.assertEqual(cmp2("_"), "_gff_gfg")
227
+
228
+ def test_complex_exceptions(self):
229
+ @node
230
+ def query_volume(x):
231
+ return str(x**2)
232
+
233
+ @node
234
+ def query_price(x):
235
+ return "$" + str(x**3)
236
+
237
+ numeric = (query_volume | float) + (query_price | float)
238
+ with self.assertRaises(ValueError):
239
+ numeric(2)
240
+
241
+ def test_decoration(self):
242
+ # It should be possible to decorate a metafunction with another metafunction and have
243
+ # everything still work (as long as the decorated function gets upgraded to a metafunction).
244
+ # I don't think this is something that will happen, but I want to enforce that it is
245
+ # possible, for purposes of conceptual purity.
246
+ @node()
247
+ @bind_call_state
248
+ def f(call_state, x):
249
+ self.assertIs(call_state._meta_entry.function, abcf)
250
+ return x + "f"
251
+
252
+ fn = node(f + "sup")
253
+ self.assertEqual(repr(fn), "SimpleFunction({!r})".format(f + "sup"))
254
+ self.assertEqual(str(fn), "(f + 'sup')")
255
+ abcf = a | b | c | fn
256
+
257
+ self.assertEqual(abcf("_"), "_abcfsup")
258
+
259
+ def test_kwargs(self):
260
+ # Kwargs are passed to all functions
261
+
262
+ @node
263
+ def k(x, k="k"):
264
+ return x + k
265
+
266
+ self.assertEqual(k("_"), "_k")
267
+ self.assertEqual(k("_", k="_"), "__")
268
+
269
+ kk = k + k
270
+ self.assertEqual(kk("_"), "_k_k")
271
+ self.assertEqual(kk("_", k="_"), "____")
272
+
273
+ klen = k | len
274
+ self.assertEqual(klen("_"), 2)
275
+ with self.assertRaises(TypeError):
276
+ # passing a kwarg to len causes an error
277
+ klen("_", k=5)
278
+
279
+ def test_multi_call_immutability(self):
280
+ # Unless you manually supply the same call_state, mutliple calls do not share state.
281
+ @node
282
+ @bind_call_state
283
+ def f(call_state, x):
284
+ if "f" not in call_state.data:
285
+ call_state.data["f"] = x
286
+ return x
287
+
288
+ @node
289
+ @bind_call_state
290
+ def g(call_state, x):
291
+ return x + call_state.data.get("f", "g")
292
+
293
+ cmp = a | b | c | d | e | f | g
294
+ self.assertEqual(cmp("_"), "_abcde_abcde")
295
+ self.assertEqual(cmp("*"), "*abcde*abcde")
296
+
297
+ state = CallState()
298
+ self.assertEqual(cmp("_", call_state=state), "_abcde_abcde")
299
+ self.assertEqual(cmp("*", call_state=state), "*abcde_abcde")
300
+
301
+ def test_generators(self):
302
+ # Can you use Metafunctions with generators?
303
+
304
+ @node
305
+ def gen_a():
306
+ yield from itertools.repeat("a")
307
+
308
+ @node
309
+ def gen_b(pred):
310
+ yield from (x + "b" for x in pred)
311
+
312
+ @node
313
+ def gen_c(pred):
314
+ return (x + "c" for x in pred)
315
+
316
+ cmp = gen_a | gen_c | gen_b
317
+ gen = cmp()
318
+ for _ in range(5):
319
+ self.assertEqual(next(gen), "acb")
@@ -0,0 +1,24 @@
1
+ from metafunctions.core import SimpleFunction
2
+ from metafunctions.tests.util import BaseTestCase
3
+
4
+
5
+ class TestUnit(BaseTestCase):
6
+ def test_decorator(self):
7
+ self.assertEqual(a.__name__, "a")
8
+ self.assertEqual(a.__module__, __name__)
9
+
10
+ def test_call(self):
11
+ self.assertEqual(a("_"), "_a")
12
+ self.assertEqual(l("_"), "_l")
13
+
14
+ def test_str(self):
15
+ self.assertEqual(repr(a), f"SimpleFunction({a._function!r})")
16
+ self.assertEqual(str(a), "a")
17
+
18
+
19
+ @SimpleFunction
20
+ def a(x):
21
+ return x + "a"
22
+
23
+
24
+ l = SimpleFunction(lambda x: x + "l")
@@ -0,0 +1,83 @@
1
+ import os
2
+ import unittest
3
+
4
+ from metafunctions import exceptions
5
+ from metafunctions.api import concurrent
6
+ from metafunctions.api import node
7
+ from metafunctions.api import star
8
+ from metafunctions.tests.simple_nodes import *
9
+ from metafunctions.tests.util import BaseTestCase
10
+
11
+
12
+ class TestUnit(BaseTestCase):
13
+ def test_simple_star(self):
14
+ @node
15
+ def f(*args):
16
+ return args
17
+
18
+ cmp = (a | b) | star(f)
19
+ self.assertEqual(cmp("_"), ("_", "a", "b"))
20
+
21
+ self.assertEqual(star(f)([1, 2, 3]), (1, 2, 3))
22
+
23
+ def test_str_repr(self):
24
+ @node
25
+ def f(*args):
26
+ return args
27
+
28
+ @star
29
+ @node
30
+ def g(*x):
31
+ return x
32
+
33
+ @star
34
+ def h(*x):
35
+ return x
36
+
37
+ cmp = (a | b) | star(f)
38
+ star_a = star(a)
39
+ merge_star = star(a + b)
40
+ chain_star = star(a | b)
41
+
42
+ self.assertEqual(str(cmp), "(a | b | star(f))")
43
+ self.assertEqual(str(star_a), "star(a)")
44
+ self.assertEqual(str(g), "star(g)")
45
+ self.assertEqual(str(merge_star), "star(a + b)")
46
+ self.assertEqual(str(chain_star), "star(a | b)")
47
+
48
+ # You can technically apply star to a regular function, and it'll become a SimpleFunction
49
+ self.assertEqual(str(h), f"star({h._function.__closure__[0].cell_contents})")
50
+
51
+ # reprs remain the same
52
+ self.assertEqual(repr(star_a), f"SimpleFunction({star_a._function})")
53
+
54
+ @unittest.skipUnless(hasattr(os, "fork"), "Concurent isn't available on windows")
55
+ def test_concurrent(self):
56
+ # Concurrent and star can work together, although this organization no longer makes sense
57
+ with self.assertRaises(exceptions.CompositionError):
58
+ aabbcc = a & b & c | concurrent(star(a & b & c))
59
+ # self.assertEqual(aabbcc('_'), '_aa_bb_cc')
60
+
61
+ aabbcc = (a & b & c) | star(concurrent(a & b & c))
62
+ self.assertEqual(aabbcc("_"), ("_aa", "_bb", "_cc"))
63
+
64
+ @unittest.skip("TODO")
65
+ def test_recursive_upgrade(self):
66
+ aabbcc = (a & b & c) | star(a + b + c)
67
+ self.assertEqual(aabbcc("_"), "_aa_bb_cc")
68
+
69
+ def test_simple_args(self):
70
+ @node
71
+ def foo(a):
72
+ return a
73
+
74
+ @node
75
+ def bar(b):
76
+ return b
77
+
78
+ @node
79
+ def cool(a, b, c, d):
80
+ return (a, b, c, d)
81
+
82
+ f = (foo & bar & "arg_c" & "arg_d") | star(cool)
83
+ assert f("a", "b") == ("a", "b", "arg_c", "arg_d")
@@ -0,0 +1,86 @@
1
+ import os
2
+ from unittest import mock
3
+
4
+ from metafunctions import util
5
+ from metafunctions.api import locate_error
6
+ from metafunctions.api import node
7
+ from metafunctions.tests.simple_nodes import *
8
+ from metafunctions.tests.util import BaseTestCase
9
+
10
+
11
+ class TestUnit(BaseTestCase):
12
+ def test_locate_error(self):
13
+ @node
14
+ def fail(x):
15
+ raise ZeroDivisionError("division by zero")
16
+
17
+ cmp = a + b | (c & fail & fail)
18
+ with_tb = locate_error(cmp, use_color=False)
19
+
20
+ with self.assertRaises(ZeroDivisionError) as e:
21
+ cmp("x")
22
+ self.assertEqual(str(e.exception), "division by zero")
23
+
24
+ with self.assertRaises(ZeroDivisionError) as e:
25
+ with_tb("x")
26
+ self.assertEqual(
27
+ str(e.exception),
28
+ "division by zero \n\nOccured in the following function: ((a + b) | (c & ->fail<- & fail))",
29
+ )
30
+
31
+ # regular calls still work
32
+ with_tb2 = locate_error(a | b | c)
33
+ self.assertEqual(with_tb2("_"), "_abc")
34
+
35
+ self.assertEqual(str(with_tb), str(cmp))
36
+ self.assertEqual(str(with_tb), "((a + b) | (c & fail & fail))")
37
+ self.assertEqual(str(with_tb2), "(a | b | c)")
38
+
39
+ def test_replace_nth(self):
40
+ s = "aaaaaaaaaa"
41
+ self.assertEqual(util.replace_nth(s, "aa", 3, "BB"), "aaaaBBaaaa")
42
+
43
+ new = util.replace_nth(s, "nothere", 1, "BB")
44
+ self.assertEqual(new, s)
45
+
46
+ def test_color_highlights(self):
47
+ s = "a ->test<- string ->for<- highlight"
48
+ self.assertEqual(
49
+ util.color_highlights(s),
50
+ "a {} string {} highlight".format(
51
+ util.red("->test<-"), util.red("->for<-")
52
+ ),
53
+ )
54
+
55
+ def test_red(self):
56
+ self.assertEqual(util.red("x"), "\x1b[31mx\x1b[0m")
57
+
58
+ def test_system_supports_color(self):
59
+ tty = mock.Mock(isatty=lambda: True)
60
+ not_tty = mock.Mock(isatty=lambda: False)
61
+
62
+ def supports_color(env, platform="linux", stdout=tty):
63
+ with (
64
+ mock.patch.dict(os.environ, env, clear=True),
65
+ mock.patch.object(util.sys, "platform", platform),
66
+ mock.patch.object(util.sys, "stdout", stdout),
67
+ ):
68
+ return util.system_supports_color()
69
+
70
+ # Default: color only on a tty, and never on windows
71
+ self.assertTrue(supports_color({}))
72
+ self.assertFalse(supports_color({}, stdout=not_tty))
73
+ self.assertFalse(supports_color({}, stdout=object()))
74
+ self.assertFalse(supports_color({}, platform="win32"))
75
+
76
+ # NO_COLOR disables color
77
+ self.assertFalse(supports_color({"NO_COLOR": "1"}))
78
+ self.assertTrue(supports_color({"NO_COLOR": ""}))
79
+
80
+ # FORCE_COLOR enables color, even without a tty or on windows
81
+ self.assertTrue(supports_color({"FORCE_COLOR": "1"}, stdout=not_tty))
82
+ self.assertTrue(supports_color({"FORCE_COLOR": "1"}, platform="win32"))
83
+ self.assertFalse(supports_color({"FORCE_COLOR": ""}, stdout=not_tty))
84
+
85
+ # NO_COLOR takes precedence over FORCE_COLOR
86
+ self.assertFalse(supports_color({"NO_COLOR": "1", "FORCE_COLOR": "1"}))
@@ -0,0 +1,38 @@
1
+ """
2
+ Testing tools
3
+ """
4
+
5
+ import os
6
+ import random
7
+ import sys
8
+ import tempfile
9
+ import unittest
10
+
11
+
12
+ class BaseTestCase(unittest.TestCase):
13
+ def __init__(self, methodName="runTest"):
14
+ super().__init__(methodName)
15
+ self._tempdir = None
16
+ self.seed = random.randrange(sys.maxsize)
17
+
18
+ @property
19
+ def self_destructing_directory(self):
20
+ """Return a temporary directory that exists for the duration of the test and is automatically removed after teardown."""
21
+ if not (self._tempdir and os.path.exists(self._tempdir.name)):
22
+ self._tempdir = tempfile.TemporaryDirectory(
23
+ prefix=f"{self._testMethodName}_"
24
+ )
25
+ self.addCleanup(self.cleanup_self_destructing_directory)
26
+ self._tempdir.__enter__()
27
+ return self._tempdir.name
28
+
29
+ def cleanup_self_destructing_directory(self):
30
+ # Only try to exit the temporaryDirectory context manager if the directory still exists
31
+ if os.path.exists(self._tempdir.name):
32
+ self._tempdir.__exit__(None, None, None)
33
+
34
+ def _formatMessage(self, msg, standardMsg):
35
+ msg = msg or ""
36
+ return super()._formatMessage(
37
+ msg=f"{msg} (seed: {self.seed})", standardMsg=standardMsg
38
+ )
metafunctions/util.py ADDED
@@ -0,0 +1,52 @@
1
+ """Utilities for working with MetaFunctions"""
2
+
3
+ import os
4
+ import re
5
+ import sys
6
+
7
+ HIGHLIGHT_TEMPLATE = "->{}<-"
8
+
9
+
10
+ def red(string):
11
+ """Wrap string in ANSI escape codes for red text"""
12
+ return f"\033[31m{string}\033[0m"
13
+
14
+
15
+ def highlight(string):
16
+ return HIGHLIGHT_TEMPLATE.format(string)
17
+
18
+
19
+ _color_regex = re.compile(HIGHLIGHT_TEMPLATE.format(".*?"))
20
+
21
+
22
+ def color_highlights(string):
23
+ """Color all highlights red"""
24
+ for substr in _color_regex.findall(string):
25
+ string = string.replace(substr, red(substr))
26
+ return string
27
+
28
+
29
+ def system_supports_color():
30
+ """
31
+ Returns True if the running system's terminal supports color, and False otherwise.
32
+
33
+ A non-empty NO_COLOR environment variable disables color (https://no-color.org), and a
34
+ non-empty FORCE_COLOR enables it. NO_COLOR takes precedence. Otherwise, color is off on
35
+ Windows, and elsewhere is on only if stdout is a tty.
36
+ """
37
+ if os.environ.get("NO_COLOR"):
38
+ return False
39
+ if os.environ.get("FORCE_COLOR"):
40
+ return True
41
+ if sys.platform == "win32":
42
+ return False
43
+ # isatty is not always implemented
44
+ return hasattr(sys.stdout, "isatty") and sys.stdout.isatty()
45
+
46
+
47
+ def replace_nth(string, substring, occurance_index: int, new_substring):
48
+ """Return string, with the instance of substring at `occurance_index` replaced with new_substring"""
49
+ escaped = re.escape(substring)
50
+ # There's probably a better regex for this.
51
+ regex = f"((?:.*?{escaped}.*?){{{occurance_index - 1}}}.*?){escaped}(.*$)"
52
+ return re.sub(regex, rf"\1{new_substring}\2", string)