diff options
author | Théo de la Hogue | 2022-04-06 17:50:48 +0200 |
---|---|---|
committer | Théo de la Hogue | 2022-04-06 17:50:48 +0200 |
commit | 3dd08c5f7e5422e7aa133a672b412a7d30acc0d5 (patch) | |
tree | 373f4de80715c1bcec76493d559bc92f289b429c | |
parent | dcf754a016eb2e22cbfa4d3d2ee01b5d919138c3 (diff) | |
download | argaze-3dd08c5f7e5422e7aa133a672b412a7d30acc0d5.zip argaze-3dd08c5f7e5422e7aa133a672b412a7d30acc0d5.tar.gz argaze-3dd08c5f7e5422e7aa133a672b412a7d30acc0d5.tar.bz2 argaze-3dd08c5f7e5422e7aa133a672b412a7d30acc0d5.tar.xz |
Adding append function and improving pop_first_until function of TimeStampedBuffer
-rw-r--r-- | src/argaze/DataStructures.py | 31 |
1 files changed, 18 insertions, 13 deletions
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}') + + |