The "loop and a half"

Steve D'Aprano steve+python at pearwood.info
Sat Oct 7 09:19:47 EDT 2017


On Sat, 7 Oct 2017 11:06 pm, bartc wrote:

> That's not entering the data interactively (such as typing 'sort' then
> it sits silently recording lines of text (you hope) until it sees EOF).

I manually hit paste, that's just as much a form of data entry as typing
characters one at a time. And then I hit Ctrl-D, and the program responds by
sorting the input. That's a manual, interactive process.

My example only specified copying data out of another application because
that's my usual use-case. Perhaps I'm writing an email, or using a news
reader (as I'm doing now).

But that part isn't critical. Perhaps I haven't typed them yet. Since there's
only a few of them, a dozen or so, its trivial to type or paste them into
stdin. Because I'm not a caveman, I use all the tools at my disposal, and
that includes the ability to copy and paste from one application to another.

Maybe I even type some lines, and paste others. Its 2017 and I've been using
computers long enough to know how to copy and paste from one application to
another like a Boss. You ought to see me, I drag the mouse over text like a
virtuoso, and middle-click like a world-champion.

The fact that stdin is a pretty feature-poor line based editor is neither in
dispute nor is it a problem. If I make a mistake, its no big deal. I just
re-type the line, sort the lot including the erroneous line, then edit out
the bad line after I've pasted it into the email.

YOU, not me, is the one talking in absolutes: according to you, nobody could
possibly need anything less than a full-featured user-friendly application
with prompts and all the bells and whistles in order to sort even half a
dozen lines of text. You make absolute statements about how we ought to work:

    "You use a text editor to create your data. Then use existing 
    file-based sort."

No we don't. Not always. Sometimes we might, but its not compulsory and
sometimes the task is small enough that having sort read from stdin is all we
need. There's no need for saving text into a file.

I've given you a real-world actual use-case where your solution is
inconvenient, complicated and difficult. This is a case where I don't need
all the bells and whistles you claim are indispensable.

I'm not saying there's "never" a use for such an application. Of course there
are cases where entering data into stdin is inconvenient and underpowered.
Nobody disputes that. YOU are arguing against some imaginary, exaggerated
position where we say that nobody ever, under any circumstances, needs a UI
more friendly than bare-bones stdin.

Nobody said that. You are projecting your own absolutism onto us. We're
explaining why the sort program is designed the way it is. That doesn't mean
that other designs don't have their uses too.


> You say the lines already exist in a text editor. 

I said "whatever application I'm using", which is not necessarily a text
editor. Perhaps it is my email client. Perhaps it is a graphics program, and
I want to enter some sorted text. Perhaps its a web form.

Or perhaps I haven't actually typed the lines yet.


> Exactly what I said; 
> you start with text that has already been entered or generated by other
> means.

Unless I don't.


> However it does seem to expose a flaw in the ability of command line
> tools to work with non-command line tools.
> 
> So I have to copy 33,000 lines from a document, 

Don't be daft. Nobody says that stdin is a sufficient interface for a
heavy-weight task like that. With 33000 lines of text, I absolutely would
save them to a permanent file on disk, because I wouldn't want to risk having
the application crash or the power go off just as I've typed line 32999 and
lose the lot.

For 33000 lines, having one extra temporary file floating around is a cost
worth paying. For 33 lines, it is not.

You want a one-size fits all solution. Are you capable of understanding that
different tasks and different scenarios are often best solved using different
techniques?


[...]
> Put that way, it doesn't sound very sophisticated does it?

Nobody said it was sophisticated. That's the whole point: having sort read
from stdin as you type into the terminal is the LEAST sophisticated,
simplest, most bare-bones, basic technique that works.

If I want to prise open the lid of a paint can, a screwdriver will do the job.
I don't need to purchase a $50 gold-plated electric paint lid opening machine
that requires ten minutes to set up. Now maybe if I needed to open 33000
paint tins, it would be worth using a more heavyweight solution. But I only
need to open two tins. A screwdriver is fine.


> (Now I expect you're going to bombard with workarounds to fix all those
> issues.)

Not at all. 



[...]
> <snip long sequence designed to make Bart look an idiot>

You've got data in an email client. You've already typed it out, and then you
think "I want this sorted". What do you do?

You have to copy the lines into a text editor, save to disk, then launch your
file-based sort program, tell it to sort the saved file, reload the saved
file, copy the lines back again, then delete the now-unneeded file.

If you can see some steps which are unnecessary, then do tell.


> That's not what I meant at all, which is about creating the data from
> scratch. Here, the data STILL already magically exists somewhere!
> 
> Is it possible that you guys have never had to create anything original?
> 
> (How would /I/ sort a block of text in my editor? There's no built-in
> function, so using your approach:
> 
>   * Select the block
> 
>   * Ctrl KWfileEnter to write it to 'file'

Oops, you've just overwritten some data in 'file' that you needed. Damn.

That's three steps:

Ctrl-K to enter "operate on selected text" mode, or equivalent;
W to Save
enter a file name


>   * Elsewhere, sort <file >file2
> 
>   * Back in editor, Ctrl KY Ctrl KRfile2Enter to delete then insert file2

Ctrl-K to enter "operate on selected text" mode;
Y to Delete
Ctrl-K to enter "operate on selected text" mode;
R to Read from a file (at last an actual mnemonic command!)
enter a file name

That's five steps.

And now you have two "temporary" files, file and file2, which need to be
deleted. Two more steps that you conveniently don't count.

And you think that memorising these non-standard keyboard shortcuts Ctrl-KW
Ctrl-KY Ctrl-KR is better than memorising Ctrl-D which works across thousands
of applications? Good for you. You probably would love Emacs, except other
people use it, and therefore you will hate it.


[...]
> However, how hard would it for the editor to do its own sorting?

Yes, it is a mystery to me why so few editors include a "sort lines" function.


-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.




More information about the Python-list mailing list