[libfann] 138/242: Vincenzo's patch to fix python bindings

Christian Kastner chrisk-guest at moszumanska.debian.org
Sat Oct 4 21:10:31 UTC 2014


This is an automated email from the git hooks/post-receive script.

chrisk-guest pushed a commit to tag Version2_0_0
in repository libfann.

commit c28f28e0792ef72cceec787d6ce04c0ecdca40f4
Author: Evan Nemerson <evan at coeus-group.com>
Date:   Thu Jun 24 10:27:18 2004 +0000

    Vincenzo's patch to fix python bindings
---
 python/fann.i        | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++-
 python/fann_helper.c | 26 ++++++++++++++++++++
 2 files changed, 93 insertions(+), 1 deletion(-)

diff --git a/python/fann.i b/python/fann.i
index 10eaa27..86ce59c 100644
--- a/python/fann.i
+++ b/python/fann.i
@@ -7,8 +7,74 @@
 #include "../src/include/fann.h"
 %}
 
-/* Let's just grab the original header file here */
+/*Define some typemaps*/
+
+%typemap(in) fann_type[ANY] {
+  int i;
+  if (!PySequence_Check($input)) {
+    PyErr_SetString(PyExc_ValueError,"Expected a sequence");
+    return NULL;
+  }
+  if (PySequence_Length($input) == 0) {
+    PyErr_SetString(PyExc_ValueError,"Size mismatch. Expected some elements");
+    return NULL;
+  }
+  $1 = (float *) malloc(PySequence_Length($input)*sizeof(float));
+  for (i = 0; i < PySequence_Length($input); i++) {
+    PyObject *o = PySequence_GetItem($input,i);
+    if (PyNumber_Check(o)) {
+      $1[i] = (float) PyFloat_AsDouble(o);
+    } else {
+      PyErr_SetString(PyExc_ValueError,"Sequence elements must be numbers");      
+      return NULL;
+    }
+  }
+}
+
+%typemap(in) int[ANY] {
+  int i;
+  if (!PySequence_Check($input)) {
+    PyErr_SetString(PyExc_ValueError,"Expected a sequence");
+    return NULL;
+  }
+  if (PySequence_Length($input) == 0) {
+    PyErr_SetString(PyExc_ValueError,"Size mismatch. Expected some elements");
+    return NULL;
+  }
+  $1 = (unsigned int *) malloc(PySequence_Length($input)*sizeof(unsigned int));
+  for (i = 0; i < PySequence_Length($input); i++) {
+    PyObject *o = PySequence_GetItem($input,i);
+    if (PyNumber_Check(o)) {
+      $1[i] = (int) PyInt_AsLong(o);
+    } else {
+      PyErr_SetString(PyExc_ValueError,"Sequence elements must be numbers");      
+      return NULL;
+    }
+  }
+}
+
+%typemap(freearg) fann_type value[ANY] {
+   if ($1) free($1);
+}
+
+%typemap(out) PyObject* {
+  $result = $1;
+}
+
+%apply fann_type[ANY] {fann_type *};
+%apply int[ANY] {int *, unsigned int*};
+
 #define FANN_INCLUDE
 %varargs(10,int n = 0) fann_create;
+%rename(fann_run_old) fann_run;
+%rename(fann_run) fann_run2;
+
+%rename(fann_test_old) fann_test;
+%rename(fann_test) fann_test2;
+
+/* Let's grab the original header file here */
 %include "../src/include/fann.h"
 
+// Helper functions
+PyObject* fann_run2(struct fann *ann, fann_type *input);
+PyObject* fann_test2(struct fann *ann, fann_type *input, fann_type *desired_output);
diff --git a/python/fann_helper.c b/python/fann_helper.c
new file mode 100644
index 0000000..a2e371a
--- /dev/null
+++ b/python/fann_helper.c
@@ -0,0 +1,26 @@
+#include <Python.h>
+#include <fann.h>
+
+PyObject* fann_type_to_PyList(fann_type *array,int n)
+{
+  int i;
+  PyObject* res = PyList_New(n);
+  for (i = 0; i < n; i++) {
+    PyObject *o = PyFloat_FromDouble((double) array[i]);
+    PyList_SetItem(res,i,o);
+  }
+  return res;
+}
+
+PyObject* fann_run2(struct fann *ann, fann_type *input)
+{
+  if (!ann) return NULL;
+  return fann_type_to_PyList(fann_run(ann,input),ann->num_output);
+}
+
+
+PyObject* fann_test2(struct fann *ann, fann_type *input, fann_type *desired_output)
+{
+  if (!ann) return NULL;
+  return fann_type_to_PyList(fann_test(ann,input,desired_output),ann->num_output);
+}

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-science/packages/libfann.git



More information about the debian-science-commits mailing list