There is a library in my field of study which is written in C. The performance and accuracy of this library has made it irreplaceable. However, because of the usage of C language most of people have a hard time dealing with this package.
Using this package needs some coding in C; you have to include a library and then you can use some specific data structures and functions have been define in the library. For example, as it has been demonstrated in the code below one has to include a library, initialize it in the main and add experiment and define new variables to calculate Chi-squared:
#include <stdio.h> /* Some regular libraries */
....
#include <opr/opr.h> /* the mentioned library */
int main(int argc, char *argv[])
{
/* Initialize That library */
oprInit(argv[0]);
/* Initialize the experiment */
oprAddExperiment("experiment number 1");
/* Initialize parameter vector(s) */
opr_params hypothesis_values = oprAllocer();
opr_params theory_values = oprAllocer();
oprDefine(hypothesis_values,theta12,theta13,theta23,deltacp,sdm,ldm);
oprDefine(theory_values,theta12,theta13,theta23,deltacp,sdm,ldm);
...
for(y=0.0; y<10.0 ;y=y+0.2)
{
/* updating the values in each loop */
/* calculation Chi squared */
res=opr_chi2(hypothesis_values, theory_values);
...
}
What I intent to do is a creating a python interface by creating a translator that creates the C file and runs it, i.e. if the user typed import opr the code create a C file with #include <opr/opr.h>, the main function and initialization command.
I know these kind of of-the-top-the-head solutions are not the best, So I was wondering if there is another clean and optimal way to use this library in a python script?
I have heard that python is good glue language and also heard that numerical packages like numpy use some C or FORTRAN libs, how can I use this glue like capability or do similar job like numy?
As I mentioned earlier the library's performance is unique and do not intent to reinvent the wheel by writing it in Python language unless there is a possible improvement over the actual code can be made.
-
5Python has very good documentation, including how to extend and embed it, as well as the C API.Some programmer dude– Some programmer dude2019年10月25日 08:59:52 +00:00Commented Oct 25, 2019 at 8:59
-
@Someprogrammerdude thanks! this is exactly what I needed.Mr. Who– Mr. Who2019年10月25日 09:07:59 +00:00Commented Oct 25, 2019 at 9:07
-
I would go for an extension, write an extension that wraps oprgeckos– geckos2019年10月29日 11:57:08 +00:00Commented Oct 29, 2019 at 11:57
1 Answer 1
Download python source and have a look at some of the library extensions. You will get an idea of how things work and how it should be done.