|
@@ -126,8 +126,55 @@ class Interval(): |
|
|
self.end = end |
|
|
self.end = end |
|
|
self.duration = duration |
|
|
self.duration = duration |
|
|
self.start = end - duration |
|
|
self.start = end - duration |
|
|
|
|
|
|
|
|
|
|
|
# set precision |
|
|
|
|
|
self.start = round(self.start, self.DEFAUT_PRECISION) |
|
|
|
|
|
self.end = round(self.end, self.DEFAUT_PRECISION) |
|
|
|
|
|
self.duration = round(self.duration, self.DEFAUT_PRECISION) |
|
|
|
|
|
|
|
|
@classmethod |
|
|
@classmethod |
|
|
def from_start(cls, start=None): |
|
|
def from_start(cls, start=None): |
|
|
"""Create an interval from a start time using the default duration""" |
|
|
"""Create an interval from a start time using the default duration""" |
|
|
return cls(start=start, duration=cls.DEFAULT_DURATION) |
|
|
return cls(start=start, duration=cls.DEFAULT_DURATION) |
|
|
|
|
|
|
|
|
|
|
|
@classmethod |
|
|
|
|
|
def from_end(cls, end=None): |
|
|
|
|
|
"""Create an interval from an end time using the default duration""" |
|
|
|
|
|
return cls(end=end, duration=cls.DEFAULT_DURATION) |
|
|
|
|
|
|
|
|
|
|
|
def __repr__(self): |
|
|
|
|
|
return f"Interval({self.start}, {self.end}, {self.duration})" |
|
|
|
|
|
|
|
|
|
|
|
def __lt__(self, other): |
|
|
|
|
|
if self.start == other.start: |
|
|
|
|
|
return self.end < other.end |
|
|
|
|
|
return self.start < other.start |
|
|
|
|
|
|
|
|
|
|
|
# -------------------------------------------------------------- |
|
|
|
|
|
# TODO: handle bad cases, eg negative duration, start > end, etc |
|
|
|
|
|
# -------------------------------------------------------------- |
|
|
|
|
|
|
|
|
|
|
|
def move_start(self, new_start: float | int, relative: bool = False): |
|
|
|
|
|
"""Update start time of Interval, keeping end time constant (& so modify duration)""" |
|
|
|
|
|
if relative: |
|
|
|
|
|
self.start += new_start |
|
|
|
|
|
else: |
|
|
|
|
|
self.start = new_start |
|
|
|
|
|
self.duration = self.end - self.start |
|
|
|
|
|
|
|
|
|
|
|
def move_end(self, new_end: float | int, relative: bool = False): |
|
|
|
|
|
"""Update end time of Interval, keeping start time constant (& so modify duration)""" |
|
|
|
|
|
if relative: |
|
|
|
|
|
self.end += new_end |
|
|
|
|
|
else: |
|
|
|
|
|
self.end = new_end |
|
|
|
|
|
self.duration = self.end - self.start |
|
|
|
|
|
|
|
|
|
|
|
def update_duration(self, new_duration: float | int, relative: bool = False): |
|
|
|
|
|
"""Update duration of Interval, keeping start time constant (& so modify end time)""" |
|
|
|
|
|
if relative: |
|
|
|
|
|
self.duration += new_duration |
|
|
|
|
|
else: |
|
|
|
|
|
self.duration = new_duration |
|
|
|
|
|
self.end = self.start + self.duration |
|
|
|
|
|
|