From 98f980c7b52fd3ae6e6b91fc83915d3e6d7c5f36 Mon Sep 17 00:00:00 2001 From: Rob Hallam <0504004h@student.gla.ac.uk> Date: Fri, 19 Jul 2024 11:13:38 +0100 Subject: [PATCH] feat: [Interval] add inst. & move time methods These are useful for changing Intervals (eg consolitation) --- pipeline/utils.py | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/pipeline/utils.py b/pipeline/utils.py index 0e90f0a..c3331be 100644 --- a/pipeline/utils.py +++ b/pipeline/utils.py @@ -126,8 +126,55 @@ class Interval(): self.end = end self.duration = 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 def from_start(cls, start=None): """Create an interval from a start time using the 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 +