Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > how can I execute a function string

Reply
Thread Tools

how can I execute a function string

 
 
Rajarshi Guha
Guest
Posts: n/a
 
      01-13-2004
Hi ,
I have some code that generates a function on the fly in a string.
At a later point in time I want to execute this function (which also
requires a parameters to be passed to it). So the code is something like
this:

def generate_func():
s = """ def function(x):
print x
return 2
"""
return s

funcstring = generate_func()
retval = ....

That is, retval should have the value returned from the evaluation of the
function in the string funcstring.

Is this possible by means of simple function calls or does this involve
some sort of black magic?

Thanks,
 
Reply With Quote
 
 
 
 
Francis Avila
Guest
Posts: n/a
 
      01-13-2004
Rajarshi Guha wrote in message ...
>Hi ,
> I have some code that generates a function on the fly in a string.
>At a later point in time I want to execute this function (which also
>requires a parameters to be passed to it). So the code is something like
>this:
>
>def generate_func():
> s = """ def function(x):
> print x
> return 2
>"""
> return s
>
>funcstring = generate_func()
>retval = ....
>
>That is, retval should have the value returned from the evaluation of the
>function in the string funcstring.
>
>Is this possible by means of simple function calls or does this involve
>some sort of black magic?
>
>Thanks,


What is it with code-generation this week?

Anyway, the exec statement (not function) will execute the contents of a
string or code object as though it were inline code:

E.g.:
>>> x

Traceback (most recent call last):
....
NameError: name 'x' is not defined
>>> exec 'x = 1'
>>> x

1
>>>


Use like so:

exec generate_func()
# 'function' is now in your namespace.
function('hello') # should print 'hello' and return 2.

A *slightly* better approach is to make a code object, using compile().

def gen_func():
s = '''
def f(x):
return x
'''
return compile(s, '<source>', 'single')

exec gen_func() # f gets dumped into your namespace.

If you're very ambitious, you can generate an AST tree and compile that: see
the compile module. In this simple case there's no advantage, however.

I'll have to think about how to turn the raw source of a function definition
into a callable. I'm not sure it's possible in a 100% reliable manner.
Here's a first attempt (untested):

def makecallable(source):
"""Return a function from string source.

'def <funcname>' must be the first tokens in source!

"""
co = compile(source, '<makecallable()>', 'single')
funcname = co.co_varnames[0] #This is VERY brittle!!
fakelocals = {}
exec co in globals(), fakelocals
return fakelocals[funcname]

Now that I've told you about exec, I'll tell you that you almost certainly
shouldn't be using it. There is most likely a better way to do what you're
doing than building code strings and executing them. Python has a lot of
reflection, introspection, and dynamic object generation capabilities, which
generally remove the need for using exec, which is slow, hackish, and
error-prone. Please tell us more about what you're doing.
--
Francis Avila

 
Reply With Quote
 
 
 
 
Hans Nowak
Guest
Posts: n/a
 
      01-13-2004
Rajarshi Guha wrote:

> Hi ,
> I have some code that generates a function on the fly in a string.
> At a later point in time I want to execute this function (which also
> requires a parameters to be passed to it). So the code is something like
> this:
>
> def generate_func():
> s = """ def function(x):
> print x
> return 2
> """
> return s
>
> funcstring = generate_func()
> retval = ....
>
> That is, retval should have the value returned from the evaluation of the
> function in the string funcstring.
>
> Is this possible by means of simple function calls or does this involve
> some sort of black magic?


It's possible:

>>> def generate_func():

.... s = """def f(x):
.... print x
.... return 2"""
.... d = {}
.... exec s in d
.... return d['f']
....
>>>
>>> g = generate_func()
>>> g(4)

4
2
>>>


....but this way of doing it has several drawbacks. For example, exec is picky
about the strings it accepts; e.g. if you leave a trailing newline you get a
syntax error. Compare:

>>> def generate_func():

.... s = """def f(x):
.... print x
.... return 2
.... """
.... d = {}
.... exec s in d
.... return d['f']
....
>>> g = generate_func()

Traceback (most recent call last):
File "<input>", line 1, in ?
File "<input>", line 7, in generate_func
File "<string>", line 4

^
SyntaxError: invalid syntax

Also, if the string containing the syntax comes from an "untrusted source",
then it's a potential security hazard.

I'm not sure what you're trying to do here, but you might want to consider
generating an actual function, rather than a string to be evaluated:

>>> def generate_func():

.... def f(x):
.... print x
.... return 2
.... return f
....
>>> h = generate_func()
>>> h(5)

5
2

HTH,

--
Hans ((E-Mail Removed))
http://zephyrfalcon.org/



 
Reply With Quote
 
Rajarshi Guha
Guest
Posts: n/a
 
      01-14-2004
On Mon, 12 Jan 2004 21:23:20 -0500, Francis Avila wrote:

> Rajarshi Guha wrote in message ...
>>Hi ,
>> I have some code that generates a function on the fly in a string.
>>At a later point in time I want to execute this function (which also
>>requires a parameters to be passed to it). So the code is something like
>>this:

[
> If you're very ambitious, you can generate an AST tree and compile that:
> see the compile module. In this simple case there's no advantage,
> however.
>
> I'll have to think about how to turn the raw source of a function
> definition into a callable. I'm not sure it's possible in a 100%
> reliable manner. Here's a first attempt (untested):
>
> def makecallable(source):
> """Return a function from string source.
>
> 'def <funcname>' must be the first tokens in source!
>
> """
> co = compile(source, '<makecallable()>', 'single') funcname =
> co.co_varnames[0] #This is VERY brittle!! fakelocals = {} exec co in
> globals(), fakelocals
> return fakelocals[funcname]
>


Well I was playing with Andrews curves which allows one to visualize a
vector of numbers in terms of a Fourier series. So basically I was
creating a string that ontained the Fourier representation of the vector
and then evaluating the resultant function.

I know this can be done simpler using loops - I jst wanted to see how I
could do this using a bit of code generation
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Given a string - execute a function by the same name python@bdurham.com Python 6 05-08-2008 04:47 PM
Can i execute aspx file in my asp pag with server.execute method(sorry) Savas Ates ASP General 1 08-17-2004 04:52 PM
Re: To execute a string as function Larry__Weiss C Programming 0 06-24-2003 08:14 PM
Re: To execute a string as function Dan Pop C Programming 6 06-24-2003 05:04 PM



Advertisments