ソースを参照

feat: [Interval] add inst. & move time methods

These are useful for changing Intervals (eg consolitation)
main
Rob Hallam 3ヶ月前
コミット
98f980c7b5
1個のファイルの変更47行の追加0行の削除
  1. +47
    -0
      pipeline/utils.py

+ 47
- 0
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


読み込み中…
キャンセル
保存