[Tutor] Redirecting Python Interpreter Output

Ketut Mahaindra KMahaindra@beijing.sema.slb.com
Sun, 11 Aug 2002 10:23:27 +0800


This message is in MIME format. Since your mail reader does not understand
this format, some or all of this message may not be legible.

--Boundary_(ID_h9VI79z7PdZK7gRSxuf97w)
Content-type: text/plain; charset=iso-8859-1
Content-transfer-encoding: 7BIT

Hi,

Thanks for the response ...
But now what if I want to catch the output from C++ ?
Anybody got any experience on this ?

So, for example I have a PyRun_SimpleString() or
PyRun_SimpleFile or PyObject_CallObject on my C++ code ...
Can I capture the output of the Python interpreter ?

thanks in advance

best regards

ito


-----Original Message-----
From: SA [mailto:sarmstrong13@mac.com]
Sent: Saturday, August 10, 2002 9:57 PM
To: Ketut Mahaindra; 'tutor@python.org'
Subject: Re: [Tutor] Redirecting Python Interpreter Output


On 8/10/02 6:15 AM, "Ketut Mahaindra" <KMahaindra@beijing.sema.slb.com>
wrote:


Hi, 

I am currently developing an application 
which embed / extend Python in C++ / MSVC 6.0 

If I embed the Python interpreter in my C++ code, is there any way to 
redirect the output of the interpreter or the output of the script ? 
For example I want to catch the output and display it in an edit box 
Any suggestions here ? 


I managed to do this with a little Python/Tkinter script. This is not the
best of scripts, but if you look in the "def expyth" function you will see
what you are looking for. t1 is the input text box and t2 is the output text
box. This script is designed to run the python code in t1 and display the
output in t2.

#!/usr/bin/env python

from Tkinter import *
import os
import commands
import sys
import tkSimpleDialog

class PyShell:
    
    def clearin(self):
        self.t1.delete(0.0,END)
    def clearout(self):
        self.t2.delete(0.0,END)
    def expyth(self):
        self.t2.delete(0.0, END)  #Clear Textbox 2, ready it for input.
        self.output = commands.getoutput("/sw/bin/python -c '%s'" %
self.t1.get(0.0, END).strip())  #run commands from textbox1 and capture
their output.
        self.t2.insert(END, self.output) #write output to textbox2
    def doSave(self):
        SaveDialog = Saving(self.f)
        filename = SaveDialog.getName()
        self.saveText(filename)
        del(saveDialog)
    def __init__(self, top):
        self.t1 = Text(top, height="12", width="84", font="Courier 12")
        self.t1.pack(side=TOP, pady=2)
        self.f = Frame(top)
        self.f.pack()
        self.b1 = Button(self.f, text="Execute", command=self.expyth)
        self.b1.pack(side=LEFT)
        self.b2 = Button(self.f, text="Clear Input", command=self.clearin)
        self.b2.pack(side=LEFT)
        self.b3 = Button(self.f, text="Clear Output", command=self.clearout)
        self.b3.pack(side=LEFT)
        self.b4 = Button(self.f, text="Save Input", command=self.doSave)
        self.b4.pack(side=LEFT)
        self.t2 = Text(top, height="12", width="84", bg="lightblue",
font="Courier 12")
        self.t2.pack(side=TOP, pady=2)

class Saving(tkSimpleDialog.Dialog):
    
    def body(self, master):
        Label(master, text="Directory:").grid(row=0)
        Label(master, text="Filename:").grid(row=1)
        self.e1 = Entry(master)
        self.e2 = Entry(master)
        self.e1.grid(row=0, column=1)
        self.e2.grid(row=1, column=1)
        return self.e1
    def apply(self):
        dir = self.e1.get()
        fn = self.e2.get()
        self.name = dir + fn
    def getName(self):
        return self.name

                    
root = Tk()
app = PyShell(root)
root.mainloop()


Hope this help you.
SA


-- 
"I can do everything on my Mac I used to on my PC. Plus a lot more ..."
-Me

--Boundary_(ID_h9VI79z7PdZK7gRSxuf97w)
Content-type: text/html; charset=iso-8859-1
Content-transfer-encoding: 7BIT

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<META NAME="Generator" CONTENT="MS Exchange Server version 5.5.2653.12">
<TITLE>RE: [Tutor] Redirecting Python Interpreter Output</TITLE>
</HEAD>
<BODY>

<P><FONT SIZE=2>Hi,</FONT>
</P>

<P><FONT SIZE=2>Thanks for the response ...</FONT>
<BR><FONT SIZE=2>But now what if I want to catch the output from C++ ?</FONT>
<BR><FONT SIZE=2>Anybody got any experience on this ?</FONT>
</P>

<P><FONT SIZE=2>So, for example I have a PyRun_SimpleString() or</FONT>
<BR><FONT SIZE=2>PyRun_SimpleFile or PyObject_CallObject on my C++ code ...</FONT>
<BR><FONT SIZE=2>Can I capture the output of the Python interpreter ?</FONT>
</P>

<P><FONT SIZE=2>thanks in advance</FONT>
</P>

<P><FONT SIZE=2>best regards</FONT>
</P>

<P><FONT SIZE=2>ito</FONT>
</P>
<BR>

