3

I'm doing genetic programming framework and I need to be able to execute some string representing complete python programs. I'm using Python 2.7. I have a config class in which the primitive sets are defined. Lets say

class Foo():
    def a(self,x):
        return x

    def b(self,y):
        return y

I'm extracting the functions with the python inspect module and I want to create some executable source code with imports and everything. I end up with a string that looks like this

import sys

def a(x,y):
    return x

def b(y):
    return y

def main(x,y)
    lambda x,y: a(b(y),a(x,y))

main(*sys.argv)

My problem is that I don't know how to pass command line arguments to the string I'm running with eval(). How can I pass command line arguments to a source file I want to run with eval()?

Edit: There are millions of individuals so writing to a file is not a great option.

Edit: I made a mistake. The eval() method is used only for expressions and not statements so using exec() is the correct approach

7
  • 2
    What exactly are you trying to eval? Also your class defs don't have self as first arguments. Commented Jan 13, 2014 at 19:58
  • What do you mean by "individuals" in the phrase "execute individuals"? Commented Jan 13, 2014 at 19:59
  • 2
    @Dan I think he's killing people... genetically. Yikes! Commented Jan 13, 2014 at 20:01
  • You could implement something using partials or dynamically crated function/code objects, but if you want to stick with building things with a string and eval, just make your string a Formatter and run eval on iterations of your formatted string. Commented Jan 13, 2014 at 20:02
  • How are a and b to be extracted from Foo()? Your a() signature doesn't fit, btw. Since methods and functions in Python are first-class objects, you can just store references to them and call them as needed, no need to use eval() for that. Commented Jan 13, 2014 at 20:11

2 Answers 2

5
eval("function_name")(arg1, arg2)

or if you have a list of arguments:

arguments= [arg1,arg2,arg3,something]
eval("function_name")(*arguments)
Sign up to request clarification or add additional context in comments.

Comments

2

You have three options, roughly speaking. You can keep going with eval(),you could actually write the string as a file and execute it with subprocess.Popen(), or you could call the function something besides main() and call it after defining it with eval().

exec() way:

In the string you want to exec

main(#REPLACE_THIS#)

Function to evaluate

import string
def exec_with_args(exec_string,args):
    arg_string=reduce(lambda x,y:x+','+y,args)
    exec_string.replace("#REPLACE_THIS#", arg_string)

Subprocess way:

 import subprocess
 #Write string to a file
 exec_file=open("file_to_execute","w")
 exec_file.write(string_to_execute)
 #Run the python file as a separate process
 output=subprocess.Popen(["python","file_to_execute"].extend(argument_list),
     stdout=subprocess.PIPE)

Function Definition Way

In the string you want to exec

def function_name(*args):
    import sys

    def a(x,y):
        return x

    def b(y):
        return y

    def inner_main(x,y):
        lambda x,y: a(b(y),a(x,y))

    inner_main(*args)

Outer code

exec(program_string)
function_name(*args)

7 Comments

I'm generating 1 million individuals and more, writing to a file will slow down the process way too much I've kept it as a final option in case there is no other solution. I will try the solution and let you know.
@George Kouzmov: It sounds like you're doing some pretty crazy metaprogramming. Have you considered using lisp, which is designed for this kind of situation?
@George Kouzmov: I added another method, which I think may be superior to the first two. I don't know much about code generation, though, so let me know if you think it would work.
It's a proof of concept project. I'm using a DEAP framework for genetic programming which is doing most of the work. However I'm evaluating the individuals over a network hence the need of source code. Problem is DEAP doesn't provide that functionality so I want to generate the individuals so I can execute them easily without the need of the framework and all the frameworks I'm using are in python or at least the best ones so this is not an option.
Just 10 minutes ago I figured out the third solution however I found out that eval() has problem with function definitions you can only work with expressions so this is a mistake and I have to redefine my problem, apperanly exec is the better choice
|

Your Answer

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.