1

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.

asked Oct 25, 2019 at 8:55
3
  • 5
    Python has very good documentation, including how to extend and embed it, as well as the C API. Commented Oct 25, 2019 at 8:59
  • @Someprogrammerdude thanks! this is exactly what I needed. Commented Oct 25, 2019 at 9:07
  • I would go for an extension, write an extension that wraps opr Commented Oct 29, 2019 at 11:57

1 Answer 1

0

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.

answered Oct 29, 2019 at 11:51
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.