forked from nedbat/flourish
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparameter.py
181 lines (157 loc) · 4.69 KB
/
parameter.py
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
"""
Parameterized curves
"""
import contextlib
import contextvars
import dataclasses
from dataclasses import dataclass
class Parameter:
"""
A parameter for a curve.
All values must be int or float.
"""
def __init__(
self,
name,
key,
default,
places=1,
scale=1.0,
adjacent_step=None,
adjacent=None,
random=None,
to_short=None,
from_short=None,
):
self.name = name
self.key = key
self.default = default
self.places = places
self.scale = scale
self.adjacent_step = adjacent_step
self.random = random
if adjacent is not None:
self.adjacent = adjacent
if to_short is not None:
self.to_short = to_short
if from_short is not None:
self.from_short = from_short
def adjacent(self, v):
"""
Return a list of adjacent values for this parameter.
"""
d = self.adjacent_step
if d is None:
return []
else:
return [v - 2 * d, v - d, v + d, v + 2 * d]
def to_short(self, v):
"""
Convert the value to a short representation, which must be a string of an int.
"""
if isinstance(self.default, float):
return str(int(v / self.scale * 10**self.places))
else:
return str(v)
def from_short(self, s):
"""
Convert from a short representation, once produced by `to_short`.
"""
if isinstance(self.default, float):
return float(s) / 10**self.places * self.scale
else:
return int(s)
def repr(self, v):
if isinstance(self.default, float):
return format(v, f".{self.places}f")
else:
return repr(v)
@dataclass
class Parameterized:
"""
A parameterized thing (probably a function).
"""
# The name will be used to differentiate between multiple instances used
# together, like an x wave and a y wave.
name: str
@classmethod
def paramdefs(cls):
"""
Get the fields that are Parameters.
"""
for field in dataclasses.fields(cls):
if isinstance(field.type, Parameter):
yield field
@classmethod
def make_random(cls, name, rnd):
"""
Use the Random object `rnd` to make an instance with randomized Parameters.
"""
kwargs = {}
for field in cls.paramdefs():
if field.type.random:
val = field.type.random(rnd)
else:
val = field.type.default
kwargs[field.name] = val
return cls(name=name, **kwargs)
@classmethod
def from_params(cls, name, params):
"""
Make an instance using the params dict for Parameter values.
"""
kwargs = {}
for field in cls.paramdefs():
key = name + field.type.key
if key in params:
val = params[key]
else:
val = field.type.default
kwargs[field.name] = val
return cls(name=name, **kwargs)
@classmethod
def from_short_params(cls, name, params):
"""
Make an instance using the params dict for Parameter short values.
"""
kwargs = {}
for field in cls.paramdefs():
key = name + field.type.key
if key in params:
val = field.type.from_short(params[key])
else:
val = field.type.default
kwargs[field.name] = val
return cls(name=name, **kwargs)
def param_things(self):
"""
Produce all your things with parameters, and their extra-name if
they are extra.
"""
yield self, None
def parameters(self):
for thing, extra_name in self.param_things():
for field in thing.paramdefs():
yield (field, thing, extra_name, getattr(thing, field.name))
def short_parameters(self):
"""
Return a dict of short parameters for all of the Parameters.
"""
shorts = {}
for thing, _ in self.param_things():
for field in thing.paramdefs():
key = thing.name + field.type.key
val = getattr(thing, field.name)
if field.type.to_short:
val = field.type.to_short(val)
shorts[key] = str(val)
return shorts
## Pseudo-global parameters, like thread locals.
GlobalParameter = contextvars.ContextVar
@contextlib.contextmanager
def global_value(cvar, val):
token = cvar.set(val)
try:
yield
finally:
cvar.reset(token)