From af311882cc2e24019856499ec93b0bbd68c51a95 Mon Sep 17 00:00:00 2001 From: Bruce Flynn <brucef@ssec.wisc.edu> Date: Wed, 14 Oct 2015 20:48:35 +0000 Subject: [PATCH] Have stream info include first, last packet --- README | 3 +++ edosl0util/cli.py | 3 ++- edosl0util/stream.py | 17 +++++++++++++++-- 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/README b/README index 4eb72cf..9051216 100644 --- a/README +++ b/README @@ -22,6 +22,9 @@ parsing use the ``PacketStream`` class as an iterator. <Packet apid=826 seqid=13072 stamp=2015-10-13 00:22:04.042798> >>> packets = list(stream) >>> first, last, info = stream.info() + >>> print("First:{}\nLast:{}".format(first, last)) + First:2015-10-13 00:22:04.042798 + Last:2015-10-13 02:04:58.107582 >>> print info defaultdict(<function edosl0util.stream.<lambda>>, {800: {'count': 32351, 'last_seqid': 16043, 'num_missing': 0}, diff --git a/edosl0util/cli.py b/edosl0util/cli.py index 262c667..51059c4 100644 --- a/edosl0util/cli.py +++ b/edosl0util/cli.py @@ -80,9 +80,10 @@ def cmd_info(): first = min(packet.stamp, first) last = max(packet.stamp, last) total = 0 + first, last, info = packets.info() LOG.info("First: %s", first) LOG.info("Last: %s", last) - for apid, dat in packets.info().items(): + for apid, dat in info.items(): total += dat['count'] LOG.info("%d: count=%d missing=%d", apid, dat['count'], dat['num_missing']) LOG.info("{} total packets".format(total)) diff --git a/edosl0util/stream.py b/edosl0util/stream.py index bb5bda6..f06ac84 100644 --- a/edosl0util/stream.py +++ b/edosl0util/stream.py @@ -170,8 +170,12 @@ class PacketStream(object): """ self._stream = jpss_full_stream(fobj) self._seek_cache = deque() + self._first = None + self._last = None self._apid_info = defaultdict( - lambda: {'count': 0, 'last_seqid': self.SEQID_NOTSET, 'num_missing': 0}) + lambda: {'count': 0, + 'last_seqid': self.SEQID_NOTSET, + 'num_missing': 0}) self._fail_on_missing = fail_on_missing def __iter__(self): @@ -199,6 +203,15 @@ class PacketStream(object): def _update_info(self, packet): have_missing = False apid = self._apid_info[packet.apid] + if packet.stamp: + if self._first is None: + self._first = packet.stamp + else: + self._first = min(packet.stamp, self._first) + if self._last is None: + self._last = packet.stamp + else: + self._last = max(packet.stamp, self._last) apid['count'] += 1 if apid['last_seqid'] != self.SEQID_NOTSET: if packet.seqid > apid['last_seqid']: @@ -211,7 +224,7 @@ class PacketStream(object): return have_missing def info(self): - return self._apid_info + return self._first, self._last, self._apid_info def seek_to(self, stamp, apid=None): """ -- GitLab