tim's idea makes my code crawl: (was: Compiling Python 1.6 under MacOS X ...)
Tim Peters
tim_one at email.msn.com
Tue Sep 26 01:04:08 EDT 2000
[Tim, w/ <shudder!> code]
[Grant Griffin]
> I thought that was a pretty neat idea.
I forgot to mention: I released it to the public domain <0.9 wink>.
> However, when I tried it out (with some modifications for my
> application), I found that it didn't work right in the case where
> the directory I gave it was ".". Is that just a cockpit problem on
> my part, or is that actually a "feature" of the above?
>
> If the latter, how should we fix it? I thought about trapping "." out
> as a special case, then substituting the full path, but is there
> something better?
It does assume the argument given to it is a full path from the root. So
change this line:
dirs = [root]
to
dirs = [os.path.abspath(root)]
and fiddle the imports however you like to make that pretty. In my 2.0
code, I use
import os.path.abspath as grant
...
dirs = [grant(root)]
Whatever, it's a one-time cost at function startup.
For a *really* funky time, replace
while dirs:
dir = dirs.pop()
with
for dir in dirs:
and you get a dirt-simple breadth-first search. This relies on (a) that
"dirs" grows *inside* the loop; and (b) that Python's iteration protocol
doesn't "know" the loop length in advance. The "for" keeps marching over
the ever-growing "dirs" until dirs stops growing and the hidden iteration
index oversteps its end. Sometimes-nice bonus: at the end, dirs contains a
list of all the directories you've visited! If someone in your life is good
with needlepoint, that can make a great nerd conversation piece.
> beautiful-would-be-better-than-ug-ly y'rs,
and-sick-can-be-better-than-healthy-ly y'rs - tim
More information about the Python-list
mailing list