marina
marina

Reputation: 11

Running executable through python confuses interpreter/OS

I have the following code:

#!/usr/bin/python
import sys
import subprocess
import random
import time

if len(sys.argv) != 7:
    print "Usage: " + sys.argv[0] + " <executable> r m n p a"
    sys.exit(1)

myexecutable = sys.argv[1]
r = int(sys.argv[2])
list_m = map(int, sys.argv[3].split(","))
list_n = map(int, sys.argv[4].split(","))
list_p = map(float, sys.argv[5].split(","))
list_a = map(int, sys.argv[6].split(","))

r1 = random.Random()

for m in list_m:
    for n in list_n:
        for p in list_p:
            for a in list_a:
                if a <= m:
                    for i in range(r):
                        print '%04.3f' % (r1.randint(1, 100))
                        seed = time.time()
                        r1 = random.Random(seed)
                        print m, n, p, a, i
                        command = myexecutable + " -seed "+ str(r1) + " -m "+ str(m) + " -n "+ str(n) +" -p "+ str(p) +" -a "+ str(a)
                        f = open("seed_" + str(r1) + "-m_" + str(m) + "-n_" + str(n) + "-p_" + str(p) + "-a_" + str(a)+ "-i_" + str(i) + ".xml", "w")
                        subprocess.call(command, shell=True, stdout=f)
                        f.close()

"Myexecutable" is a working simple program written in c++ that doesn't invoke any sophisticated library, besides boost options. Everything seems fine with it.

When I run the following code with

python generate.py

I get the peculiar, unbelievable error:

/bin/sh: 1: cannot open random.Random: No such file

If I comment the random-relevant lines and put a fixed value to r1 then I get the indendation error following (lol) :

f = open("-s_" + str(r1) + "-m_" + str(m) + "-n_" + str(n) + "-p_" + str(p) + "-a_" + str(a)+ "-i_" + str(i) + ".xml", "w") ^ IndentationError: unexpected indent

The arrow points to -a_ so it can't be an indentation error, since it is in the middle of the line right?

So I clean my project, copy to a different directory, remake and then I rerun the script. Now the loop is running normally (console output and files created) but the executable doesn't seem to run (files empty) and in every loop iteration I am still getting the error:

/bin/sh: 1: cannot open random.Random: No such file

amo If I put the comments again as mentioned above I get! :

Error: the argument ('eed') for option '--seed' is invalid

Well is this an apparent memory leak (buffer/stack overflow) by the c++ executable?

Upvotes: 0

Views: 191

Answers (1)

Foo Bar User
Foo Bar User

Reputation: 2491

str(random.Random(3)) returns '<random.Random object at 0x1783870>' so i guess what you are looking for is this:

r1 = random.Random(seed)
actual_random = r1.random()
print(actual_random) # prints 0.08487199515892163 etc
print(str(r.random()).split(".")[1]) # prints 08487199515892163

for identation read pep8

also you may want to concatinate strings in this way, it looks cleaner. at least thats what i prefer:

lot_of_staff = [1,10,2,3,5,6,7,8]
conc="".join([str(i) for i in lot_of_staff])
print(conc)# prints '110235678'

list comprehensions and string.join

Upvotes: 1

Related Questions