summaryrefslogtreecommitdiff
path: root/gnuradio-runtime/python/pmt
diff options
context:
space:
mode:
Diffstat (limited to 'gnuradio-runtime/python/pmt')
-rw-r--r--gnuradio-runtime/python/pmt/CMakeLists.txt47
-rw-r--r--gnuradio-runtime/python/pmt/__init__.py39
-rw-r--r--gnuradio-runtime/python/pmt/pmt_to_python.py97
-rwxr-xr-xgnuradio-runtime/python/pmt/qa_pmt.py102
-rwxr-xr-xgnuradio-runtime/python/pmt/qa_pmt_to_python.py34
5 files changed, 319 insertions, 0 deletions
diff --git a/gnuradio-runtime/python/pmt/CMakeLists.txt b/gnuradio-runtime/python/pmt/CMakeLists.txt
new file mode 100644
index 0000000000..a9bf85a622
--- /dev/null
+++ b/gnuradio-runtime/python/pmt/CMakeLists.txt
@@ -0,0 +1,47 @@
+# Copyright 2011,2013 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.
+
+include(GrPython)
+
+########################################################################
+# Install python files
+########################################################################
+GR_PYTHON_INSTALL(FILES
+ __init__.py
+ pmt_to_python.py
+ DESTINATION ${GR_PYTHON_DIR}/pmt
+ COMPONENT "runtime_python"
+)
+
+########################################################################
+# Setup unit tests
+########################################################################
+if(ENABLE_TESTING)
+include(GrTest)
+file(GLOB py_qa_test_files "qa_*.py")
+foreach(py_qa_test_file ${py_qa_test_files})
+ get_filename_component(py_qa_test_name ${py_qa_test_file} NAME_WE)
+ set(GR_TEST_PYTHON_DIRS
+ ${CMAKE_BINARY_DIR}/gnuradio-runtime/python
+ ${CMAKE_BINARY_DIR}/gnuradio-runtime/swig
+ )
+ set(GR_TEST_TARGET_DEPS gnuradio-runtime)
+ GR_ADD_TEST(${py_qa_test_name} ${PYTHON_EXECUTABLE} ${PYTHON_DASH_B} ${py_qa_test_file})
+endforeach(py_qa_test_file)
+endif(ENABLE_TESTING)
diff --git a/gnuradio-runtime/python/pmt/__init__.py b/gnuradio-runtime/python/pmt/__init__.py
new file mode 100644
index 0000000000..0d995c24a4
--- /dev/null
+++ b/gnuradio-runtime/python/pmt/__init__.py
@@ -0,0 +1,39 @@
+#
+# Copyright 2011 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.
+#
+
+# The presence of this file turns this directory into a Python package
+
+'''
+The GNU Radio Utility Etcetera Library's Polymorphic Types for Python.
+'''
+
+import os
+
+try:
+ from pmt_swig import *
+except ImportError:
+ dirname, filename = os.path.split(os.path.abspath(__file__))
+ __path__.append(os.path.join(dirname, "..", "..", "swig"))
+ from pmt_swig import *
+
+from pmt_to_python import pmt_to_python as to_python
+from pmt_to_python import python_to_pmt as to_pmt
+
diff --git a/gnuradio-runtime/python/pmt/pmt_to_python.py b/gnuradio-runtime/python/pmt/pmt_to_python.py
new file mode 100644
index 0000000000..e551f4e983
--- /dev/null
+++ b/gnuradio-runtime/python/pmt/pmt_to_python.py
@@ -0,0 +1,97 @@
+# Copyright 2012,2013 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.
+
+try: import pmt_swig as pmt
+except: import pmt
+import numpy
+
+#define missing
+def pmt_to_tuple(p):
+ elems = list()
+ 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.make_tuple(*args)
+
+def pmt_to_vector(p):
+ v = list()
+ 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.make_vector(len(p), pmt.PMT_NIL)
+ for i, elem in enumerate(p):
+ pmt.vector_set(v, i, python_to_pmt(elem))
+ return v
+
+def pmt_to_dict(p):
+ d = dict()
+ 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.make_dict()
+ for k, v in p.iteritems():
+ #dict is immutable -> therefore pmt_dict_add returns the new dict
+ 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.make_blob(len(p))
+ pmt.blob_data(b)[:] = p
+ return b
+
+THE_TABLE = ( #python type, check pmt type, to python, from python
+ (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):
+ for python_type, pmt_check, to_python, from_python in THE_TABLE:
+ if pmt_check(p): return to_python(p)
+ return p #give up, we return the same
+
+def python_to_pmt(p):
+ for python_type, pmt_check, to_python, from_python in THE_TABLE:
+ if python_type is None:
+ if p == None: return from_python(p)
+ elif isinstance(p, python_type): return from_python(p)
+ return p #give up, we return the same
+
diff --git a/gnuradio-runtime/python/pmt/qa_pmt.py b/gnuradio-runtime/python/pmt/qa_pmt.py
new file mode 100755
index 0000000000..2a72fa6089
--- /dev/null
+++ b/gnuradio-runtime/python/pmt/qa_pmt.py
@@ -0,0 +1,102 @@
+#!/usr/bin/env python
+#
+# Copyright 2011 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.
+#
+
+import unittest
+import pmt
+
+class test_pmt(unittest.TestCase):
+
+ def test01(self):
+ a = pmt.intern("a")
+ b = pmt.from_double(123765)
+ d1 = pmt.make_dict()
+ d2 = pmt.dict_add(d1, a, b)
+ print d2
+
+ def test02(self):
+ const = 123765
+ x_pmt = pmt.from_double(const)
+ x_int = pmt.to_double(x_pmt)
+ self.assertEqual(x_int, const)
+
+ def test03(self):
+ v = pmt.init_f32vector(3, [11, -22, 33])
+ s = pmt.serialize_str(v)
+ d = pmt.deserialize_str(s)
+ self.assertTrue(pmt.equal(v, d))
+
+ def test04(self):
+ v = pmt.init_f64vector(3, [11, -22, 33])
+ s = pmt.serialize_str(v)
+ d = pmt.deserialize_str(s)
+ self.assertTrue(pmt.equal(v, d))
+
+ def test05(self):
+ v = pmt.init_u8vector(3, [11, 22, 33])
+ s = pmt.serialize_str(v)
+ d = pmt.deserialize_str(s)
+ self.assertTrue(pmt.equal(v, d))
+
+ def test06(self):
+ v = pmt.init_s8vector(3, [11, -22, 33])
+ s = pmt.serialize_str(v)
+ d = pmt.deserialize_str(s)
+ self.assertTrue(pmt.equal(v, d))
+
+ def test07(self):
+ v = pmt.init_u16vector(3, [11, 22, 33])
+ s = pmt.serialize_str(v)
+ d = pmt.deserialize_str(s)
+ self.assertTrue(pmt.equal(v, d))
+
+ def test08(self):
+ v = pmt.init_s16vector(3, [11, -22, 33])
+ s = pmt.serialize_str(v)
+ d = pmt.deserialize_str(s)
+ self.assertTrue(pmt.equal(v, d))
+
+ def test09(self):
+ v = pmt.init_u32vector(3, [11, 22, 33])
+ s = pmt.serialize_str(v)
+ d = pmt.deserialize_str(s)
+ self.assertTrue(pmt.equal(v, d))
+
+ def test10(self):
+ v = pmt.init_s32vector(3, [11, -22, 33])
+ s = pmt.serialize_str(v)
+ d = pmt.deserialize_str(s)
+ self.assertTrue(pmt.equal(v, d))
+
+ def test11(self):
+ v = pmt.init_c32vector(3, [11 + -101j, -22 + 202j, 33 + -303j])
+ s = pmt.serialize_str(v)
+ d = pmt.deserialize_str(s)
+ self.assertTrue(pmt.equal(v, d))
+
+ def test12(self):
+ v = pmt.init_c64vector(3, [11 + -101j, -22 + 202j, 33 + -303j])
+ s = pmt.serialize_str(v)
+ d = pmt.deserialize_str(s)
+ self.assertTrue(pmt.equal(v, d))
+
+if __name__ == '__main__':
+ unittest.main()
diff --git a/gnuradio-runtime/python/pmt/qa_pmt_to_python.py b/gnuradio-runtime/python/pmt/qa_pmt_to_python.py
new file mode 100755
index 0000000000..ae86fc6d53
--- /dev/null
+++ b/gnuradio-runtime/python/pmt/qa_pmt_to_python.py
@@ -0,0 +1,34 @@
+#!/usr/bin/env python
+#
+# Copyright 2013 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.
+#
+
+import unittest
+import pmt
+
+class test_pmt_to_python(unittest.TestCase):
+
+ def test01 (self):
+ b = pmt.from_double(123765)
+ self.assertEqual(pmt.to_python(b), 123765)
+ t = pmt.to_pmt(range(5))
+
+if __name__ == '__main__':
+ unittest.main()