473,545 Members | 1,893 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 895
On Jan 10, 2:10 pm, Thomas Troeger <thomas.troeger ....@siemens.co m>
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.co m>
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.splitline s()))
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
2764
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 working with Python for a year or more now, but only doing simple extending in C/C++. I'm now attempting some embedding and several questions have come...
2
2854
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 embedded scripting language (a very limited one). I would like to rip it out and replace it with Python. I am thinking that this would be relatively...
1
3165
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 looking for, but nothing that really solves or explains all this. I'll admit to being stumped, hence my question here. I'm also trying to make...
4
3296
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 style sheet inside a <style> element in the SVG output document using a CDATA block. The CSS stylesheet is not an xml document, therefore I cannot...
1
1899
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 questions about python embedding: 1) Is there a good text book or other resource on embedding/extending? (I find it hard to learn only by the tutorial...
5
2303
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 = qr/hello(?{print "saw hello\n"})mello(?{print "saw mello\n"})/; "jellohellomello" =~ /$regex/;
1
1843
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 shown in "Extending..." there are some things I ma not familiar with so I turn to the index in the Reference Manual, but they are not listed. For...
6
2987
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 interface is providing a convenient scripting toolkit for the application. One example is that a user can write a python script like: player =...
2
2310
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 problem. I actually tried out an embedding code (downloaded from the net) which actually calls any function which has a text display but when I try to...
1
3382
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. The problem is when it creates transparent PNG format image then and it pixel ate the image. e.g. If i am using a gradient in background then it...
0
7486
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7416
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7676
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7442
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5347
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4965
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3473
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3456
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1032
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.