-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtest_funutil.py
More file actions
150 lines (130 loc) · 5.12 KB
/
test_funutil.py
File metadata and controls
150 lines (130 loc) · 5.12 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
# -*- 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))]
# 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()