Module src.util.intervaled_value
Classes
class IntervaledValue (value: float, interval_size: float, normalized_value: float = 0.5)-
Intervaled Value
Holds an updateable value on a static interval. This class is for continuosly updating a value, on some static interval that is set on instantiation.
Example usage:
iv = IntervaledValue(value=3, interval_size=10, normalized_value=0.3)
iv.set_value(100)
iv.get_value() -> 10.0
iv.get_normalized_value() -> 1.0The class will instantiate an interval of specified size, around the given value. The value is placed on the interval as per
normalized_value.Example usage: IntervaledValue(value=3, interval_size=10, normalized_value=0.3) will instantiate an object that holds the value 3.0 on the range [0, 10].
Args
value- The initial value to be held.
interval_size- Size of the interval.
normalized_value- A normalized representation of
value.
Static methods
def fit_to_interval(value: float, lower_bound: float, upper_bound: float) ‑> float-
Maps a value from < inf, inf > -> [lower_bound, upper_bound]
If value is already on the interval [lower_bound, upper_bound], then this function returns the same value.
If the value is outside the interval, it returns the lower or upper bound, corresponding to (value < lower_bound) or (value > upper_bound) respectively.
Example:
fit_to_interval(-1, 0, 1) -> 0
Methods
def set_value(self, value: float, invert=False) ‑> NoneType-
Sets the held value inside the static interval.
Args
value- A new value to update the held value with. If the value is outside the interval, it sets self.value to the lower or upper bound, corresponding to (value < lower_bound) or (value > upper_bound) respectively.
invert- If True, inverts the given value on the interval.
def get_normalized_value(self, invert=False) ‑> float-
A normalized representation of the held value.
Args
invert- If True, the inverts the return value, i.e 0.25 -> 0.75
Returns
A normalized ([0.0, 1.0]) representation of the held value.
def get_value(self, invert=False) ‑> float-
Gets the held value.
Args
invert- If True, inverts the value on the interval.
Returns
The held value (is contained on the interval).