summaryrefslogtreecommitdiff
path: root/gnuradio-runtime/python
diff options
context:
space:
mode:
authorJohnathan Corgan <johnathan@corganlabs.com>2015-04-26 15:57:43 -0700
committerJohnathan Corgan <johnathan@corganlabs.com>2015-04-26 15:57:43 -0700
commit10c710479a88cf8c6f4fe383541cab63e7139b08 (patch)
tree94fba75c140b7bf0a4dcb23c3340e8dfa5915898 /gnuradio-runtime/python
parentcfd73d7d8fe273bcb3a04a344f657173fcd5b824 (diff)
parent3b1b6d3fd6faa7bd1cbeb8b0ebfe42e6b9196bf7 (diff)
Merge remote-tracking branch 'nowls/burst_shaping'
Diffstat (limited to 'gnuradio-runtime/python')
-rwxr-xr-xgnuradio-runtime/python/gnuradio/gr/qa_tag_utils.py32
-rw-r--r--gnuradio-runtime/python/gnuradio/gr/tag_utils.py33
2 files changed, 65 insertions, 0 deletions
diff --git a/gnuradio-runtime/python/gnuradio/gr/qa_tag_utils.py b/gnuradio-runtime/python/gnuradio/gr/qa_tag_utils.py
index 1a08ac5ae6..55b62a12ac 100755
--- a/gnuradio-runtime/python/gnuradio/gr/qa_tag_utils.py
+++ b/gnuradio-runtime/python/gnuradio/gr/qa_tag_utils.py
@@ -76,6 +76,38 @@ class test_tag_utils (gr_unittest.TestCase):
self.assertTrue(pmt.equal(t_tuple.value, value))
self.assertEqual(t_tuple.offset, offset)
+ def test_003(self):
+ offsets = (6, 3, 8)
+ key = pmt.string_to_symbol('key')
+ srcid = pmt.string_to_symbol('qa_tag_utils')
+ tags = []
+
+ for k in offsets:
+ t = gr.tag_t()
+ t.offset = k
+ t.key = key
+ t.value = pmt.from_long(k)
+ t.srcid = srcid
+ tags.append(t)
+
+ for k, t in zip(sorted(offsets),
+ sorted(tags, key=gr.tag_t_offset_compare_key())):
+ self.assertEqual(t.offset, k)
+ self.assertTrue(pmt.equal(t.key, key))
+ self.assertTrue(pmt.equal(t.value, pmt.from_long(k)))
+ self.assertTrue(pmt.equal(t.srcid, srcid))
+
+ tmin = min(tags, key=gr.tag_t_offset_compare_key())
+ self.assertEqual(tmin.offset, min(offsets))
+ self.assertTrue(pmt.equal(tmin.key, key))
+ self.assertTrue(pmt.equal(tmin.value, pmt.from_long(min(offsets))))
+ self.assertTrue(pmt.equal(tmin.srcid, srcid))
+
+ tmax = max(tags, key=gr.tag_t_offset_compare_key())
+ self.assertEqual(tmax.offset, max(offsets))
+ self.assertTrue(pmt.equal(tmax.key, key))
+ self.assertTrue(pmt.equal(tmax.value, pmt.from_long(max(offsets))))
+ self.assertTrue(pmt.equal(tmax.srcid, srcid))
if __name__ == '__main__':
diff --git a/gnuradio-runtime/python/gnuradio/gr/tag_utils.py b/gnuradio-runtime/python/gnuradio/gr/tag_utils.py
index dc36e05250..a7745428c7 100644
--- a/gnuradio-runtime/python/gnuradio/gr/tag_utils.py
+++ b/gnuradio-runtime/python/gnuradio/gr/tag_utils.py
@@ -108,3 +108,36 @@ def python_to_tag(tag_struct):
return tag
else:
return None
+
+def tag_t_offset_compare_key():
+ """
+ Convert a tag_t_offset_compare function into a key=function
+ This method is modeled after functools.cmp_to_key(_func_).
+ It can be used by functions that accept a key function, such as
+ sorted(), min(), max(), etc. to compare tags by their offsets,
+ e.g., sorted(tag_list, key=gr.tag_t_offset_compare_key()).
+ """
+ class K(object):
+ def __init__(self, obj, *args):
+ self.obj = obj
+ def __lt__(self, other):
+ # x.offset < y.offset
+ return gr.tag_t_offset_compare(self.obj, other.obj)
+ def __gt__(self, other):
+ # y.offset < x.offset
+ return gr.tag_t_offset_compare(other.obj, self.obj)
+ def __eq__(self, other):
+ # not (x.offset < y.offset) and not (y.offset < x.offset)
+ return not gr.tag_t_offset_compare(self.obj, other.obj) and \
+ not gr.tag_t_offset_compare(other.obj, self.obj)
+ def __le__(self, other):
+ # not (y.offset < x.offset)
+ return not gr.tag_t_offset_compare(other.obj, self.obj)
+ def __ge__(self, other):
+ # not (x.offset < y.offset)
+ return not gr.tag_t_offset_compare(self.obj, other.obj)
+ def __ne__(self, other):
+ # (x.offset < y.offset) or (y.offset < x.offset)
+ return gr.tag_t_offset_compare(self.obj, other.obj) or \
+ gr.tag_t_offset_compare(other.obj, self.obj)
+ return K