From b8184e6cfa099c921350d7b05d0695d9d3296fb8 Mon Sep 17 00:00:00 2001 From: Martin Braun <martin.braun@kit.edu> Date: Fri, 1 Feb 2013 20:17:36 +0100 Subject: core: added tag to python converters --- .../src/python/gnuradio/gr/CMakeLists.txt | 1 + gnuradio-core/src/python/gnuradio/gr/__init__.py | 1 + .../src/python/gnuradio/gr/qa_tag_utils.py | 53 +++++++++++++++++++++ gnuradio-core/src/python/gnuradio/gr/tag_utils.py | 54 ++++++++++++++++++++++ 4 files changed, 109 insertions(+) create mode 100755 gnuradio-core/src/python/gnuradio/gr/qa_tag_utils.py create mode 100644 gnuradio-core/src/python/gnuradio/gr/tag_utils.py (limited to 'gnuradio-core/src/python') diff --git a/gnuradio-core/src/python/gnuradio/gr/CMakeLists.txt b/gnuradio-core/src/python/gnuradio/gr/CMakeLists.txt index bd78c8fb4a..da22a5f987 100644 --- a/gnuradio-core/src/python/gnuradio/gr/CMakeLists.txt +++ b/gnuradio-core/src/python/gnuradio/gr/CMakeLists.txt @@ -29,6 +29,7 @@ GR_PYTHON_INSTALL(FILES gr_threading_24.py hier_block2.py prefs.py + tag_utils.py top_block.py pubsub.py DESTINATION ${GR_PYTHON_DIR}/gnuradio/gr diff --git a/gnuradio-core/src/python/gnuradio/gr/__init__.py b/gnuradio-core/src/python/gnuradio/gr/__init__.py index 5d01ea11b1..f1b971e62d 100644 --- a/gnuradio-core/src/python/gnuradio/gr/__init__.py +++ b/gnuradio-core/src/python/gnuradio/gr/__init__.py @@ -29,6 +29,7 @@ from exceptions import * from hier_block2 import * from top_block import * from gateway import basic_block, sync_block, decim_block, interp_block +from tag_utils import tag_to_python, tag_to_pmt # create a couple of aliases serial_to_parallel = stream_to_vector diff --git a/gnuradio-core/src/python/gnuradio/gr/qa_tag_utils.py b/gnuradio-core/src/python/gnuradio/gr/qa_tag_utils.py new file mode 100755 index 0000000000..daf87f912e --- /dev/null +++ b/gnuradio-core/src/python/gnuradio/gr/qa_tag_utils.py @@ -0,0 +1,53 @@ +#!/usr/bin/env python +# +# Copyright 2007,2010 Free Software Foundation, Inc. +# +# This file is part of GNU Radio +# +# GNU Radio is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3, or (at your option) +# any later version. +# +# GNU Radio is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with GNU Radio; see the file COPYING. If not, write to +# the Free Software Foundation, Inc., 51 Franklin Street, +# Boston, MA 02110-1301, USA. +# + +from gnuradio import gr, gr_unittest +import tag_utils + +try: from gruel import pmt +except: import pmt + +class test_tag_utils (gr_unittest.TestCase): + + def setUp (self): + self.tb = gr.top_block () + + + def tearDown (self): + self.tb = None + + def test_001(self): + t = gr.gr_tag_t() + t.offset = 10 + t.key = pmt.pmt_string_to_symbol('key') + t.value = pmt.pmt_from_long(23) + t.srcid = pmt.pmt_from_bool(False) + pt = tag_utils.tag_to_python(t) + self.assertEqual(pt.key, 'key') + self.assertEqual(pt.value, 23) + self.assertEqual(pt.offset, 10) + + +if __name__ == '__main__': + print 'hi' + gr_unittest.run(test_tag_utils, "test_tag_utils.xml") + diff --git a/gnuradio-core/src/python/gnuradio/gr/tag_utils.py b/gnuradio-core/src/python/gnuradio/gr/tag_utils.py new file mode 100644 index 0000000000..923718fc98 --- /dev/null +++ b/gnuradio-core/src/python/gnuradio/gr/tag_utils.py @@ -0,0 +1,54 @@ +# +# Copyright 2003-2012 Free Software Foundation, Inc. +# +# This file is part of GNU Radio +# +# GNU Radio is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3, or (at your option) +# any later version. +# +# GNU Radio is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with GNU Radio; see the file COPYING. If not, write to +# the Free Software Foundation, Inc., 51 Franklin Street, +# Boston, MA 02110-1301, USA. +# +""" Conversion tools between stream tags and Python objects """ + +try: import pmt +except: from gruel import pmt + +from gnuradio_core import gr_tag_t + +class PythonTag(object): + " Python container for tags " + def __init__(self): + self.offset = None + self.key = None + self.value = None + self.srcid = None + +def tag_to_python(tag): + """ Convert a stream tag to a Python-readable object """ + newtag = PythonTag() + newtag.offset = tag.offset + newtag.key = pmt.to_python(tag.key) + newtag.value = pmt.to_python(tag.value) + newtag.srcid = pmt.to_python(tag.srcid) + return newtag + +def tag_to_pmt(tag): + """ Convert a Python-readable object to a stream tag """ + newtag = gr_tag_t() + newtag.offset = tag.offset + newtag.key = pmt.to_python(tag.key) + newtag.value = pmt.from_python(tag.value) + newtag.srcid = pmt.from_python(tag.srcid) + return newtag + + -- cgit v1.2.3 From 21dc42548db1fc8aef0f7cc2983414279ecb1480 Mon Sep 17 00:00:00 2001 From: Tom Rondeau <trondeau@vt.edu> Date: Tue, 5 Feb 2013 13:34:45 -0500 Subject: gruel: fixing PMT naming issues with new PMT utilities. --- .../src/python/gnuradio/gr/qa_tag_utils.py | 6 +-- gruel/src/python/pmt/pmt_to_python.py | 54 +++++++++++----------- gruel/src/python/qa_pmt_to_python.py | 2 +- 3 files changed, 31 insertions(+), 31 deletions(-) (limited to 'gnuradio-core/src/python') diff --git a/gnuradio-core/src/python/gnuradio/gr/qa_tag_utils.py b/gnuradio-core/src/python/gnuradio/gr/qa_tag_utils.py index daf87f912e..86da3d227b 100755 --- a/gnuradio-core/src/python/gnuradio/gr/qa_tag_utils.py +++ b/gnuradio-core/src/python/gnuradio/gr/qa_tag_utils.py @@ -38,9 +38,9 @@ class test_tag_utils (gr_unittest.TestCase): def test_001(self): t = gr.gr_tag_t() t.offset = 10 - t.key = pmt.pmt_string_to_symbol('key') - t.value = pmt.pmt_from_long(23) - t.srcid = pmt.pmt_from_bool(False) + t.key = pmt.string_to_symbol('key') + t.value = pmt.from_long(23) + t.srcid = pmt.from_bool(False) pt = tag_utils.tag_to_python(t) self.assertEqual(pt.key, 'key') self.assertEqual(pt.value, 23) diff --git a/gruel/src/python/pmt/pmt_to_python.py b/gruel/src/python/pmt/pmt_to_python.py index 030c1c11d0..e551f4e983 100644 --- a/gruel/src/python/pmt/pmt_to_python.py +++ b/gruel/src/python/pmt/pmt_to_python.py @@ -24,63 +24,63 @@ import numpy #define missing def pmt_to_tuple(p): elems = list() - for i in range(pmt.pmt_length(p)): - elem = pmt.pmt_tuple_ref(p, i) + for i in range(pmt.length(p)): + elem = pmt.tuple_ref(p, i) elems.append(pmt_to_python(elem)) return tuple(elems) def pmt_from_tuple(p): args = map(python_to_pmt, p) - return pmt.pmt_make_tuple(*args) + return pmt.make_tuple(*args) def pmt_to_vector(p): v = list() - for i in range(pmt.pmt_length(p)): - elem = pmt.pmt_vector_ref(p, i) + for i in range(pmt.length(p)): + elem = pmt.vector_ref(p, i) v.append(pmt_to_python(elem)) return v def pmt_from_vector(p): - v = pmt.pmt_make_vector(len(p), pmt.PMT_NIL) + v = pmt.make_vector(len(p), pmt.PMT_NIL) for i, elem in enumerate(p): - pmt.pmt_vector_set(v, i, python_to_pmt(elem)) + pmt.vector_set(v, i, python_to_pmt(elem)) return v def pmt_to_dict(p): d = dict() - items = pmt.pmt_dict_items(p) - for i in range(pmt.pmt_length(items)): - pair = pmt.pmt_nth(i, items) - k = pmt.pmt_car(pair) - v = pmt.pmt_cdr(pair) + items = pmt.dict_items(p) + for i in range(pmt.length(items)): + pair = pmt.nth(i, items) + k = pmt.car(pair) + v = pmt.cdr(pair) d[pmt_to_python(k)] = pmt_to_python(v) return d def pmt_from_dict(p): - d = pmt.pmt_make_dict() + d = pmt.make_dict() for k, v in p.iteritems(): #dict is immutable -> therefore pmt_dict_add returns the new dict - d = pmt.pmt_dict_add(d, python_to_pmt(k), python_to_pmt(v)) + d = pmt.dict_add(d, python_to_pmt(k), python_to_pmt(v)) return d def numpy_to_blob(p): p = p.view(numpy.uint8) - b = pmt.pmt_make_blob(len(p)) - pmt.pmt_blob_data(b)[:] = p + b = pmt.make_blob(len(p)) + pmt.blob_data(b)[:] = p return b THE_TABLE = ( #python type, check pmt type, to python, from python - (None, pmt.pmt_is_null, lambda x: None, lambda x: pmt.PMT_NIL), - (bool, pmt.pmt_is_bool, pmt.pmt_to_bool, pmt.pmt_from_bool), - (str, pmt.pmt_is_symbol, pmt.pmt_symbol_to_string, pmt.pmt_string_to_symbol), - (int, pmt.pmt_is_integer, pmt.pmt_to_long, pmt.pmt_from_long), - (long, pmt.pmt_is_uint64, lambda x: long(pmt.pmt_to_uint64(x)), pmt.pmt_from_uint64), - (float, pmt.pmt_is_real, pmt.pmt_to_double, pmt.pmt_from_double), - (complex, pmt.pmt_is_complex, pmt.pmt_to_complex, pmt.pmt_from_complex), - (tuple, pmt.pmt_is_tuple, pmt_to_tuple, pmt_from_tuple), - (list, pmt.pmt_is_vector, pmt_to_vector, pmt_from_vector), - (dict, pmt.pmt_is_dict, pmt_to_dict, pmt_from_dict), - (numpy.ndarray, pmt.pmt_is_blob, pmt.pmt_blob_data, numpy_to_blob), + (None, pmt.is_null, lambda x: None, lambda x: pmt.PMT_NIL), + (bool, pmt.is_bool, pmt.to_bool, pmt.from_bool), + (str, pmt.is_symbol, pmt.symbol_to_string, pmt.string_to_symbol), + (int, pmt.is_integer, pmt.to_long, pmt.from_long), + (long, pmt.is_uint64, lambda x: long(pmt.to_uint64(x)), pmt.from_uint64), + (float, pmt.is_real, pmt.to_double, pmt.from_double), + (complex, pmt.is_complex, pmt.to_complex, pmt.from_complex), + (tuple, pmt.is_tuple, pmt_to_tuple, pmt_from_tuple), + (list, pmt.is_vector, pmt_to_vector, pmt_from_vector), + (dict, pmt.is_dict, pmt_to_dict, pmt_from_dict), + (numpy.ndarray, pmt.is_blob, pmt.blob_data, numpy_to_blob), ) def pmt_to_python(p): diff --git a/gruel/src/python/qa_pmt_to_python.py b/gruel/src/python/qa_pmt_to_python.py index c63403a520..71aa9000ea 100755 --- a/gruel/src/python/qa_pmt_to_python.py +++ b/gruel/src/python/qa_pmt_to_python.py @@ -26,7 +26,7 @@ import pmt class test_gruel_pmt_to_python(unittest.TestCase): def test01 (self): - b = pmt.pmt_from_double(123765) + b = pmt.from_double(123765) self.assertEqual(pmt.to_python(b), 123765) t = pmt.to_pmt(range(5)) -- cgit v1.2.3