471,055 Members | 1,648 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,055 software developers and data experts.

writing a generic method

fj
I am new in Python programming. I try to connect to Python various
libraries written either in C of in Fortran. The job is not really hard
but I meet a trouble when trying to build up a generic routine like in
C++ or F90, i.e. a single routine name for various uses.

Here is an example with a Fortran 77 library:

static PyObject* mdb_get(PyObject *self, PyObject *args){
const char *cname,*cprop;
int lname,lprop;
double x,z;
int ix;
if(PyArg_ParseTuple(args,"s#s#d",&cname,&lname,&cp rop,&lprop,&x))
mdbgetd_(cname,cprop,&x,&z,lname,lprop);
else
if(PyArg_ParseTuple(args,"s#s#i",&cname,&lname,&cp rop,&lprop,&ix))
mdbgeti_(cname,cprop,&ix,&z,lname,lprop);
else
return NULL;
return Py_BuildValue("d",z);
}

This method should be used as a single Python function mdb::get. It
should have two possible forms:
mdb.get("H2O","h_l(T)",300.)
mdb.get("H2O","atom",2)

As you see, the third argument should be either a real or an integer.
Unfortunately, the result depends on the order of instructions because
Python automatically converts
integer into real and vice versa. For instance, the intructions above
are interpreted as follows :
mdb.get("H2O","h_t(T)",300.) (correct)
mdb.get("H2O","atom",2.) (wrong because 2 and 2. are not
equivalent in my case)

So my question is : Is it possible to deactivate this automatic
conversion ?
Presently, the only solution I found was to write two functions
(mdb.getr and mdb.geti)

Nov 22 '05 #1
0 1092

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Jim Newton | last post: by
89 posts views Thread by Skybuck Flying | last post: by
26 posts views Thread by raylopez99 | last post: by
reply views Thread by =?Utf-8?B?TW9ydGVuIFdlbm5ldmlrIFtDIyBNVlBd?= | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.