By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,110 Members | 1,542 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,110 IT Pros & Developers. It's quick & easy.

swig double[], under the gun and need help

P: n/a
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.
---------------------------------------------------------------------------
Aug 25 '08 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Sells, Fred wrote:
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
Aug 25 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.