-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtest_funutil.py
More file actions
191 lines (165 loc) · 7.04 KB
/
test_funutil.py
File metadata and controls
191 lines (165 loc) · 7.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# -*- coding: utf-8 -*-
from ..syntax import macros, test, the # noqa: F401
from ..test.fixtures import session, testset
from operator import add
from functools import partial
# `Values` is also tested where function composition utilities that use it are.
from ..funutil import call, callwith, Values, valuify
def runtests():
with testset("@call (def as code block)"):
# def as a code block (function overwritten by return value)
@call
def result():
return "hello"
test[result == "hello"]
# use case 1: make temporaries fall out of scope
@call
def x():
a = 2 # many temporaries that help readability...
b = 3 # ...of this calculation, but would just pollute locals...
c = 5 # ...after the block exits
return a * b * c
test[x == 30]
# use case 2: multi-break out of nested loops
@call
def result():
for x in range(10):
for y in range(10):
if x * y == 42:
return (x, y)
... # more code here # pragma: no cover
test[result == (6, 7)]
# can also be used normally
test[the[call(add, 2, 3)] == the[add(2, 3)]]
with testset("@callwith (argument freezer), and pythonic solutions to avoid it"):
# to pass arguments when used as decorator, use @callwith instead
@callwith(3)
def result(x):
return x**2
test[result == 9]
# specialize for given arguments, choose function later
apply23 = callwith(2, 3)
def myadd(a, b):
return a + b
def mymul(a, b):
return a * b
test[apply23(myadd) == 5]
test[apply23(mymul) == 6]
# callwith is not essential; we can do the same pythonically like this:
a = [2, 3]
test[myadd(*a) == 5]
test[mymul(*a) == 6]
# build up the argument list as we go
# - note curry does not help, must use partial; this is because curry
# will happily call "callwith" (and thus terminate the gathering step)
# as soon as it gets at least one argument.
p1 = partial(callwith, 2)
p2 = partial(p1, 3)
p3 = partial(p2, 4)
apply234 = p3() # terminate gathering step by actually calling callwith
def add3(a, b, c):
return a + b + c
def mul3(a, b, c):
return a * b * c
test[apply234(add3) == 9]
test[apply234(mul3) == 24]
# pythonic solution:
a = [2]
a += [3]
a += [4]
test[add3(*a) == 9]
test[mul3(*a) == 24]
# callwith in map, if we want to vary the function instead of the data
m = map(callwith(3), [lambda x: 2 * x,
lambda x: x**2,
lambda x: x**(1 / 2)])
test[tuple(m) == (6, 9, 3**(1 / 2))]
# pythonic solution - use comprehension notation:
m = (f(3) for f in [lambda x: 2 * x,
lambda x: x**2,
lambda x: x**(1 / 2)])
test[tuple(m) == (6, 9, 3**(1 / 2))]
with testset("Values unpacking in call/callwith"):
# Leading Values: rets become positional args, kwrets become kwargs.
v = Values(1, 2, x=3)
test[call(lambda a, b, x: (a, b, x), v) == (1, 2, 3)]
test[call(add, Values(2, 3)) == 5]
# Mixed with regular args: Values expands at its position, others stay put.
def f3(a, b, c):
return (a, b, c)
test[call(f3, 1, Values(2, 3)) == (1, 2, 3)]
test[call(f3, Values(1, 2), 3) == (1, 2, 3)]
test[call(f3, 1, Values(2), 3) == (1, 2, 3)]
# Multiple Values expand in left-to-right order.
def f4(a, b, c, d):
return (a, b, c, d)
test[call(f4, Values(1, 2), Values(3, 4)) == (1, 2, 3, 4)]
test[call(f4, Values(1), 2, Values(3, 4)) == (1, 2, 3, 4)]
# Trailing positional and keyword args merge after the Values.
def f5(a, b, c, x, y):
return (a, b, c, x, y)
test[call(f5, Values(1, 2, x=10), 3, y=20) == (1, 2, 3, 10, 20)]
# Rightmost wins per unique keyword name.
# Explicit kwargs override Values.kwrets:
test[call(f5, Values(1, 2, x=10), 3, x=99, y=20) == (1, 2, 3, 99, 20)]
# Among multiple Values, the later one's kwrets override the earlier:
def fkw(a, b, *, x):
return (a, b, x)
test[call(fkw, Values(1, x=10), Values(2, x=20)) == (1, 2, 20)]
# callwith: same rules, applied at definition time.
test[callwith(Values(2, 3))(add) == 5]
test[callwith(Values(1, 2, x=3))(lambda a, b, x: (a, b, x)) == (1, 2, 3)]
test[callwith(1, Values(2, 3))(f3) == (1, 2, 3)]
test[callwith(Values(1, 2), Values(3, 4))(f4) == (1, 2, 3, 4)]
test[callwith(Values(1, 2, x=10), 3, y=20)(f5) == (1, 2, 3, 10, 20)]
test[callwith(Values(1, 2, x=10), 3, x=99, y=20)(f5) == (1, 2, 3, 99, 20)]
test[callwith(Values(1, x=10), Values(2, x=20))(fkw) == (1, 2, 20)]
# The `Values` abstraction is used by various parts of `unpythonic` that
# deal with function composition; particularly `curry`, the `compose` and
# `pipe` families, and the `with continuations` macro.
with testset("Values (multiple-return-values, named return values)"):
def f():
return Values(1, 2, 3)
result = f()
test[isinstance(result, Values)]
test[result.rets == (1, 2, 3)]
test[not result.kwrets]
test[result[0] == 1]
test[result[:-1] == (1, 2)]
a, b, c = result # if no kwrets, can be unpacked like a tuple
a, b, c = f()
def g():
return Values(x=3) # named return value
result = g()
test[isinstance(result, Values)]
test[not result.rets]
test[result.kwrets == {"x": 3}] # actually a `frozendict`
test["x" in result] # `in` looks in the named part
test[result["x"] == 3]
test[result.get("x", None) == 3]
test[result.get("y", None) is None]
test[tuple(result.keys()) == ("x",)] # also `values()`, `items()`
def h():
return Values(1, 2, x=3)
result = h()
test[isinstance(result, Values)]
test[result.rets == (1, 2)]
test[result.kwrets == {"x": 3}]
a, b = result.rets # positionals can always be unpacked explicitly
test[result[0] == 1]
test["x" in result]
test[result["x"] == 3]
def silly_but_legal():
return Values(42)
result = silly_but_legal()
test[result.rets[0] == 42]
test[result.ret == 42] # shorthand for single-value case
with testset("valuify (convert tuple as multiple-return-values into Values)"):
@valuify
def f(x, y, z):
return x, y, z
test[isinstance(f(1, 2, 3), Values)]
test[f(1, 2, 3) == Values(1, 2, 3)]
if __name__ == '__main__': # pragma: no cover
with session(__file__):
runtests()