[Tutor] thanks!

Karl Pflästerer sigurd at 12move.de
Sat Feb 14 22:21:56 EST 2004


On 15 Feb 2004, Christopher Spears <- cspears2002 at yahoo.com wrote:

> I solved the problem!  As several have pointed out, I

Congrats.

> was providing file names not pathways.  Here is the
> resulting code:


[Code]

> Notice I do not use a lot of functional programming. 

You use map() and filter().  It's a good start :-)

> I have nothing against functional programming.  I just
> do not undertand much of it, which is why I took this

I had the (maybe wrong) impression that you wanted to use functional
style.

Some annotations to your solution if you like:

>     files = map(os.path.join,[directory] *
> len(files),files)

If you just want to join the file with its path there's no need to
create a list of directories.

map(lambda f: os.path.join(d, f), files)

(with d == directory) will suffice.  Or write it with a list
comprehension.  To create a list to throw it in the next moment away is
too expensive; even more the function len() should only get used when
you absolutely need it; it has to traverse the whole list to find its
length.

>     sizes.sort(); sizes.reverse();
>     for s in sizes:
>         biggest_files.append([files_of_size[s],s])
> #Sorts keys from largest to smallest and places the
> names in a
> #new list in that order.

You use here a list to hold exactly two values which won't get changed:
[files_of_size[s],s]; a tuple is here the better choice.  Lists should
get used if you don't know in advance how much values will be in the
sequence and if you want to change the values afterwards; if not use a
tuple which gives you faster code.

General: most people write the comments *above* the according code;
furthermore there are good articles about how to write comments and what
to write in a comment and what not.  E.g. there's no need to write the
code a second time the way you did it; that doesn't help the reader (we
assume he knows Python); better explain why something is written the way
it's written; e.g. most people would use a tuple as container but if you
chose a list it might be worth to explain why.



   Karl
-- 
Please do *not* send copies of replies to me.
I read the list




More information about the Tutor mailing list