On 05/10/2017 23:42, Gregory Ewing wrote:
bartc wrote:
It can be done with an in-band end-of-data code,

Then you need some way of escaping that in-band code, in case
it happens to equal some of the data you want to sort.

It needn't be a big deal. You can do this (you have to in Python as the normal 255 character signifying eof on a character stream is trapped):

 EOD=chr(26)      # can be anything that is an unlikely input line.

 def myinput(prompt):
     try:
         return input(prompt)
     except EOFError:
         return EOD

Then you can keep using a simple type of loop without worrying about EOF status:

 print ("Type Ctrl-Z on its own line to finish.") # Windows

 while 1:

     buffer=myinput("Enter something: ")
     if buffer==EOD: break
     print ("You typed:",buffer)     # process(buffer)

 print ("Bye")

Yes, I tried typing 'sort' in Linux, where it apparently hangs (same on Windows actually). The reason: because it might have killed someone to have added a message saying what you are expected to type and how to end it. (Namely, press Ctrl-D start at the start of a line in Linux, and Ctrl-Z followed by Enter, I think also at the start, in Windows.)

How to signal EOF from the keyboard is a fundamental piece
of knowledge about the OS. It's not the responsibility of
individual programs to teach it to you, any more than it's
your car's responsibility to explain what the steering wheel
does each time you start the engine.

If you have a utility that is designed to also be used interactively, but it shows absolutely nothing when you start it, like this:

 >sort





then it stinks. You wouldn't think much of a shell prompt that literally showed nothing at all instead of something like:

 c:\python39>

How would you even know what program you were in the middle of after the initial command has scrolled off the screen? How would you now it was still running? As typing a line into it give no response.

I don't run these things often enough to remember exactly how to specify an EOF with the keyboard. It might be:

- One of Ctrl C, D, Z or Break

- It may or not may not only work at the start of a line

- It may or not require to be followed by Enter

I anyway would never inflict such an interface on my users, who would also associate Ctrl C etc with aborting a program that's gone wrong.

--
bartc
--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to