swig double[], under the gun and need help

S

Sells, Fred

I'm using python 2.4 under linux (centos 5.1).
I need to pass an array of doubles to a c function
but am getting an error, shown near the bottom of
this post.
----------------------------------------------------
my swig interface file looks like this

* File: rug520.i */
%module rug520
%include "typemaps.i"
%include "carrays.i"
%array_class(double, doubleArray);
%{
#define SWIG_FILE_WITH_INIT
#include "rug520.h"
extern double[] nCmiArray;
%}


%apply int *OUTPUT { char *sRugHier,
char * sRugMax,
int * iRugHier,
int * iRugMax,
double * nCmiValueHier,
double * nCmiValueMax,
int * iAdlSum,
int * iCpsCode,
char * sRugsVersion,
char * sDllVersion,
int * iError };


int RugCalc( char * sMdsRecord,
char * sRehabType,
char * sModel,
int iQuarterlyFlag,
double nCmiArray[],

char * sRugHier,
char * sRugMax,
int * iRugHier,
int * iRugMax,
double * nCmiValueHier,
double * nCmiValueMax,
int * iAdlSum,
int * iCpsCode,
char * sRugsVersion,
char * sDllVersion,
int * iError );

------------------------------------------------------------------
my test code looks like this:
import sys, os, rug520
cmi=[0.0] *59

def getrug(mds):
results = rug520.RugCalc(mds, 'mcare', '34', 0, cmi)
print 'results', results

datafile = open('mdsdata.txt')
for d in datafile:
if d[0]=='B':
getrug(d)
------------------------------------------------------------------------
I get this error message
File "testrug520.py", line 11, in ?
getrug(d)
File "testrug520.py", line 5, in getrug
results = rug520.RugCalc(mds, 'mcare', '34', 0, cmi)
TypeError: in method 'RugCalc', argument 5 of type 'double []'

--------------------------------------------------------------------------

I'm guessing that I am not passing a double array to the c code. I cannot change
the c code due to politics. I could write a c "wrapper" if I had to, but would rather
stay within python or the swig interface definitions if possible/practical.

I'm not much of a c programmer; mostly java and python with a little c++.
I've looked in the swig docs and tried google, but either have not found
it or just don't understand what they are telling me here.

---------------------------------------------------------------------------
The information contained in this message may be privileged and / or
confidential and protected from disclosure. If the reader of this message is
not the intended recipient, you are hereby notified that any dissemination,
distribution or copying of this communication is strictly prohibited. If you
have received this communication in error, please notify the sender
immediately by replying to this message and deleting the material from any
computer.
---------------------------------------------------------------------------
 
D

Diez B. Roggisch

I'm using python 2.4 under linux (centos 5.1).
I need to pass an array of doubles to a c function
but am getting an error, shown near the bottom of
this post.
----------------------------------------------------
my swig interface file looks like this

* File: rug520.i */
%module rug520
%include "typemaps.i"
%include "carrays.i"
%array_class(double, doubleArray);
%{
#define SWIG_FILE_WITH_INIT
#include "rug520.h"
extern double[] nCmiArray;
%}


%apply int *OUTPUT { char *sRugHier,
char * sRugMax,
int * iRugHier,
int * iRugMax,
double * nCmiValueHier,
double * nCmiValueMax,
int * iAdlSum,
int * iCpsCode,
char * sRugsVersion,
char * sDllVersion,
int * iError };


int RugCalc( char * sMdsRecord,
char * sRehabType,
char * sModel,
int iQuarterlyFlag,
double nCmiArray[],

char * sRugHier,
char * sRugMax,
int * iRugHier,
int * iRugMax,
double * nCmiValueHier,
double * nCmiValueMax,
int * iAdlSum,
int * iCpsCode,
char * sRugsVersion,
char * sDllVersion,
int * iError );

------------------------------------------------------------------
my test code looks like this:
import sys, os, rug520
cmi=[0.0] *59

def getrug(mds):
results = rug520.RugCalc(mds, 'mcare', '34', 0, cmi)
print 'results', results

datafile = open('mdsdata.txt')
for d in datafile:
if d[0]=='B':
getrug(d)
------------------------------------------------------------------------
I get this error message
File "testrug520.py", line 11, in ?
getrug(d)
File "testrug520.py", line 5, in getrug
results = rug520.RugCalc(mds, 'mcare', '34', 0, cmi)
TypeError: in method 'RugCalc', argument 5 of type 'double []'

--------------------------------------------------------------------------

I'm guessing that I am not passing a double array to the c code. I cannot
change
the c code due to politics. I could write a c "wrapper" if I had to, but
would rather stay within python or the swig interface definitions if
possible/practical.

I'm not much of a c programmer; mostly java and python with a little c++.
I've looked in the swig docs and tried google, but either have not found
it or just don't understand what they are telling me here.

I don't know swig, but if all you have is a real C-API, try & use ctypes.
It's much easier to create bindings for, keeps you fully in the warm and
cozy womb of python programming and doesn't need no compilation to create
the actual binding.

Diez
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top