473,394 Members | 1,746 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,394 software developers and data experts.

Help with use of code.InteractiveInterpreter for multiline code

I'm trying to embed a Python interpreter in a GUI I'm developing, and
I'm having trouble understanding the proper use of
code.InteractiveInterpreter.

Here's what I'm trying:

% python
Python 2.3 (#1, Sep 13 2003, 00:49:11)
[GCC 3.3 20030304 (Apple Computer, Inc. build 1495)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
from code import InteractiveInterpreter
a = InteractiveInterpreter()
a.runsource('a = 0') False a.runsource('b = 0') False a.runsource('print a,b') 0 0
False a.runsource('def q():') True a.runsource(' print "hi"')

File "<input>", line 1
print "hi"
^
SyntaxError: invalid syntax
False
What's the proper way to call the interpreter instance for a multiline
example like this?

Thanks in advance,

Rick

Jul 18 '05 #1
2 2595

Take a look at the source code of the code module (file code.py in the
lib directory). The push() method of the InteractiveConsole class
shows how to handle multi-line statements.

Basically, keep collecting lines as long as the result returned by the
runsource() call is true. Maybe instead of calling the runsource()
method, just call the push() method.

/Jean Brouwers
In article <11*********************@z14g2000cwz.googlegroups. com>,
<"ri*********@yahoo.com"> wrote:
I'm trying to embed a Python interpreter in a GUI I'm developing, and
I'm having trouble understanding the proper use of
code.InteractiveInterpreter.

Here's what I'm trying:

% python
Python 2.3 (#1, Sep 13 2003, 00:49:11)
[GCC 3.3 20030304 (Apple Computer, Inc. build 1495)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
from code import InteractiveInterpreter
a = InteractiveInterpreter()
a.runsource('a = 0') False a.runsource('b = 0') False a.runsource('print a,b') 0 0
False a.runsource('def q():') True a.runsource(' print "hi"')

File "<input>", line 1
print "hi"
^
SyntaxError: invalid syntax
False
What's the proper way to call the interpreter instance for a multiline
example like this?

Thanks in advance,

Rick

Jul 18 '05 #2
ri*********@yahoo.com wrote:
I'm trying to embed a Python interpreter in a GUI I'm developing, and
I'm having trouble understanding the proper use of
code.InteractiveInterpreter.
[examples of calling the interpreter instance] What's the proper way to call the interpreter instance for a multiline
example like this?

Thanks in advance,

Rick


I haven't worked with the code module before, but here is my go at it.
It seems that the call to runsource() needs the entire multiline code at
once, with newlines including a trailing newline:
import code
a = code.InteractiveInterpreter()
a.runsource('def q():\n print "hi, a"\n') False a.runsource('q()') hi, a
False

With the InteractiveConsole, you can push each line of code
individually, like so:
import code
b = code.InteractiveConsole()
b.push('def q():') True b.push(' print "hi, b"') True b.push('') False b.runsource('q()')

hi, b
False

HTH,
Jim
Jul 18 '05 #3

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

Similar topics

0
by: Rasmus Fogh | last post by:
Dear All, I need a way of writing strings or arbitrary Python code that will a) allow the strings to be read again unchanged (like repr) b) write multiline strings as multiline strings instead...
1
by: Philippe C. Martin | last post by:
Hi, I'm looking for a very simple way to figure out if a line of code requires further lines of code ex: for i in range(10): So far I have found the following trick that seem dangerous to me...
13
by: Yatima | last post by:
Hey Folks, I've got some info in a bunch of files that kind of looks like so: Gibberish 53 MoreGarbage 12 RelevantInfo1 10/10/04
2
by: Mortimer Schnurd | last post by:
Hi All, I am a VB 6 programmer who is now trying to learn C#. In doing so, I am trying to convert some of my VB modules to C#. I routinely user Reg Expressions in VB and am having some trouble...
1
by: Rahul | last post by:
Hi Everybody I have some problem in my script. please help me. This is script file. I have one *.inq file. I want run this script in XML files. But this script errors shows . If u want i am...
53
by: jaso | last post by:
Can you give any comments on this code? I used one goto, is it bad? #include <stdio.h> #include <stdlib.h> #include <ctype.h> #include <string.h> #include <assert.h> #define NOT_NULL 1
5
by: Ian Tedridge | last post by:
If I have 3 textboxes on a form plus 1 multiline textbox. How can I add the text from textbox 1 to 3 to the multiline textbox ? I can add the 3 textbox contents to one line of the multiline...
0
by: CaJack | last post by:
Hi, I’m having trouble with my code and I’m starting to get lost. The Program takes the country that’s been typed in to the textbox and when the query button is clicked the program queries an access...
2
by: Mike | last post by:
I am trying to write a little program for my own use using VB2005 express edition. I have a list of peoples names in a file that I read into an array of strings. I am using a multiline textbox to...
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
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.