471,328 Members | 1,863 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,328 software developers and data experts.

Embedding python code into text document question.

Dear all,

I've written a program that parses a string or file for embedded python
commands, executes them and fills in the returned value. The input might
look like this:

process id: $$return os.getpid()$$
current date: $$return time.ctime()$$
superuser: $$
if os.geteuid():
return "Yes"
else:
return "No"$$

I've tried several solutions using eval, execfile or compile, but none
of those would solve my problem. Does anyone have a solution that works?
Any suggestions? Any help will be appreciated :)

Regards,
Thomas.
Jan 10 '08 #1
5 840
On Jan 10, 2:10 pm, Thomas Troeger <thomas.troeger....@siemens.com>
wrote:
Dear all,

I've written a program that parses a string or file for embedded python
commands, executes them and fills in the returned value. The input might
look like this:

process id: $$return os.getpid()$$
current date: $$return time.ctime()$$
superuser: $$
if os.geteuid():
return "Yes"
else:
return "No"$$

I've tried several solutions using eval, execfile or compile, but none
of those would solve my problem. Does anyone have a solution that works?
Any suggestions? Any help will be appreciated :)

AST visitor approach:

http://aspn.activestate.com/ASPN/Coo.../Recipe/440629
Jan 10 '08 #2
On Thu, 10 Jan 2008 14:10:05 +0100, Thomas Troeger wrote:
I've written a program that parses a string or file for embedded python
commands, executes them and fills in the returned value. The input might
look like this:

process id: $$return os.getpid()$$
current date: $$return time.ctime()$$
superuser: $$
if os.geteuid():
return "Yes"
else:
return "No"$$

I've tried several solutions using eval, execfile or compile, but none
of those would solve my problem. Does anyone have a solution that works?
Any suggestions? Any help will be appreciated :)
My suggestion would be: use one of the many already existing templating
systems.

Ciao,
Marc 'BlackJack' Rintsch
Jan 10 '08 #3
On Jan 10, 1:10 pm, Thomas Troeger <thomas.troeger....@siemens.com>
wrote:
Dear all,

I've written a program that parses a string or file for embedded python
commands, executes them and fills in the returned value. The input might
look like this:

process id: $$return os.getpid()$$
current date: $$return time.ctime()$$
superuser: $$
if os.geteuid():
return "Yes"
else:
return "No"$$

I've tried several solutions using eval, execfile or compile, but none
of those would solve my problem. Does anyone have a solution that works?
Any suggestions? Any help will be appreciated :)
You could wrap the bits of code in a def statement and then exec it:
>>print field
return os.getpid()
>>exec("def field_func():\n" + "".join(" %s\n" % line for line in field.splitlines()))
print field_func()
3904
Jan 10 '08 #4
Thomas Troeger wrote:
I've written a program that parses a string or file for embedded python
commands, executes them and fills in the returned value. The input might
look like this:

process id: $$return os.getpid()$$
current date: $$return time.ctime()$$
superuser: $$
if os.geteuid():
return "Yes"
else:
return "No"$$

I've tried several solutions using eval, execfile or compile, but none
of those would solve my problem. Does anyone have a solution that works?
Any suggestions? Any help will be appreciated :)
What you're looking for is a templating system for Python. There are
already many with varying degrees of complexity and emphasis, including
one I've put together, EmPy:

http://www.alcyone.com/software/empy/

For more, google around for Python templating.

--
Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
San Jose, CA, USA && 37 18 N 121 57 W && AIM, Y!M erikmaxfrancis
If the sun comes up / And you're not home / I'll be strong
-- India Arie
Jan 10 '08 #5
Thanks guys, you've helped me very much :) Cheers & happy new year!
Jan 11 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Alicia Haumann | last post: by
2 posts views Thread by Roose | last post: by
1 post views Thread by amit | last post: by
1 post views Thread by jeremito | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.