473,406 Members | 2,273 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,406 software developers and data experts.

Executing a script created by the end user

I am working on a python project where an object will have a script that
can be edited by the end user: object.script

If the script is a simple one with no functions, I can easily execute it
using:
exec object.script

But if the object script is a bit more complicated, such as the example
below, my approach does not work:

def main():
hello1()
hello2()

def hello1():
print 'hello1'

def hello2():
print 'hello2'
Jul 18 '05 #1
2 1048
Craig Howard wrote:
I am working on a python project where an object will have a script that
can be edited by the end user: object.script

If the script is a simple one with no functions, I can easily execute it
using:
exec object.script

But if the object script is a bit more complicated, such as the example
below, my approach does not work:

def main():
hello1()
hello2()

def hello1():
print 'hello1'

def hello2():
print 'hello2'


What do you want to do if you get a script like this? Run main? You
could do something like:

py> s = """
.... def main():
.... hello1()
.... hello2()
....
.... def hello1():
.... print 'hello1'
....
.... def hello2():
.... print 'hello2'
.... """
py> d = {}
py> exec s in d
py> d["main"]()
hello1
hello2

(Actually, you don't need to exec it in d, that's probably just good
practice.)

Steve
Jul 18 '05 #2
compile and eval is a good way to go.
Regards,

Fuzzy
http://www.voidspace.org.uk/python/index.shtml

Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Nick Coghlan | last post by:
Anyone playing with the CPython interpreter's new command line switch might have noticed that it only works with top-level modules (i.e. scripts that are directly on sys.path). If the script is...
15
by: Nick Coghlan | last post by:
Python 2.4's -m command line switch only works for modules directly on sys.path. Trying to use it with modules inside packages will fail with a "Module not found" error. This PEP aims to fix that...
3
by: PzYon | last post by:
Hey 2gether! I'm trying to execute a PERL script on the web server when the user presses a button in an ASP.NET Web Application using Visual Basic. The most obvious solution for me seemed to be...
4
by: chris.dunigan | last post by:
I'm looking for an example of how to execute an existing DTS­ package from an ASP (VB)script and would appreciate any and all response. ­I don't even know if it's possible Thanks - Chuck...
0
by: Ian Warford | last post by:
Hi, I have a problem with a PL/PerlU function trying to execute an external perl script. The script is suid to a non-root user and I am executing it in the PL/PerlU function as so : $rc =...
3
by: Mike | last post by:
Hi, I am trying to resize a HTML table through Javascript. When the user control loads the first time, the table is resized, but then it doesn't anymore. I am using the following code in the...
7
by: David R. | last post by:
Hello all, I would like to generate a Sql-Script-File and a Batch-File to execute the Batch-File over C#-Code and use the SQL-File as an Input-File for the command "osql" in the Batch-File. I...
0
by: Sebastien Dupont | last post by:
Hi, I'm having a problem with executing a subprogram from a Python script. The subprogram is written in VBS (os = Win Server 2003). When I execute the Python script manually, the VBS script...
1
by: lonewolfbw | last post by:
I have the following VBScript which I can execute from my workstation (I am a domain admin), in order to change the local computer's administrator password. However, I am noticing an oddity. ...
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: 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...
0
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,...
0
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,...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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...

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.