<P><FONT SIZE=2>-----Original Message-----</FONT>
<BR><FONT SIZE=2>From: SA [<A HREF="mailto:sarmstrong13@mac.com">mailto:sarmstrong13@mac.com</A>]</FONT>
<BR><FONT SIZE=2>Sent: Saturday, August 10, 2002 9:57 PM</FONT>
<BR><FONT SIZE=2>To: Ketut Mahaindra; 'tutor@python.org'</FONT>
<BR><FONT SIZE=2>Subject: Re: [Tutor] Redirecting Python Interpreter Output</FONT>
</P>
<BR>

<P><FONT SIZE=2>On 8/10/02 6:15 AM, &quot;Ketut Mahaindra&quot; &lt;KMahaindra@beijing.sema.slb.com&gt; wrote:</FONT>
</P>
<BR>

<P><FONT SIZE=2>Hi, </FONT>
</P>

<P><FONT SIZE=2>I am currently developing an application </FONT>
<BR><FONT SIZE=2>which embed / extend Python in C++ / MSVC 6.0 </FONT>
</P>

<P><FONT SIZE=2>If I embed the Python interpreter in my C++ code, is there any way to </FONT>
<BR><FONT SIZE=2>redirect the output of the interpreter or the output of the script ? </FONT>
<BR><FONT SIZE=2>For example I want to catch the output and display it in an edit box </FONT>
<BR><FONT SIZE=2>Any suggestions here ? </FONT>
</P>
<BR>

<P><FONT SIZE=2>I managed to do this with a little Python/Tkinter script. This is not the best of scripts, but if you look in the "def expyth" function you will see what you are looking for. t1 is the input text box and t2 is the output text box. This script is designed to run the python code in t1 and display the output in t2.</FONT></P>

<P><FONT SIZE=2>#!/usr/bin/env python</FONT>
</P>

<P><FONT SIZE=2>from Tkinter import *</FONT>
<BR><FONT SIZE=2>import os</FONT>
<BR><FONT SIZE=2>import commands</FONT>
<BR><FONT SIZE=2>import sys</FONT>
<BR><FONT SIZE=2>import tkSimpleDialog</FONT>
</P>

<P><FONT SIZE=2>class PyShell:</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp; </FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp; def clearin(self):</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.t1.delete(0.0,END)</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp; def clearout(self):</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.t2.delete(0.0,END)</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp; def expyth(self):</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.t2.delete(0.0, END)&nbsp; #Clear Textbox 2, ready it for input.</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.output = commands.getoutput(&quot;/sw/bin/python -c '%s'&quot; % self.t1.get(0.0, END).strip())&nbsp; #run commands from textbox1 and capture their output.</FONT></P>

<P><FONT SIZE=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.t2.insert(END, self.output) #write output to textbox2</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp; def doSave(self):</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SaveDialog = Saving(self.f)</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; filename = SaveDialog.getName()</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.saveText(filename)</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; del(saveDialog)</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp; def __init__(self, top):</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.t1 = Text(top, height=&quot;12&quot;, width=&quot;84&quot;, font=&quot;Courier 12&quot;)</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.t1.pack(side=TOP, pady=2)</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.f = Frame(top)</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.f.pack()</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.b1 = Button(self.f, text=&quot;Execute&quot;, command=self.expyth)</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.b1.pack(side=LEFT)</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.b2 = Button(self.f, text=&quot;Clear Input&quot;, command=self.clearin)</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.b2.pack(side=LEFT)</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.b3 = Button(self.f, text=&quot;Clear Output&quot;, command=self.clearout)</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.b3.pack(side=LEFT)</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.b4 = Button(self.f, text=&quot;Save Input&quot;, command=self.doSave)</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.b4.pack(side=LEFT)</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.t2 = Text(top, height=&quot;12&quot;, width=&quot;84&quot;, bg=&quot;lightblue&quot;, font=&quot;Courier 12&quot;)</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.t2.pack(side=TOP, pady=2)</FONT>
</P>

<P><FONT SIZE=2>class Saving(tkSimpleDialog.Dialog):</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp; </FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp; def body(self, master):</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Label(master, text=&quot;Directory:&quot;).grid(row=0)</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Label(master, text=&quot;Filename:&quot;).grid(row=1)</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.e1 = Entry(master)</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.e2 = Entry(master)</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.e1.grid(row=0, column=1)</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.e2.grid(row=1, column=1)</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return self.e1</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp; def apply(self):</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; dir = self.e1.get()</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fn = self.e2.get()</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.name = dir + fn</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp; def getName(self):</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return self.name</FONT>
</P>

<P><FONT SIZE=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </FONT>
<BR><FONT SIZE=2>root = Tk()</FONT>
<BR><FONT SIZE=2>app = PyShell(root)</FONT>
<BR><FONT SIZE=2>root.mainloop()</FONT>
</P>
<BR>

<P><FONT SIZE=2>Hope this help you.</FONT>
<BR><FONT SIZE=2>SA</FONT>
</P>
<BR>

<P><FONT SIZE=2>-- </FONT>
<BR><FONT SIZE=2>&quot;I can do everything on my Mac I used to on my PC. Plus a lot more ...&quot;</FONT>
<BR><FONT SIZE=2>-Me</FONT>
</P>

</BODY>
</HTML>

--Boundary_(ID_h9VI79z7PdZK7gRSxuf97w)--