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 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
|
from __future__ import annotations
from typing import Callable, SupportsFloat
from gaphas.types import TypedProperty
# epsilon for float comparison
# is simple abs(x - y) > EPSILON enough for canvas needs?
EPSILON = 1e-6
# Variable Strengths:
VERY_WEAK = 0
WEAK = 10
NORMAL = 20
STRONG = 30
VERY_STRONG = 40
REQUIRED = 100
class variable:
"""Easy-to-use drop Variable descriptor.
>>> class A(object):
... x = variable(varname='_v_x')
... y = variable(STRONG)
... def __init__(self):
... self.x = 12
>>> a = A()
>>> a.x
Variable(12, 20)
>>> a._v_x
Variable(12, 20)
>>> a.x = 3
>>> a.x
Variable(3, 20)
>>> a.y
Variable(0, 30)
"""
def __init__(self, strength=NORMAL, varname=None):
self._strength = strength
self._varname = varname or f"_variable_{id(self)}"
def __get__(self, obj, class_=None):
if not obj:
return self
try:
return getattr(obj, self._varname)
except AttributeError:
setattr(obj, self._varname, Variable(strength=self._strength))
return getattr(obj, self._varname)
def __set__(self, obj, value):
try:
getattr(obj, self._varname).value = float(value)
except AttributeError:
v = Variable(strength=self._strength)
setattr(obj, self._varname, v)
v.value = value
class Variable:
"""Representation of a variable in the constraint solver.
Each Variable has a ``value`` and a ``strength``. In a constraint the weakest
variables are changed.
You can even do some calculating with it. The Variable always represents a
float variable.
The ``variable`` decorator can be used to easily define variables in classes.
"""
def __init__(self, value: SupportsFloat = 0.0, strength: int = NORMAL):
self._value = float(value)
self._strength = strength
self._handlers: set[Callable[[Variable, float], None]] = set()
def add_handler(self, handler: Callable[[Variable, float], None]) -> None:
"""Add a handler, to be invoked when the value changes."""
self._handlers.add(handler)
def remove_handler(self, handler: Callable[[Variable, float], None]) -> None:
"""Remove a handler."""
self._handlers.discard(handler)
def notify(self, old: float) -> None:
"""Notify all handlers."""
for handler in self._handlers:
handler(self, old)
@property
def strength(self) -> int:
"""Strength."""
return self._strength
def dirty(self) -> None:
"""Mark the variable dirty in all attached constraints.
Variables are marked dirty also during constraints solving to
solve all dependent constraints, i.e. two equals constraints
between 3 variables.
"""
self.notify(self._value)
def set_value(self, value: SupportsFloat) -> None:
oldval = self._value
v = float(value)
if abs(oldval - v) > EPSILON:
self._value = v
self.notify(oldval)
value: TypedProperty[float, SupportsFloat]
value = property(lambda s: s._value, set_value)
def __str__(self):
return f"Variable({self._value:g}, {self._strength:d})"
__repr__ = __str__
def __float__(self):
return float(self._value)
def __eq__(self, other):
"""
>>> Variable(5) == 5
True
>>> Variable(5) == 4
False
>>> Variable(5) != 5
False
"""
return abs(self._value - other) < EPSILON
def __ne__(self, other):
"""
>>> Variable(5) != 4
True
>>> Variable(5) != 5
False
"""
return abs(self._value - other) > EPSILON
def __gt__(self, other):
"""
>>> Variable(5) > 4
True
>>> Variable(5) > 5
False
"""
return self._value.__gt__(float(other))
def __lt__(self, other):
"""
>>> Variable(5) < 4
False
>>> Variable(5) < 6
True
"""
return self._value.__lt__(float(other))
def __ge__(self, other):
"""
>>> Variable(5) >= 5
True
"""
return self._value.__ge__(float(other))
def __le__(self, other):
"""
>>> Variable(5) <= 5
True
"""
return self._value.__le__(float(other))
def __add__(self, other):
"""
>>> Variable(5) + 4
9.0
"""
return self._value.__add__(float(other))
def __sub__(self, other):
"""
>>> Variable(5) - 4
1.0
>>> Variable(5) - Variable(4)
1.0
"""
return self._value.__sub__(float(other))
def __mul__(self, other):
"""
>>> Variable(5) * 4
20.0
>>> Variable(5) * Variable(4)
20.0
"""
return self._value.__mul__(float(other))
def __floordiv__(self, other):
"""
>>> Variable(21) // 4
5.0
>>> Variable(21) // Variable(4)
5.0
"""
return self._value.__floordiv__(float(other))
def __mod__(self, other):
"""
>>> Variable(5) % 4
1.0
>>> Variable(5) % Variable(4)
1.0
"""
return self._value.__mod__(float(other))
def __divmod__(self, other):
"""
>>> divmod(Variable(21), 4)
(5.0, 1.0)
>>> divmod(Variable(21), Variable(4))
(5.0, 1.0)
"""
return self._value.__divmod__(float(other))
def __pow__(self, other):
"""
>>> pow(Variable(5), 4)
625.0
>>> pow(Variable(5), Variable(4))
625.0
"""
return self._value.__pow__(float(other))
def __truediv__(self, other):
"""
>>> Variable(5) / 4.
1.25
>>> Variable(5) / Variable(4)
1.25
>>> Variable(5.) / 4
1.25
>>> 10 / Variable(5.)
2.0
"""
return self._value.__truediv__(float(other))
# .. And the other way around:
def __radd__(self, other):
"""
>>> 4 + Variable(5)
9.0
>>> Variable(4) + Variable(5)
9.0
"""
return self._value.__radd__(float(other))
def __rsub__(self, other):
"""
>>> 6 - Variable(5)
1.0
"""
return self._value.__rsub__(other)
def __rmul__(self, other):
"""
>>> 4 * Variable(5)
20.0
"""
return self._value.__rmul__(other)
def __rfloordiv__(self, other):
"""
>>> 21 // Variable(4)
5.0
"""
return self._value.__rfloordiv__(other)
def __rmod__(self, other):
"""
>>> 5 % Variable(4)
1.0
"""
return self._value.__rmod__(other)
def __rdivmod__(self, other):
"""
>>> divmod(21, Variable(4))
(5.0, 1.0)
"""
return self._value.__rdivmod__(other)
def __rpow__(self, other):
"""
>>> pow(4, Variable(5))
1024.0
"""
return self._value.__rpow__(other)
def __rtruediv__(self, other):
"""
>>> 5 / Variable(4.)
1.25
>>> 5. / Variable(4)
1.25
"""
return self._value.__rtruediv__(other)
|