Exploratory query

Alex Martelli aleax at aleax.it
Tue Aug 5 07:45:47 EDT 2003


Don Todd wrote:

> I'm a total python newbie and have not really even begun to learn.
> Before I start, however, I would like to know if python is the proper
> tool for what I want to do.
> 
> I want to query various MH mailboxes to see if they contain new mail.
> There is a program, flist, that will do this, but it is a pain to run it
> every time. I'd like something a la xbiff or gbiffy.
> 
> Would python allow me to run flist and use its output, or would I need
> to re-write flist? The idea is to keep something on the screen and poll
> the mailboxes every n seconds and update the display.

Yes, it should be pretty easy to write a Python script (with e.g. a
Tkinter GUI) to do just this -- run another arbitrary program X every
Y seconds and keep a window display with the latest stdout of said
program X.  (Of course, you can then make a more refined version that
knows the format of stdout for given programs and parses it in order
to provide prettier or more useful output, and add more features).

Such a script could be something like:

import Tkinter as Tk
import optparse, sys, os

# acquire arguments
parser = optparse.OptionParser()
parser.add_option('-c', '--command', dest='command',
    help='Command to be executed periodically',
    metavar='PIPELINE', default='date')
parser.add_option('-p', '--period', dest='period',
    help='How many seconds to wait between executions',
    metavar='SECONDS', default=5)
options, args = parser.parse_args()
if args:
    print >> sys.stderr, "Unknown args %r ignored" % args

# run command, update output if needed
last_output = ''
def run_command():
    global last_output
    new_output = os.popen(options.command).read()
    if new_output != last_output:
        last_output = new_output
        text.config(state=Tk.NORMAL)
        text.delete(1.0, Tk.END)
        text.insert(Tk.END, new_output)
        text.config(state=Tk.DISABLED)
    text.after(options.period, run_command)
    
# prepare and display a GUI
text = Tk.Text()
text.config(state=Tk.DISABLED)
text.pack()
run_command()
Tk.mainloop()


Running this with -cflist should already give you a first rough
prototype of what you might want.  Many improvements are of course
possible, but I hope this shows it's worth studying some Python
(and Tkinter, or other GUI toolkit if you prefer) in order to be
able to implement such improvements.


Alex





More information about the Python-list mailing list