Fix some spurious traithandler bugs
This commit is contained in:
parent
e0717fd07c
commit
fb8403e729
1 changed files with 27 additions and 22 deletions
|
|
@ -521,25 +521,6 @@ class Trait:
|
||||||
f"loaded for {type(self).__name__}."
|
f"loaded for {type(self).__name__}."
|
||||||
)
|
)
|
||||||
|
|
||||||
# Private helper members
|
|
||||||
|
|
||||||
def _enforce_bounds(self, value):
|
|
||||||
"""Ensures that incoming value falls within trait's range."""
|
|
||||||
if self._type in RANGE_TRAITS:
|
|
||||||
if self.min is not None and value <= self.min:
|
|
||||||
return self.min
|
|
||||||
if self._data["max"] == "base" and value >= self.mod + self.base:
|
|
||||||
return self.mod + self.base
|
|
||||||
if self.max is not None and value >= self.max:
|
|
||||||
return self.max
|
|
||||||
return value
|
|
||||||
|
|
||||||
def _mod_base(self):
|
|
||||||
return self._enforce_bounds(self.mod + self.base)
|
|
||||||
|
|
||||||
def _mod_current(self):
|
|
||||||
return self._enforce_bounds(self.mod + self.current)
|
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
"""Debug-friendly representation of this Trait."""
|
"""Debug-friendly representation of this Trait."""
|
||||||
return "{}({{{}}})".format(
|
return "{}({{{}}})".format(
|
||||||
|
|
@ -604,6 +585,20 @@ class Trait:
|
||||||
if key in self._data["extra_properties"]:
|
if key in self._data["extra_properties"]:
|
||||||
del self._data["extra_properties"][key]
|
del self._data["extra_properties"][key]
|
||||||
|
|
||||||
|
# Limiting the value to set
|
||||||
|
|
||||||
|
def _enforce_bounds(self, value):
|
||||||
|
"""Ensures that incoming value falls within trait's range."""
|
||||||
|
return value
|
||||||
|
|
||||||
|
def _mod_base(self):
|
||||||
|
"""Calculate adding base and modifications"""
|
||||||
|
return self._enforce_bounds(self.mod + self.base)
|
||||||
|
|
||||||
|
def _mod_current(self):
|
||||||
|
"""Calculate the current value"""
|
||||||
|
return self._enforce_bounds(self.mod + self.current)
|
||||||
|
|
||||||
# Numeric operations
|
# Numeric operations
|
||||||
|
|
||||||
def __eq__(self, other):
|
def __eq__(self, other):
|
||||||
|
|
@ -811,7 +806,7 @@ class StaticTrait(Trait):
|
||||||
@current.setter
|
@current.setter
|
||||||
def current(self, value):
|
def current(self, value):
|
||||||
"""Current == base for Static Traits."""
|
"""Current == base for Static Traits."""
|
||||||
self.base = self.current = value
|
self.base = value
|
||||||
|
|
||||||
def reset(self):
|
def reset(self):
|
||||||
raise TraitException(f"Cannot reset static Trait {self.key}.")
|
raise TraitException(f"Cannot reset static Trait {self.key}.")
|
||||||
|
|
@ -825,6 +820,16 @@ class CounterTrait(Trait):
|
||||||
|
|
||||||
trait_type = "counter"
|
trait_type = "counter"
|
||||||
|
|
||||||
|
def _enforce_bounds(self, value):
|
||||||
|
"""Ensures that incoming value falls within trait's range."""
|
||||||
|
if self.min is not None and value <= self.min:
|
||||||
|
return self.min
|
||||||
|
if self._data["max_value"] == "base" and value >= self.mod + self.base:
|
||||||
|
return self.mod + self.base
|
||||||
|
if self.max is not None and value >= self.max:
|
||||||
|
return self.max
|
||||||
|
return value
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def actual(self):
|
def actual(self):
|
||||||
"The actual value of the Trait"
|
"The actual value of the Trait"
|
||||||
|
|
@ -838,9 +843,9 @@ class CounterTrait(Trait):
|
||||||
@min.setter
|
@min.setter
|
||||||
def min(self, amount):
|
def min(self, amount):
|
||||||
if amount is None:
|
if amount is None:
|
||||||
self._data["min"] = amount
|
self._data["min_value"] = amount
|
||||||
elif type(amount) in (int, float):
|
elif type(amount) in (int, float):
|
||||||
self._data["min"] = amount if amount < self.base else self.base
|
self._data["min_value"] = amount if amount < self.base else self.base
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def max(self):
|
def max(self):
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue