sys.stdin.read() replacement

Ben Last ben at benlast.com
Mon Aug 16 08:40:53 EDT 2004


Re-reading the original question, I think that Benjamin's right (as all
people called Ben tend to be, except me); the original poster was after a
method of editing previous lines, and readline won't do that; it does single
lines only.  A curses-based console app would allow the editing of previous
lines, but that's a whole order-of-magnitude shift in complexity (and I
don't know of any curses modules for Windows).

As a workaround - if the user is required to type in each line preceded by a
number, then it'd be possible to just take the last version of a line.   But
that may not be possible.

So; Unix solution - create a temporary filename (with os.tempnam() for
example), spawn off an editor to edit it, and parse the resulting edited
file when the edit session completes, like the Unix vipw command does.

Windows equivalent - invoke notepad.exe as the editor.

ben

> -----Original Message-----
> From: python-list-bounces+ben=benlast.com at python.org
> [mailto:python-list-bounces+ben=benlast.com at python.org]On Behalf Of
> Benjamin Niemann
> Sent: 16 August 2004 13:10
> To: python-list at python.org
> Subject: Re: sys.stdin.read() replacement
>
>
> not sure, if importing readline really gets the desired result (if I
> understand the initial question correctly)
>
> suppose the following input:
>    > this is ym first line
>    > and another line
>    (oops there's a typo in the first line, user hits cursor up two
> times, and corrects it)
>    > this is my first line
>    ...
>
> what the program will then reads is:
>    "this is ym first line"
>    "and another line"
>    "this is my first line"
>    ...
> with no chance to see that the third line is supposed to replace line one.
> Or is readline actually more powerful than I remember?
>
>
> > import readline will work on Windows:
> > a) if you're running the cygwin python, inside cygwin
> > b) if you've installed a readline module; there are at least
> two available.
> > Excuse me for the self-referential answer, but about half and
> hour before I
> > read this, I wrote a blog entry all about readline modules,
> with links to
> > and comments about the Windows ones.  The permalink is
> > http://www.livejournal.com/users/benlast/16766.html or it's the
> most recent
> > entry at http://www.livejournal.com/users/benlast/
> >
> > Karin: if you do import a readline, you can then use
> sys.stdin.readline() to
> > read a line of input, and you should get some sort of history
> functionality
> > (up-arrow, for example, will recall a previous line).
> >
> > regards
> > b
> >
> >
> >>-----Original Message-----
> >>From: python-list-bounces+ben=benlast.com at python.org
> >>[mailto:python-list-bounces+ben=benlast.com at python.org]On Behalf Of
> >>P at draigBrady.com
> >>Sent: 16 August 2004 11:52
> >>To: python-list at python.org
> >>Subject: Re: sys.stdin.read() replacement
> >>
> >>
> >>Karin Lagesen wrote:
> >>
> >>>I am writing a small write-logfile script for use on the
> >>
> >>terminal. I have here
> >>
> >>>a section where the user should be able to type in several
> >>
> >>lines. I have solved
> >>
> >>>it so far by using sys.stdin.read(), which makes it possible to
> >>
> >>type in several
> >>
> >>>lines (separated by enter) and then terminate the session by
> >>
> >>typing ^D. This is
> >>
> >>>all very good. However, with this it is only possible to edit
> >>
> >>the line you are
> >>
> >>>in. If you spot an error further up in your text, you cannot
> >>
> >>get at it. Do any
> >>
> >>>of you know of a way I can do this?
> >>
> >>
> >>import readline
> >>
> >>(I don't know if it works on windos)
> >>
> >>Pádraig.
> >>--
> >>http://mail.python.org/mailman/listinfo/python-list
> >
> >
> --
> http://mail.python.org/mailman/listinfo/python-list




More information about the Python-list mailing list