Repository navigation
Expand file tree
/
Copy pathfunctionUtils.py
More file actions
126 lines (95 loc) · 2.82 KB
/
Copy pathfunctionUtils.py
File metadata and controls
126 lines (95 loc) · 2.82 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
import time
import numpy as np
def TIMED(function):
def wrapper(*args, **kwargs):
T = time.time()
result = function(*args, **kwargs)
T = time.time() - T
print(">>>function {} timed {}".format(function.__name__, T))
return result
return wrapper
def DELAYED(delay):
def decorate(function):
def wrapper(*args, **kwargs):
result = function(*args, **kwargs)
time.sleep(delay)
return result
return wrapper
return decorate
def NOISY(noise):
def decorate(function):
def wrapper(*args, **kwargs):
result = function(*args, **kwargs)
result+=np.random.randn()*noise
return result
return wrapper
return decorate
def FLOAT32(function):
def wrapper(*args, **kwargs):
args = [np.float32(a) for a in args]
kwargs = {a: np.float32(kwargs[a]) for a in kwargs}
result = function(*args, **kwargs)
result = np.float32(result)
return result
return wrapper
def RESTRICT_INVOCATIONS(n):
def decorate(function):
function.__n = n
def wrapper(*args, **kwargs):
function.__n -= 1
if function.__n < 0:
raise EOFError("Exceeded the maximal number of invocations.")
result = function(*args, **kwargs)
return result
return wrapper
return decorate
class AbstractShape:
"""
An abstract class that represents a closed shape.
"""
def __init__(self):
raise NotImplementedError()
pass
def sample(self):
"""
Returns
-------
A random point (x,y) on the shape contour.
"""
raise NotImplementedError()
return (0, 0)
def contour(self, n: int):
"""
This method is used to draw the shape contour. It returns an array of
consecutive points on the shape contour. Larger n results in smoother
shapes.
Parameters
----------
n : int
the number of points on the shape contour to return.
Returns
-------
np.ndarray((n,2))
An array of consecutive points on the shape contour.
"""
raise NotImplementedError()
return np.ndarray((n, 2), dtype=np.float32)
def area(self) -> np.float32:
"""
Returns
-------
float
The area of the shape.
"""
raise NotImplementedError()
return 1.0
@RESTRICT_INVOCATIONS(1)
@FLOAT32
@TIMED
@NOISY(0.1)
@DELAYED(delay=3)
def plus(a, b):
return a + b
if __name__ == "__main__":
print(plus(3, 1.0 / 11))
print(plus(3, 1.0 / 11))