summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/gras/sbuffer.i42
-rw-r--r--python/gras/CMakeLists.txt5
-rw-r--r--python/gras/GRAS_Block.i21
-rw-r--r--python/gras/GRAS_SBuffer.i21
-rw-r--r--python/gras/GRAS_Utils.py15
-rw-r--r--python/gras/__init__.py1
-rw-r--r--tests/CMakeLists.txt1
-rw-r--r--tests/sbuffer_test.py47
8 files changed, 132 insertions, 21 deletions
diff --git a/include/gras/sbuffer.i b/include/gras/sbuffer.i
new file mode 100644
index 0000000..d8cadc6
--- /dev/null
+++ b/include/gras/sbuffer.i
@@ -0,0 +1,42 @@
+// Copyright (C) by Josh Blum. See LICENSE.txt for licensing information.
+
+#ifndef INCLUDED_GRAS_SBUFFER_I
+#define INCLUDED_GRAS_SBUFFER_I
+
+%{
+#include <gras/gras.hpp>
+#include <gras/sbuffer.hpp>
+%}
+
+////////////////////////////////////////////////////////////////////////
+// remove base class warning
+////////////////////////////////////////////////////////////////////////
+#pragma SWIG nowarn=401
+
+////////////////////////////////////////////////////////////////////////
+// Export swig element comprehension
+////////////////////////////////////////////////////////////////////////
+%include <gras/gras.hpp>
+%include <gras/sbuffer.hpp>
+
+////////////////////////////////////////////////////////////////////////
+// Replace the get method
+////////////////////////////////////////////////////////////////////////
+%extend gras::SBuffer
+{
+ %insert("python")
+ %{
+ def get(self):
+ from gras.GRAS_Utils import pointer_to_ndarray
+ addr = long(self.get_actual_memory())
+ return pointer_to_ndarray(
+ addr=addr + self.offset,
+ nitems=self.length,
+ )
+
+ def __len__(self): return self.length
+
+ %}
+}
+
+#endif /*INCLUDED_GRAS_SBUFFER_I*/
diff --git a/python/gras/CMakeLists.txt b/python/gras/CMakeLists.txt
index 334a6a5..2ffdfc8 100644
--- a/python/gras/CMakeLists.txt
+++ b/python/gras/CMakeLists.txt
@@ -16,15 +16,18 @@ find_package(Boost) #for headers
list(APPEND GR_SWIG_INCLUDE_DIRS ${GRAS_INCLUDE_DIRS})
list(APPEND GR_SWIG_INCLUDE_DIRS ${Boost_INCLUDE_DIRS})
set(GR_SWIG_LIBRARIES gras)
+file(GLOB GR_SWIG_SOURCE_DEPS "${GRAS_SOURCE_DIR}/include/gras/*.i")
GR_SWIG_MAKE(GRAS_Block GRAS_Block.i)
GR_SWIG_MAKE(GRAS_HierBlock GRAS_HierBlock.i)
GR_SWIG_MAKE(GRAS_ThreadPool GRAS_ThreadPool.i)
+GR_SWIG_MAKE(GRAS_SBuffer GRAS_SBuffer.i)
GR_SWIG_INSTALL(
TARGETS
GRAS_Block
GRAS_HierBlock
GRAS_ThreadPool
+ GRAS_SBuffer
DESTINATION ${GR_PYTHON_DIR}/gras
COMPONENT ${GRAS_COMP_PYTHON}
)
@@ -33,7 +36,7 @@ GR_SWIG_INSTALL(
# Install rules
########################################################################
GR_PYTHON_INSTALL(
- FILES __init__.py
+ FILES __init__.py GRAS_Utils.py
DESTINATION ${GR_PYTHON_DIR}/gras
COMPONENT ${GRAS_COMP_PYTHON}
)
diff --git a/python/gras/GRAS_Block.i b/python/gras/GRAS_Block.i
index e5d112a..5093f10 100644
--- a/python/gras/GRAS_Block.i
+++ b/python/gras/GRAS_Block.i
@@ -179,32 +179,13 @@ struct BlockPython : Block
%}
////////////////////////////////////////////////////////////////////////
-// Conversion to the numpy array
-////////////////////////////////////////////////////////////////////////
-%pythoncode %{
-
-import numpy
-
-def pointer_to_ndarray(addr, dtype, nitems, readonly=False):
- class array_like:
- __array_interface__ = {
- 'data' : (addr, readonly),
- 'typestr' : dtype.base.str,
- 'descr' : dtype.base.descr,
- 'shape' : (nitems,) + dtype.shape,
- 'strides' : None,
- 'version' : 3,
- }
- return numpy.asarray(array_like()).view(dtype.base)
-%}
-
-////////////////////////////////////////////////////////////////////////
// Python overload for adding pythonic interfaces
////////////////////////////////////////////////////////////////////////
%pythoncode %{
import numpy
import traceback
+from GRAS_Utils import pointer_to_ndarray
def sig_to_dtype_sig(sig):
if sig is None: sig = ()
diff --git a/python/gras/GRAS_SBuffer.i b/python/gras/GRAS_SBuffer.i
new file mode 100644
index 0000000..a26e3b0
--- /dev/null
+++ b/python/gras/GRAS_SBuffer.i
@@ -0,0 +1,21 @@
+// Copyright (C) by Josh Blum. See LICENSE.txt for licensing information.
+
+%include <gras/sbuffer.i>
+
+%include <PMC/PMC.i>
+
+DECL_PMC_SWIG_TYPE(gras::SBuffer, swig_sbuffer)
+
+%pythoncode %{
+
+RegisterPy2PMC(
+ is_py = lambda x: isinstance(x, SBuffer),
+ py2pmc = swig_sbuffer_to_pmc,
+)
+
+RegisterPMC2Py(
+ is_pmc = pmc_is_swig_sbuffer,
+ pmc2py = pmc_to_swig_sbuffer,
+)
+
+%}
diff --git a/python/gras/GRAS_Utils.py b/python/gras/GRAS_Utils.py
new file mode 100644
index 0000000..a42d816
--- /dev/null
+++ b/python/gras/GRAS_Utils.py
@@ -0,0 +1,15 @@
+# Copyright (C) by Josh Blum. See LICENSE.txt for licensing information.
+
+import numpy
+
+def pointer_to_ndarray(addr, nitems, dtype=numpy.dtype(numpy.uint8), readonly=False):
+ class array_like:
+ __array_interface__ = {
+ 'data' : (addr, readonly),
+ 'typestr' : dtype.base.str,
+ 'descr' : dtype.base.descr,
+ 'shape' : (nitems,) + dtype.shape,
+ 'strides' : None,
+ 'version' : 3,
+ }
+ return numpy.asarray(array_like()).view(dtype.base)
diff --git a/python/gras/__init__.py b/python/gras/__init__.py
index 9f613f2..dac4f03 100644
--- a/python/gras/__init__.py
+++ b/python/gras/__init__.py
@@ -3,3 +3,4 @@
from GRAS_Block import Block, Tag, IOSignature
from GRAS_HierBlock import HierBlock, TopBlock
from GRAS_ThreadPool import ThreadPoolConfig, ThreadPool
+from GRAS_SBuffer import SBufferConfig, SBuffer
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
index 8dc44e4..275bbda 100644
--- a/tests/CMakeLists.txt
+++ b/tests/CMakeLists.txt
@@ -11,3 +11,4 @@ set(GR_TEST_PYTHON_DIRS ${GRAS_PYTHON_DIRS})
GR_ADD_TEST(block_test ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/block_test.py)
GR_ADD_TEST(hier_block_test ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/hier_block_test.py)
GR_ADD_TEST(thread_pool_test ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/thread_pool_test.py)
+GR_ADD_TEST(sbuffer_test ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/sbuffer_test.py)
diff --git a/tests/sbuffer_test.py b/tests/sbuffer_test.py
new file mode 100644
index 0000000..2c7c5b0
--- /dev/null
+++ b/tests/sbuffer_test.py
@@ -0,0 +1,47 @@
+# Copyright (C) by Josh Blum. See LICENSE.txt for licensing information.
+
+import unittest
+import gras
+
+class SBufferTest(unittest.TestCase):
+
+ def test_make_config(self):
+ c = gras.SBufferConfig()
+ print c.length
+ print c.memory
+
+ def test_make_sbuffer(self):
+ c = gras.SBufferConfig()
+ c.length = 42
+ s = gras.SBuffer(c)
+ self.assertEqual(c.length, s.get_actual_length())
+ self.assertEqual(c.length, s.length)
+ self.assertEqual(c.length, len(s))
+
+ def test_get_sbuffer(self):
+ c = gras.SBufferConfig()
+ c.length = 24
+ s = gras.SBuffer(c)
+ a = s.get()
+ self.assertEqual(c.length, len(s))
+ self.assertEqual(c.length, len(a))
+
+ def test_more_get_with_offset(self):
+ c = gras.SBufferConfig()
+ c.length = 4
+ s = gras.SBuffer(c)
+ a0 = s.get()
+ a0[:] = [1, 2, 3, 4]
+ a1 = s.get()
+ self.assertEqual(tuple(a1), (1, 2, 3, 4))
+
+ #now offset the start of buffer
+ s.offset = 2
+ s.length = 2
+ a2 = s.get()
+ self.assertEqual(2, len(s))
+ self.assertEqual(2, len(a2))
+ self.assertEqual(tuple(a2), (3, 4))
+
+if __name__ == '__main__':
+ unittest.main()