Using Python to Execute a C or FORTRAN Program (Windows)

W

W. eWatson

I think Python is capable of executing a compiled C or FORTRAN program,
and maybe even getting some parameters passed back. Does anyone have a
example of how this might be done? I'm running under Win XP Pro.
 
M

Mensanator

I think Python is capable of executing a compiled C or FORTRAN program,

Sure, if it was compiled to an .exe file.
and maybe even getting some parameters passed back.

Sure, if the program prints to stdout.
Does anyone have a
example of how this might be done? I'm running under Win XP Pro.

Here's one. The test program is factor.exe (included in
the MIRACL library). I recompiled it (factor!.exe) to
produce consitent output.

Instead of stupidly saying

C:\factor!\miracl\source>factor 31
31
this number is prime!

I now have it print

C:\factor!\miracl\source>factor! 31
PRIME_FACTOR 31

I now call it from Python and have Python fix its mistakes.

## Mistakes? - numbers reported as COMPOSITE may still
## be factorable. Composites are not returned to the
## start of the algorithm for re-factoring. But that's ok,
## if I don't like it, I'm free to fix it myself since
## I have the source code (so says the author).
##
## Hey, I won't look a gift horse in the mouth, but you
## are advised that when you see...
##
## C:\factor!\miracl\source>factor! 5081842980034330599
## 30221143303110332712493139579190463526792062
## 62204589342623811236647989889145173098650749
##
## PRIME_FACTOR 37
## PRIME_FACTOR 43
## PRIME_FACTOR 167
## COMPOSITE_FACTOR 507787751
## PRIME_FACTOR 69847
## PRIME_FACTOR 30697
## PRIME_FACTOR 89017
## PRIME_FACTOR 3478697
## PRIME_FACTOR 434593
## PRIME_FACTOR 49998841
## PRIME_FACTOR 161610704597143
## PRIME_FACTOR 14064370273
## COMPOSITE_FACTOR 963039394703598565337297
## PRIME_FACTOR 11927295803
##
## ...you should follow that with...
##
## C:\factor!\miracl\source>factor! 507787751
## PRIME_FACTOR 29819
## PRIME_FACTOR 17029
##
## C:\factor!\miracl\source>factor! 963039394703598565337297
## PRIME_FACTOR 518069464441
## PRIME_FACTOR 1858900129817



import os
import random
import time

factor_program = 'factor! -d200 '

the_composites =
[['COMPOSITE_FACTOR','50818429800343305993022114330311033271249313957919046352679206262204589342623811236647989889145173098650749']]

##random_digits = [str(random.randint(0,9)) for d in range(30)]
##test_number = ''.join(random_digits)
##the_composites = [['COMPOSITE_FACTOR',test_number]]

the_primes = []
the_intractables = []

phase = 1
the_times = []
while the_composites:
print "="*40
print 'Phase',phase
the_comp = the_composites.pop(0)
print the_comp
print
the_times.append(time.time()) # time how long it takes to run
factor!.exe
the_output = os.popen(factor_program+the_comp[1]).readlines()
the_times.append(time.time())
new_factors = [i.split() for i in the_output]
for i in new_factors: print i
print
if len(new_factors) == 1:
# it's prime or intractable
if new_factors[0][0] == 'PRIME_FACTOR':
the_primes.append([new_factors[0][0],long(new_factors[0][1])])
else:
the_intractables.append([new_factors[0][0],long(new_factors[0]
[1])])
new_factors.pop()
while new_factors:
j = new_factors.pop(0)
if j[0] == 'PRIME_FACTOR':
the_primes.append([j[0],long(j[1])])
else:
the_composites.append(j)
print the_times[phase] - the_times[phase-1],'seconds'
phase += 1

print "="*40
print
print 'Factoring complete'
print

the_primes.sort()
the_intractables.sort()
the_primes.extend(the_intractables)

for i in the_primes:
print i[0],i[1]
print
print "="*40

