473,378 Members | 1,539 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,378 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 6682
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...
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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.