Expect like syntax in Python

Guy Gascoigne - Piggford guy at wyrdrune.com
Sun Feb 3 09:25:14 CET 2002

I'm trying to find a way to write something like expect in a python module,
and I'm having trouble getting the syntax the way that I want.  In expect I
can write something like this:

expect {
    -re "ast login" {
    -re "ogin" {
           send  user
    -re "sword" {
           send password


The nearest that I've managed to get to in Python is something like this:

        while 1:
                if self.expect( ( "ast login", 0 ),
                                ( "ogin", self.send_user ),
                                ( "sword", self.send_password ),
                                ( "\$", "done" ) ) == "done":
            except IndexError:
                skipcount += 1
                if skipcount == 2:
                    raise IndexError

Where send_user and send_password are separately defined functions.  What I
really want to be able to do is to define the action beside the pattern,
calling short functions that are defined separately makes it harder to read
and trickier to follow exactly what is happening.  I've also played around
with just returning an index, but that also gets ugly as the list of
possible patterns gets larger.

Am I missing something?  I'm fairly new to python but I don't see a way to
get what I want.  Does anyone have any suggestions on how to handle this
more cleanly?


Guy Gascoigne - Piggford - www.wyrdrune.com
"Sometimes verbal ingenuity is not enough" - B. Banzai

More information about the Python-list mailing list