How to do...?

Quinn Dunkan quinn at mark.ugcs.caltech.edu
Fri Nov 3 15:15:42 EST 2000


On Fri, 3 Nov 2000 19:22:30 +0100, Guy Van Rentergem
<guy.van.rentergem at skynet.be> wrote:
>Hello people,
>
>How can I put characters in the keyboard buffer without typing anything?

>From within python you colud do something like:

class PushFile:
    def __init__(self, fp):
        self.fp = fp
    def readline(self):
        if self.pushback:
            return self.pushback.pop()
        else:
            return self.fp.readline()
    def push(self, line):
        self.pushback.append(line)

and replace the file you're reading from with PushFile.  This can be handy for
automatically testing interactive programs.

If you want to control an external program's keyboard input, see the expect
module.



More information about the Python-list mailing list