Expect like syntax in Python

Guy Gascoigne - Piggford guy at wyrdrune.com
Sun Feb 3 23:49:32 EST 2002


What I really want is some way to get anonymous functions that can be passed
as part of a list of paramaters and called at the appropriate time.  There
doesn't appear to be any way to get that particular effect in Python.

As for expect like functions in java - luckily I don't need all of the
features of expect, more a case of simple telnet scripting. Between our own
telnet code and pattern matching we've actually got something that works
pretty well so far.

BTW I copied my original question to the end of the message in case it rang
any bells.

Guy

"Donn Cave" <donn at drizzle.com> wrote in message
news:1012765918.861234 at yasure...
> Quoth "Guy Gascoigne - Piggford" <guy at wyrdrune.com>:
> Which I missed, but when I had thoughts like that once upon a time,
> the best I could think of was to use the exception mechanism.  Or
> abuse it, if you like.  Make pattern objects, feed them to the expect
> parser in a try block, which would raise it as an exception to dispatch
> to the appropriate except block.  Horrible.
>
> But you must have support for pseudotty devices, which is a low level
> functionality that is not so portable.  To my knowledge available only
> on UNIX and similar platforms, and not on all such for Python.  Have
> that in Java?  I don't know, but if not, you're sunk.
>
> Donn Cave, donn at drizzle.com

> Quoth "Guy Gascoigne - Piggford" <guy at wyrdrune.com>:
>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" {
>           pass
>           }
>    -re "ogin" {
>           send  user
>           }
>    -re "sword" {
>           send password
>           }
>}
>
>etc.
>
>The nearest that I've managed to get to in Python is something like
>this:
>
>        while 1:
>            try:
>                if self.expect( ( "ast login", 0 ),
>                                ( "ogin", self.send_user ),
>                                ( "sword", self.send_password ),
>                                ( "\$", "done" ) ) == "done":
>                    break
>            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?
>






More information about the Python-list mailing list