## ========================================
## Phase 1
## ['COMPOSITE_FACTOR',
'50818429800343305993022114330311033271249313957919046352679206262204589342623811236647989889145173098650749']
##
## ['PRIME_FACTOR', '37']
## ['PRIME_FACTOR', '43']
## ['PRIME_FACTOR', '167']
## ['COMPOSITE_FACTOR', '507787751']
## ['PRIME_FACTOR', '69847']
## ['PRIME_FACTOR', '30697']
## ['PRIME_FACTOR', '89017']
## ['PRIME_FACTOR', '3478697']
## ['PRIME_FACTOR', '434593']
## ['PRIME_FACTOR', '49998841']
## ['PRIME_FACTOR', '161610704597143']
## ['PRIME_FACTOR', '14064370273']
## ['COMPOSITE_FACTOR', '963039394703598565337297']
## ['PRIME_FACTOR', '11927295803']
##
## ========================================
## Phase 2
## ['COMPOSITE_FACTOR', '507787751']
##
## ['PRIME_FACTOR', '29819']
## ['PRIME_FACTOR', '17029']
##
## ========================================
## Phase 3
## ['COMPOSITE_FACTOR', '963039394703598565337297']
##
## ['PRIME_FACTOR', '518069464441']
## ['PRIME_FACTOR', '1858900129817']
##
## ========================================
##
## Factoring complete
##
## PRIME_FACTOR 37
## PRIME_FACTOR 43
## PRIME_FACTOR 167
## PRIME_FACTOR 17029
## PRIME_FACTOR 29819
## PRIME_FACTOR 30697
## PRIME_FACTOR 69847
## PRIME_FACTOR 89017
## PRIME_FACTOR 434593
## PRIME_FACTOR 3478697
## PRIME_FACTOR 49998841
## PRIME_FACTOR 11927295803
## PRIME_FACTOR 14064370273
## PRIME_FACTOR 518069464441
## PRIME_FACTOR 1858900129817
## PRIME_FACTOR 161610704597143
##
## ========================================
 
W

W. eWatson

Mensanator said:
Sure, if it was compiled to an .exe file.


Sure, if the program prints to stdout.


Here's one. The test program is factor.exe (included in
the MIRACL library). I recompiled it (factor!.exe) to
produce consitent output.
....
Thanks. OK, I think I can follow that. I want to pass it along to
someone who either missed this possibility in some coding, ignored it,
or felt more comfortable about just writing the whole program from
scratch in c++. His program was originally written in Python, but a new
hardware device (capture card) had no good interface with Python, so he
wrote it in C++, which does. From my knowledge of the Python program
before the entry of c++, it seems he could have farmed out the hardware
interface in much the same way he had done it before with a capture card
well know to him.

Would the same Python interface work for a compiled C++ program?
 
S

sturlamolden

His program was originally written in Python, but a new
hardware device (capture card) had no good interface with Python, so he
wrote it in C++, which does. From my knowledge of the Python program
before the entry of c++, it seems he could have farmed out the hardware
interface in much the same way he had done it before with a capture card
well know to him.

This sounds a bit incompetent.

Why didn't he just write an extension module in C++?

Also, if you don't know how to spawn a subprocess, I'd question your
competence as well.
 
M

Mensanator

...
Thanks. OK, I think I can follow that. I want to pass it along to
someone who either missed this possibility in some coding, ignored it,
or felt more comfortable about just writing the whole program from
scratch in c++. His program was originally written in Python, but a new
hardware device (capture card) had no good interface with Python, so he
wrote it in C++, which does. From my knowledge of the Python program
before the entry of c++, it seems he could have farmed out the hardware
interface in much the same way he had done it before with a capture card
well know to him.

Would the same Python interface work for a compiled C++ program?

If it runs from the command line and writes to stdout, I don't
why it wouldn't.
 
W

W. eWatson

sturlamolden said:
This sounds a bit incompetent.

Why didn't he just write an extension module in C++?

Also, if you don't know how to spawn a subprocess, I'd question your
competence as well.

I can't read his mind.
 
G

Gib Bogle

W. eWatson said:
...
Thanks. OK, I think I can follow that. I want to pass it along to
someone who either missed this possibility in some coding, ignored it,
or felt more comfortable about just writing the whole program from
scratch in c++. His program was originally written in Python, but a new
hardware device (capture card) had no good interface with Python, so he
wrote it in C++, which does. From my knowledge of the Python program
before the entry of c++, it seems he could have farmed out the hardware
interface in much the same way he had done it before with a capture card
well know to him.

Would the same Python interface work for a compiled C++ program?

I am working on a Python UI (PyQt4) to a Fortran program. The approach I've
taken is to build the Fortran code as a DLL (or .so). Python can load the
library and call a procedure that initiates execution. I'm using sockets to
pass data of different types back to the Python program. This is a work in
progress, and I'm learning Python as we go.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top