Repository navigation
Expand file tree
/
Copy pathtest_misc.py
More file actions
178 lines (150 loc) · 5.19 KB
/
Copy pathtest_misc.py
File metadata and controls
178 lines (150 loc) · 5.19 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
# -*- coding: utf-8 -*-
from operator import add
from functools import partial
from collections import deque
from sys import float_info
from ..misc import call, callwith, raisef, pack, namelambda, timer, getattrrec, setattrrec, Popper, ulp
from ..fun import withself
def test():
# def as a code block (function overwritten by return value)
#
@call
def result():
return "hello"
assert 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
assert 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
assert result == (6, 7)
# can also be used normally
assert call(add, 2, 3) == add(2, 3)
# to pass arguments when used as decorator, use @callwith instead
@callwith(3)
def result(x):
return x**2
assert 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
assert apply23(myadd) == 5
assert apply23(mymul) == 6
# callwith is not essential; we can do the same pythonically like this:
a = [2, 3]
assert myadd(*a) == 5
assert 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
assert apply234(add3) == 9
assert apply234(mul3) == 24
# pythonic solution:
a = [2]
a += [3]
a += [4]
assert add3(*a) == 9
assert 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)])
assert 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)])
assert tuple(m) == (6, 9, 3**(1/2))
l = lambda: raisef(ValueError, "all ok")
try:
l()
except ValueError:
pass
else:
assert False
myzip = lambda lol: map(pack, *lol)
lol = ((1, 2), (3, 4), (5, 6))
assert tuple(myzip(lol)) == ((1, 3, 5), (2, 4, 6))
square = lambda x: x**2
assert square.__code__.co_name == "<lambda>"
assert square.__name__ == "<lambda>"
assert square.__qualname__ == "test.<locals>.<lambda>"
square = namelambda("square")(square)
assert square.__code__.co_name == "square"
assert square.__name__ == "square"
assert square.__qualname__ == "test.<locals>.square"
# CAUTION: in case of nested lambdas, the inner doesn't see the outer's new name:
nested = namelambda("outer")(lambda: namelambda("inner")(withself(lambda self: self)))
assert nested.__qualname__ == "test.<locals>.outer"
assert nested().__qualname__ == "test.<locals>.<lambda>.<locals>.inner"
# simple performance timer as a context manager
with timer() as tictoc:
for _ in range(int(1e6)):
pass
assert tictoc.dt > 0 # elapsed time in seconds (float)
with timer(p=True): # auto-print mode for convenience
for _ in range(int(1e6)):
pass
# access underlying data in an onion of wrappers
class Wrapper:
def __init__(self, x):
self.x = x
w = Wrapper(Wrapper(42))
assert type(getattr(w, "x")) == Wrapper
assert type(getattrrec(w, "x")) == int
assert getattrrec(w, "x") == 42
setattrrec(w, "x", 23)
assert type(getattr(w, "x")) == Wrapper
assert type(getattrrec(w, "x")) == int
assert getattrrec(w, "x") == 23
# pop-while iterator
inp = deque(range(5)) # efficiency: deque can popleft() in O(1) time
out = []
for x in Popper(inp):
out.append(x)
assert inp == deque([])
assert out == list(range(5))
inp = deque(range(3))
out = []
for x in Popper(inp):
out.append(x)
if x < 10:
inp.appendleft(x + 10)
assert inp == deque([])
assert out == [0, 10, 1, 11, 2, 12]
# works for a list, too, although not efficient (pop(0) takes O(n) time)
inp = list(range(5))
out = []
for x in Popper(inp):
out.append(x)
assert inp == []
assert out == list(range(5))
# Unit in the Last Place, float utility
# https://en.wikipedia.org/wiki/Unit_in_the_last_place
eps = float_info.epsilon
assert ulp(1.0) == eps
# test also at some base-2 exponent switch points
assert ulp(2.0) == 2 * eps
assert ulp(0.5) == 0.5 * eps
print("All tests PASSED")
if __name__ == '__main__':
test()