🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.tcl
Article #56

Re: Python

#56
From: saito
Date: Thu, 15 Oct 2026 12:52
70 lines
2859 bytes
On 10/15/2026 4:37 AM, Christian Gollwitzer wrote:
> 
> YOu could write a Python script that reads from stdin and executes the 
> commands. I'm using a scheme like this in a larger Python application 
> that is remotely controlled from a Tcl GUI.
> 

I would prefer the Tcl way but I can't make it work. So I ended up doing 
something similar yesterday. Through a few iterations of the idea, this 
is what it looks like now:

===== save to driver.py ====================

import sys

errCounter = 0

while True:
     try:
         line = input()
     except EOFError:
         break

     try:
         try:
             # simple expression?
             result = eval(line)
             if result is not None:
                 print(result)

         except SyntaxError:
             # code block?
             exec(line)

     except Exception as e:
         errCounter=errCounter+1
         print(f"ERROR #{errCounter}: ", e)

============================================


This is how to use it:

============================================
set fid [open |[list python.exe -u driver.py 2>@1] r+]

fconfigure $fid -buffering line -blocking 0

puts $dif {X = 10}
gets $fid
puts $fid {X}
gets $fid

============================================


It works quite well. Things to note:

1) Currently both input and output is line oriented.

2) If the result from python has multiple lines, you need to go multiple 
gets to consume all.

Both of these could be improved upon but it seems to work quite well at 
the moment for the task it was meant for.




Technical Details
Message-ID: <11aqib8$2dede$1@dont-email.me>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail
References: <11anurd$1gcvi$2@dont-email.me> <11aq3bq$27p1e$1@dont-email.me>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail
From: saito <saitology9@gmail.com>
Newsgroups: comp.lang.tcl
Subject: Re: Python
Date: Thu, 15 Oct 2026 08:52:56 -0400
Organization: A noiseless patient Spider
Lines: 70
Message-ID: <11aqib8$2dede$1@dont-email.me>
References: <11anurd$1gcvi$2@dont-email.me> <11aq3bq$27p1e$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 15 Oct 2026 12:52:56 +0000 (UTC)
Injection-Info: dont-email.me;
	logging-data="2537902"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX1+qZtCeoiqor4BXPZUP9b8f"; posting-host="841ceba43f7665a4b30dbe55a22ae79b"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:MSjYQgGzO49yWbD1ZzhlusIUeCI= sha256:U/hLnQqccTQxy29fG+pAUjGFB9ezo6xsCF9DjzFOKdI=
	sha1:zdtpYobEcVAWJ4WRis5LV/E7HHg= sha256:t+RvvbxRbrpVdt6UI13jcI+erD9mw73bAzeA4+3mwTg=
In-Reply-To: <11aq3bq$27p1e$1@dont-email.me>
Content-Language: en-US