Is this a dream or a nightmare? (Was Re: XML)

Kragen Sitaker kragen at dnaco.net
Mon Oct 9 00:04:40 EDT 2000


In article <39dfa617.25953118 at news.davesworld.net>,
David T. Grove <pete at petes-place.com> wrote:
>Also, I must admit that "good programming practices" have hit me
>pretty hard recently, when I've had to babysit some newbies from
>another planet... er... country. My elses got unsnuggled and
>
>die unless @d = map {"$basedir/$_"} grep {!-d && -w} readdir(dir);
>
>...which is perfectly logical and readable to me as a single
>"thought"... "get me all the filenames in that directory or halt
>immediately because you'll screw something up". Single thoughts don't
>translate into code very well without one-liners like this, because
>you're so busy with sub thoughts that you forget what you're
>programming until your current thought is done.

Well, this translates fairly straightforwardly into Python, and it's
less than 25 lines:
    d = map(lambda x, b=basedir: b + "/" + x, 
            filter(lambda d: not os.path.isdir(d) and os.access(d, os.W_OK),
                   os.listdir(dir)))
    if d == []: raise "Perl Programmer's Exception"

Closer to 4.  It is a bit more verbose, and arguably more readable.

It also has the same bug: if dir isn't '.', it will work remarkably
poorly.  This suggests that your assertion of "logical and readable to
me" may be overstated.  :)
-- 
<kragen at pobox.com>       Kragen Sitaker     <http://www.pobox.com/~kragen/>
Perilous to all of us are the devices of an art deeper than we ourselves
possess.
                -- Gandalf the Grey [J.R.R. Tolkien, "Lord of the Rings"]





More information about the Python-list mailing list