472,975 Members | 1,363 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Exec Multiple Lines?

I'd like to dynamically execute multiple lines of indented code from
within a script, but I can't seem to find a suitable function. Exec only
works with unindented code, and execfile only works with files. I
suppose I could write my string to a temporary file and then use
execfile, but that seems like a hack. Is there an easier way? Any help
is appreciated.
Jul 18 '05 #1
2 6640
On Sat, 12 Jun 2004 02:26:39 -0400, Chris S. wrote:
I'd like to dynamically execute multiple lines of indented code from
within a script, but I can't seem to find a suitable function. Exec only
works with unindented code, and execfile only works with files. I
suppose I could write my string to a temporary file and then use
execfile, but that seems like a hack. Is there an easier way? Any help
is appreciated.


txt = """
class myclass:
def testf(s,x,y):
print "testf called with %s,%s"%(x,y)
"""

exec(compile(txt,"-","exec"))

a = myclass()
a.testf("var1","var2")
Jul 18 '05 #2
Chris S. wrote:
I'd like to dynamically execute multiple lines of indented code from
within a script, but I can't seem to find a suitable function. Exec only
works with unindented code, and execfile only works with files. I
suppose I could write my string to a temporary file and then use
execfile, but that seems like a hack. Is there an easier way? Any help
is appreciated.


Either dedent or trick Python into expecting indented code:
s = """ .... print "and I say hello"
.... print "hello, hello"
.... """ exec s Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<string>", line 2
print "and I say hello"
^
SyntaxError: invalid syntax exec "if 1:\n%s" % s and I say hello
hello, hello


Peter

Jul 18 '05 #3

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

Similar topics

5
by: PeterF | last post by:
Hi, I've got the following problem: exec() and shell_exec(), etc. don't give anything back, just the error code 127 (command not found?!) For example, the following script: <?php exec("cat...
3
by: Jerry | last post by:
I've found a problem with exec, passthru, shell_exec & system. I'm trying to run the following exec("sort -r -n -k2,2 r1.txt > r2.txt") with r1.txt being a numeric file. The file looks like...
2
by: D. Alvarado | last post by:
Hello, I am running PHP 4 on Apache 1.31 for Fedora Core 2 Linux. I run a process exec("/bin/sh $my_script", $output, $return); that takes about 15-20 seconds but produces multiple lines of...
3
by: John Bowling | last post by:
I have a java (2.0) program with the following lines: String cmdArray1 = {"lp", "-d", "hp4m", "MyFile"}; System.out.println(Runtime.getRuntime().exec(cmdArray1)); It compliles properly, but...
0
by: Jan | last post by:
I store sql-commands in a database table. In the first step I get the sql command out of the database table with embedded sql. In the second step I try to execute the command, which i got from the...
5
by: snicks | last post by:
I'm trying to exec a program external to my ASP.NET app using the following code. The external app is a VB.NET application. Dim sPPTOut As String sPPTOut = MDEPDirStr + sID + ".ppt" Dim p As...
5
by: TPJ | last post by:
I have the following code: ----------------------------------- def f(): def g(): a = 'a' # marked line 1 exec 'a = "b"' in globals(), locals() print "g: a =", a
31
by: eliben | last post by:
Hello, In a Python program I'm writing I need to dynamically generate functions and store them in a dict. eval() can't work for me because a function definition is a statement and not an...
7
by: gregory.lielens | last post by:
Hi, I am using a small python file as an input file (defining constants, parameters, input data, ...) for a python application. The input file is simply read by an exec statement in a specific...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.