summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJosh Blum2012-11-09 00:41:10 -0800
committerJosh Blum2012-11-09 00:41:10 -0800
commit74142393b1161fbf0af2531e6346846cc5fcb241 (patch)
tree99a6fe48fd514674a73255fed9062d1dfcd27001
parentcb7b2b751220f2444c6187b7d6bf862b4cd56b37 (diff)
downloadsandhi-74142393b1161fbf0af2531e6346846cc5fcb241.tar.gz
sandhi-74142393b1161fbf0af2531e6346846cc5fcb241.tar.bz2
sandhi-74142393b1161fbf0af2531e6346846cc5fcb241.zip
working on tags python support
-rw-r--r--python/gras/GRAS_Block.i22
-rw-r--r--python/gras/GRAS_HierBlock.i3
-rw-r--r--python/gras/GRAS_ThreadPool.i3
-rw-r--r--python/gras/__init__.py2
-rw-r--r--tests/block_test.py23
-rw-r--r--tests/demo_blocks.py52
6 files changed, 81 insertions, 24 deletions
diff --git a/python/gras/GRAS_Block.i b/python/gras/GRAS_Block.i
index 9503af8..54f2186 100644
--- a/python/gras/GRAS_Block.i
+++ b/python/gras/GRAS_Block.i
@@ -14,8 +14,6 @@
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
-#define GRAS_API
-
////////////////////////////////////////////////////////////////////////
// SWIG director shit - be explicit with all virtual methods
////////////////////////////////////////////////////////////////////////
@@ -29,7 +27,6 @@
%feature("nodirector") gras::BlockPython::notify_topology;
%feature("nodirector") gras::BlockPython::work;
-
////////////////////////////////////////////////////////////////////////
// http://www.swig.org/Doc2.0/Library.html#Library_stl_exceptions
////////////////////////////////////////////////////////////////////////
@@ -95,6 +92,7 @@ struct PyGILPhondler
////////////////////////////////////////////////////////////////////////
// Pull in the implementation goodies
////////////////////////////////////////////////////////////////////////
+%include <gras/gras.hpp>
%include <gras/element.i>
%include <gras/io_signature.i>
%include <gras/sbuffer.hpp>
@@ -225,6 +223,15 @@ def sig_to_dtype_sig(sig):
if sig is None: sig = ()
return map(numpy.dtype, sig)
+Tag__ = Tag
+
+class Tag(object):
+ def __init__(self, offset=0, key=None, value=None, srcid=None):
+ self.offset = offset
+ self.key = key
+ self.value = value
+ self.srcid = srcid
+
class Block(BlockPython):
def __init__(self, name='Block', in_sig=None, out_sig=None):
BlockPython.__init__(self, name)
@@ -288,4 +295,13 @@ class Block(BlockPython):
def stop(self): return True
+ def post_output_tag(self, which_output, tag):
+ t = Tag__(
+ tag.offset,
+ Py2PMC(tag.key),
+ Py2PMC(tag.value),
+ Py2PMC(tag.srcid),
+ )
+ BlockPython.post_output_tag(self, which_output, t)
+
%}
diff --git a/python/gras/GRAS_HierBlock.i b/python/gras/GRAS_HierBlock.i
index 1a55f02..7745be6 100644
--- a/python/gras/GRAS_HierBlock.i
+++ b/python/gras/GRAS_HierBlock.i
@@ -14,8 +14,6 @@
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
-#define GRAS_API
-
////////////////////////////////////////////////////////////////////////
// http://www.swig.org/Doc2.0/Library.html#Library_stl_exceptions
////////////////////////////////////////////////////////////////////////
@@ -59,6 +57,7 @@ struct PyTSPhondler
#include <gras/top_block.hpp>
%}
+%include <gras/gras.hpp>
%include <gras/element.i>
%include <gras/io_signature.i>
diff --git a/python/gras/GRAS_ThreadPool.i b/python/gras/GRAS_ThreadPool.i
index a50e8a8..12138af 100644
--- a/python/gras/GRAS_ThreadPool.i
+++ b/python/gras/GRAS_ThreadPool.i
@@ -14,12 +14,11 @@
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
-#define GRAS_API
-
%{
#include <gras/thread_pool.hpp>
%}
%include <std_string.i>
+%include <gras/gras.hpp>
%include <gras/thread_pool.hpp>
diff --git a/python/gras/__init__.py b/python/gras/__init__.py
index 4d74879..b348ca3 100644
--- a/python/gras/__init__.py
+++ b/python/gras/__init__.py
@@ -1,3 +1,3 @@
-from GRAS_Block import Block
+from GRAS_Block import Block, Tag
from GRAS_HierBlock import HierBlock, TopBlock
from GRAS_ThreadPool import ThreadPoolConfig, ThreadPool
diff --git a/tests/block_test.py b/tests/block_test.py
index 23322a4..6d922a9 100644
--- a/tests/block_test.py
+++ b/tests/block_test.py
@@ -21,7 +21,7 @@ class BlockTest(unittest.TestCase):
tb = gras.TopBlock()
src0 = VectorSource(numpy.float32, [1, 3, 5, 7, 9])
src1 = VectorSource(numpy.float32, [0, 2, 4, 6, 8])
- adder = add_2x(numpy.float32)
+ adder = Add2X(numpy.float32)
sink = VectorSink(numpy.float32)
tb.connect((src0, 0), (adder, 0))
tb.connect((src1, 0), (adder, 1))
@@ -30,5 +30,26 @@ class BlockTest(unittest.TestCase):
tb = None
self.assertEqual(sink.get_vector(), (1, 5, 9, 13, 17))
+ def test_add_fc32(self):
+ tb = gras.TopBlock()
+ src0 = VectorSource(numpy.complex64, [1, 3j, 5, 7j, 9])
+ src1 = VectorSource(numpy.complex64, [0, 2j, 4, 6j, 8])
+ adder = Add2X(numpy.complex64)
+ sink = VectorSink(numpy.complex64)
+ tb.connect((src0, 0), (adder, 0))
+ tb.connect((src1, 0), (adder, 1))
+ tb.connect(adder, sink)
+ tb.run()
+ tb = None
+ self.assertEqual(sink.get_vector(), (1, 5j, 9, 13j, 17))
+
+ def test_tag_source(self):
+ tb = gras.TopBlock()
+ src = TagSource([1, 2, 3])
+ sink = NullSink(numpy.uint8)
+ tb.connect(src, sink)
+ tb.run()
+ tb = None
+
if __name__ == '__main__':
unittest.main()
diff --git a/tests/demo_blocks.py b/tests/demo_blocks.py
index 3a5d5cb..7187ec7 100644
--- a/tests/demo_blocks.py
+++ b/tests/demo_blocks.py
@@ -1,21 +1,6 @@
import gras
import numpy
-class add_2x(gras.Block):
- def __init__(self, sig):
- gras.Block.__init__(self,
- name = "add 2x",
- in_sig = [sig, sig],
- out_sig = [sig],
- )
-
- def work(self, ins, outs):
- nitems = min(*map(len, (ins[0], ins[1], outs[0])))
- outs[0][:nitems] = ins[0][:nitems] + ins[1][:nitems]
- self.consume(0, nitems)
- self.consume(1, nitems)
- self.produce(0, nitems)
-
class NullSource(gras.Block):
def __init__(self, out_sig):
gras.Block.__init__(self, 'NullSource', out_sig=[out_sig])
@@ -29,7 +14,10 @@ class NullSink(gras.Block):
gras.Block.__init__(self, 'NullSink', in_sig=[in_sig])
def work(self, ins, outs):
+ print 'consume',len(ins[0])
self.consume(0, len(ins[0]))
+ #FIXME should not need == 0 part!!
+ if len(ins[0]) == 0: self.mark_done()
class VectorSource(gras.Block):
def __init__(self, out_sig, vec):
@@ -55,3 +43,37 @@ class VectorSink(gras.Block):
def work(self, ins, outs):
self._vec.extend(ins[0].copy())
self.consume(0, len(ins[0]))
+
+class Add2X(gras.Block):
+ def __init__(self, sig):
+ gras.Block.__init__(self,
+ name = "Add2X",
+ in_sig = [sig, sig],
+ out_sig = [sig],
+ )
+
+ def work(self, ins, outs):
+ nitems = min(*map(len, (ins[0], ins[1], outs[0])))
+ outs[0][:nitems] = ins[0][:nitems] + ins[1][:nitems]
+ self.consume(0, nitems)
+ self.consume(1, nitems)
+ self.produce(0, nitems)
+
+class TagSource(gras.Block):
+ def __init__(self, tags):
+ gras.Block.__init__(self,
+ name = "TagSource",
+ out_sig = [numpy.uint8],
+ )
+ self._tags = tags
+
+ def work(self, ins, outs):
+ offset = self.nitems_written(0)
+ self.post_output_tag(0, gras.Tag(offset, 'key', self._tags[0]))
+ self.produce(0, len(outs[0]))
+ self._tags = self._tags[1:]
+ print 'produce', len(outs[0])
+ print 'self._tags', self._tags
+ if not self._tags:
+ self.mark_done()
+ print 'done!'