From 3dd08c5f7e5422e7aa133a672b412a7d30acc0d5 Mon Sep 17 00:00:00 2001 From: Théo de la Hogue Date: Wed, 6 Apr 2022 17:50:48 +0200 Subject: Adding append function and improving pop_first_until function of TimeStampedBuffer --- src/argaze/DataStructures.py | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) (limited to 'src') diff --git a/src/argaze/DataStructures.py b/src/argaze/DataStructures.py index 5fe7ce6..7409192 100644 --- a/src/argaze/DataStructures.py +++ b/src/argaze/DataStructures.py @@ -60,16 +60,29 @@ class TimeStampedBuffer(collections.OrderedDict): def __str__(self): return json.dumps(self, default = vars) + def append(self, timestamped_buffer): + """Append a timestamped buffer.""" + + for ts, value in timestamped_buffer.items(): + self[ts] = value + def pop_first(self): """Easing FIFO access mode""" return self.popitem(last=False) def pop_first_until(self, ts): - """Pop all item until a given timestamped value and returning the last poped item""" + """Pop all item until a given timestamped value and return the last poped item""" + + # get last timestamp before given timestamp + earliest_ts = self.get_last_before(ts) + + # when no timestamped have been found + if earliest_ts == None: + raise ValueError popep_ts, poped_value = self.pop_first() - while popep_ts != ts: + while popep_ts != earliest_ts: popep_ts, poped_value = self.pop_first() return popep_ts, poped_value @@ -78,16 +91,6 @@ class TimeStampedBuffer(collections.OrderedDict): """Easing FIFO access mode""" return self.popitem(last=True) - def pop_last_until(self, ts): - """Pop all item until a given timestamped value and returning the last poped item""" - - popep_ts, poped_value = self.pop_last() - - while popep_ts != ts: - popep_ts, poped_value = self.pop_last() - - return popep_ts, poped_value - def get_last_before(self, ts): """Retreive last item timestamp before a given timestamp value.""" @@ -108,4 +111,6 @@ class TimeStampedBuffer(collections.OrderedDict): with open(filepath, 'w', encoding='utf-8') as jsonfile: json.dump(self, jsonfile, ensure_ascii = False, default=vars) except: - raise RuntimeError(f'Can\' write {filepath}') \ No newline at end of file + raise RuntimeError(f'Can\' write {filepath}') + + -- cgit v1.1