Representation of a duration with vagueness.
Represents a duration identified by 2 float values:
- the duration value;
- the duration margin.
Representation of a duration with vagueness.
Represents a duration identified by 2 float values:
Create a new sppasDuration instance.
def __init__(self, value, vagueness=0.0):
"""Create a new sppasDuration instance.
:param value: (float) value of the duration.
:param vagueness: (float) represents the vagueness of the value.
"""
self.__value = 0.0
self.__margin = 0.0
self.set_value(value)
self.set_margin(vagueness)
Return myself.
def get(self):
"""Return myself."""
return self
Set the value/vagueness of another sppasDuration instance.
def set(self, other):
"""Set the value/vagueness of another sppasDuration instance.
:param other: (sppasDuration)
"""
if isinstance(other, sppasDuration) is False:
raise AnnDataTypeError(other, 'sppasDuration')
self.__value = other.get_value()
self.__margin = other.get_margin()
Return the duration value (float).
def get_value(self):
"""Return the duration value (float)."""
return self.__value
Set the duration to a new value.
def set_value(self, value):
"""Set the duration to a new value.
:param value: (float) the new duration value.
"""
try:
self.__value = float(value)
if self.__value < 0.0:
self.__value = 0.0
raise AnnDataNegValueError(value)
except TypeError:
raise AnnDataTypeError(value, 'float')
Return the vagueness of the duration (float).
def get_margin(self):
"""Return the vagueness of the duration (float)."""
return self.__margin
Fix the vagueness margin of the duration.
def set_margin(self, vagueness):
"""Fix the vagueness margin of the duration.
:param vagueness: (float) the duration margin.
"""
try:
self.__margin = float(vagueness)
if self.__margin < 0.0:
self.__margin = 0.0
raise AnnDataNegValueError(vagueness)
except TypeError:
raise AnnDataTypeError(vagueness, 'float')
Return a deep copy of self.
def copy(self):
"""Return a deep copy of self."""
t = self.__value
r = self.__margin
return sppasDuration(t, r)
def __format__(self, fmt):
return str(self).__format__(fmt)
def __repr__(self):
return 'Duration: {:f}, {:f}'.format(self.get_value(), self.get_margin())
def __str__(self):
return '({:f}, {:f})'.format(self.get_value(), self.get_margin())
def __hash__(self):
return hash((self.__value, self.__margin))
Equal is required to use '==' between 2 sppasDuration instances or between a sppasDuration and an other object representing time. This relationship takes into account the vagueness.
def __eq__(self, other):
"""Equal is required to use '==' between 2 sppasDuration instances or
between a sppasDuration and an other object representing time.
This relationship takes into account the vagueness.
:param other: (Duration, float, int) is the other duration to compare with.
"""
if isinstance(other, (int, float, sppasDuration)) is False:
return False
if isinstance(other, sppasDuration) is True:
delta = abs(self.__value - other.get_value())
radius = self.__margin + other.get_margin()
return delta <= radius
if isinstance(other, (int, float)):
delta = abs(self.__value - other)
radius = self.__margin
return delta <= radius
LowerThan is required to use '<' between 2 sppasDuration instances or between a sppasDuration and an other time object.
def __lt__(self, other):
"""LowerThan is required to use '<' between 2 sppasDuration instances
or between a sppasDuration and an other time object.
:param other: (Duration, float, int) is the other duration to compare with.
"""
if isinstance(other, sppasDuration) is True:
return self != other and self.__value < other.get_value()
return self != other and self.__value < other
GreaterThan is required to use '>' between 2 Duration instances or between a Duration and an other time object.
def __gt__(self, other):
"""GreaterThan is required to use '>' between 2 Duration instances
or between a Duration and an other time object.
:param other: (Duration, float, int) is the other duration to compare with.
"""
if isinstance(other, sppasDuration) is True:
return self != other and self.__value > other.get_value()
return self != other and self.__value > other
Not equals.
def __ne__(self, other):
"""Not equals."""
return not self == other
Lesser or equal.
def __le__(self, other):
"""Lesser or equal."""
return self < other or self == other
Greater or equal.
def __ge__(self, other):
"""Greater or equal."""
return self > other or self == other