2

I read similar posts but nothing specific to this. Or maybe my logic is incorrect and you can correct me please.

What I'm trying to do:

Write python code, that then calls an already compiled C or C++ code. But how would I pass an argument to that C/C++ script given that it's already been compiled?

I have a program in Python that manipulates an Excel file. And then at some point I need to search through, append, and create an array of 10,000 cells. I figured C/C++ would make this faster since python is taking some time to do that.

Help please.

Michael 1022
  • 91
  • 2
  • 10
  • This sounds somewhat like an [XY problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). – Christian Dean Dec 01 '16 at 22:13
  • Perhaps...but before you guys flag this, please anyone any pointers? Would greatly appreciate it. – Michael 1022 Dec 01 '16 at 22:18
  • 2
    No, no. Your misunderstanding my point. I'm not saying your question should be flagged, I'm simply saying you should ask about how to solve your problem, not how to fix your solution. – Christian Dean Dec 01 '16 at 22:19
  • Are you sure the problem is with Python, and not an inefficient implementation? Have you looked at `pandas` and `numpy`? – brianpck Dec 01 '16 at 22:19
  • compiled c and c++ functions still take arguments. Maybe take a look at [this](http://stackoverflow.com/questions/145270/calling-c-c-from-python?rq=1) after making sure your problem isn't with the algorithm. – jaggedSpire Dec 01 '16 at 22:22
  • I'm using openpyxl. All I do is iterate over rows/columns and manipulate the data, split text, and put it together in different order etc, extract things from it, store in new column but I have 10,000 of those and it takes longer than I thought to get this done. Not sure where I'd implement numpy with openpyxl – Michael 1022 Dec 01 '16 at 22:26
  • Thank you. Boost Python seems like a good solution. A very simple follow up: if I had written and compiled a program in C/C++ into an executable. I can just call os.startfile() in python to run it, correct? – Michael 1022 Dec 01 '16 at 22:29
  • Have you profiled your code yet? – Ignacio Vazquez-Abrams Dec 01 '16 at 22:32

1 Answers1

2

Let's say we want to write a Python script that acts as a wrapper on top of a C binary and passes arguments from the terminal to the C binary. first, create a test.c C program as follows:

#include <stdio.h>

int main(int argc, char *argv[])
{
   if(argc > 1)
   {
      int i;
      printf("C binary: ");
      for(i = 0; i < argc; i++)
         printf("%s ", argv[i]);
      printf("\n");
   }
   else
      printf("%s: No argument is provided!\n", argv[0]);
   return(0);
}

then compile it using:

gcc -o test test.c

and run it for two dummy arguments using:

./test arg1 arg2

Now, going back to your question. How I could pass arguments from Python to a C binary. First you need a Python script to read the arguments from the terminal. The test.py would do that for you:

import os
import sys

argc = len(sys.argv)
argv =  sys.argv

if argc > 2:
   cmd = './'
   for i in range(1,argc):
      cmd = cmd + argv[i] + ' '
   if os.path.isfile(argv[1]):
      print('Python script: ', cmd)
      os.system(cmd)
   else:
      print('Binary file does not exist')
      bin = 'gcc -o ' + argv[1] + ' '+ argv[1] + '.c'
      print(bin)
      os.system(bin)
      if os.path.isfile(argv[1]):
         os.system(cmd)
      else:
         print('Binary source does not exist')
         exit(0)
else:
   print('USAGE: python3.4', argv[0], " BINARY_FILE INPUT_ARGS");
   exit(0)

Having test.c and test.py in the same directory. Now, you can pass arguments from the terminal to the test C binary using:

python3.4 test.py test arg1 arg2

Finally, the output would be:

Python script:  ./test arg1 arg2
C binary: ./test arg1 arg2

Two last remarks:

  • Even if you don't compile the source code, the test.py will look for the test.c source file and try to compile it.
  • If you don't want to pass arguments from the terminal, you can always define the arguments in the Python script and pass them to the C binary.
hmofrad
  • 1,784
  • 2
  • 22
  • 28