sorting pathnames by basename
Erik Price
erikprice at mac.com
Tue Aug 20 11:57:55 EDT 2002
I respond to Michael O'Donnell, Kevin Clark, Ben Scott, and
pll at lanminds.com in this response:
Michael O'Donnell wrote:
> Wow. That doesn't suck. Thanks!
Well, I wrote it in a text editor called BBEdit on Mac OS X. BBEdit's
advert blurb is, literally: "It doesn't suck" (http://barebones.com/).
Which I agree with wholeheartedly, it is my favorite programming
environment.
Kevin Clark wrote:
> One-liners are not representitive of the readability of Perl code. I
> hope you agree.
Definitely. Perhaps those who are new to Perl don't quite see it yet
(although I admit that I am still new to Perl), but I find that the
one-liners are very elegant and spare me the bother of dropping into an
editor for something so simple. Like writing some bash code right there
on the command line.
For that matter, I have to defer to Kevin's one-liner as it was
definitely more elegant than the Python code. I only posted it because
he asked for it!
Kevin Clark also wrote:
> Can any of the Python programmers on this list implement this as a
> one-liner? Just wondering.
>
> (with Python's indentation rules, I think that this would be difficult)
It's antithetical to the Python Way. JAPHs like elegant and terse
one-liners, but Pythoneers generally don't or can't use them. Larry
Wall wrote Perl so that there is more than one way to do everything,
including coding style (use strict or english, etc). Python's
philosophy is "there should generally be one obvious way to do
something" even if that isn't the most efficient-looking code.
Kevin Clark finally added:
> OBTW, while I appreciate Erik's effort (because, in fact, I am
> learning Python), in actuality, I believe that his program contains a
> bug
Good point! That Python script was a "five-minuter". If I had written
it properly, it should have taken this into account, and also been
written as a module so that it could itself be reimplemented in other
code later ("import basenamesort.py"). Also, instead of being limited
to just a filter for standard input, it would also have had the ability
to be executed directly as a standalone program, perhaps with an
argument passed to it ("filenames.txt" or something). That would have
made it a "proper" modular Python script, but I fell asleep during
"Clash of the Titans" at a friends' house a short while before , and I
just wanted to check/respond to email before I went to bed. Of course a
Python script could be written to accommodate these extra functions.
Ben Scott wrote:
> Without even knowing anything about Python,
> I suspect that code could be made denser, by eliminating temporary
> variables
> and nesting things instead.
Actually, I tried to write it more densely but couldn't. I'm not a
Python guru by any stretch, but I had thought that I could use "for key
in sorted.sort()" instead of having the sort() method call on a separate
line. Apparently I can't. I've been spending a LOT more time learning
Perl than Python, lately (b/c I'm unemployed and there are dozens more
Perl jobs than Python jobs out there right now).
Ben Scott also wrote:
> This is an accident. In general, Perl was not designed -- it evolved.
> It started small, and then creeping featurism ran rampant on it. Much
> like
> Unix, there are a great many things in Perl that don't make a lot of
> sense
> unless you know the history behind them.
That's right! Don't forget that Perl 6 is (supposed to be) completely
redesigned from scratch for this very reason.
pll at lanminds.com wrote:
> Is Python also a "text-munging" type language aimed at sysadmin type
> problems? Was it too, specifically designed to pick up where awk
> and sed fell short?
Guido van Rossum originally wrote the language specifically for
educational purposes, because of all of the shortcomings of BASIC. He
wanted a fully object-oriented language that be difficult for
programmers to develop bad habits from (note the lack of a GOTO, and the
strict enforcement of whitespace and indenting). This is why Python has
a nice interactive interpreter -- for real-time experimentation. Of
course, programmers like the interactive interpreter because they can
whip up a function or class definition and then play with it to see how
it interacts with their code without having to execute an entire script,
getting immediate feedback on their work.
While I have seen some system-administration stuff written in Python,
there isn't much. For one, Perl is far more widely-used and installed
on more systems. Also, if I had to do extensive text munging, there is
no question that I would use Perl to do it over Python unless I had some
compelling reason not to. (This is just my opinion, but) Perl's
text-munging abilities are more convenient to use than Python's. It's
like, they're built into the syntax and mentality of Perl, whereas with
Python you will probably end up importing a module like the string
module and then executing various methods of the String object (or
aggregating the String object into a custom-made, more powerful object
and calling its methods), just like you would with any other task in
Python.
In conclusion, I don't want to oversimplify things, but I'm going to
anyway:
I like Perl best for scripts that are a few hundred lines or less.
I like Python for larger, more modular and extensible programs and
software.
If someone had asked me to come up with a script to sort some files by
their basename, I would reach for Perl in a heartbeat (and still
wouldn't have had a script as elegant as Kevin's).
But at the point where I say to myself, "I really think that I could
write this better and more easily if I used an object-oriented
methodology and designed some class definitions to help me", I would
turn to Python and not look back.
Erik
--
Erik Price
email: erikprice at mac.com
jabber: erikprice at jabber.org
More information about the gnhlug-discuss
mailing list