473,385 Members | 1,796 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 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 891
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Alicia Haumann | last post by:
I accidentally sent this to webmaster@python.org, so this could be a duplicate if "webmaster" forwards it to this list. :{ Hi, there. Thanks for any help that can be offered. I've been...
2
by: Roose | last post by:
With some googling I have found these resources: http://docs.python.org/ext/win-dlls.html http://www.python.org/doc/faq/windows.html I have a large Win32/MFC/C/C++ application that has an...
1
by: Craig Ringer | last post by:
Hi folks I'm a bit of a newbie here, though I've tried to appropriately research this issue before posting. I've found a lot of questions, a few answers that don't really answer quite what I'm...
4
by: FC | last post by:
Hello folks, I was wondering if there any other method of achieving the following: I have a XSL transformation outputting a SVG document. For reasons too long to explain here, I must embed a CSS...
1
by: amit | last post by:
Hello, I am currently studying how to embedd python. I am developing a graphical C++ application. My goal is to embedd python script that will control some kind of animation. I have some...
5
by: Avi Kak | last post by:
Folks, Does regular expression processing in Python allow for executable code to be embedded inside a regular expression? For example, in Perl the following two statements $regex =...
1
by: jeremito | last post by:
I am trying to learn how to extend and/or embed Python. I have looked at the document "Extending and Embedding the Python Interpreter" and also "Python/C API Reference Manual. In the examples...
6
by: Qun Cao | last post by:
Hi Everyone, I am a beginner on cross language development. My problem at hand is to build a python interface for a C++ application built on top of a 3D game engine. The purpose of this python...
2
by: bappai | last post by:
Hello, I am trying to actually call a GUI from my C++ code which would have buttons and therefore can call functions from C++ again, ie extend the C++ code also. I am faced with a peculiar...
1
by: neovantage | last post by:
Hey all, I am using a PHP script which creates headings at run time in a sense at page execution. I am stuck a with a very little problem which i am sure i will have the solution from experts. ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.