[Tutor] Pythonese/Efficiency/Generalese critique please

Lee Cullens leec03273 at mac.com
Sun Jun 5 18:50:01 CEST 2005


OK Kent - got it:

My little formating function could be written as  (tested)

     def cellpos(pname, alvl, blvl, clvl):

         # breakout path components into list
          pl = pname.split('/')

         # insert empty cells for repeated names and
         #  add new path components (cells) to csvline
         csvline = '"",'*(alvl - blvl) + '"' + '","'.join(pl 
[alvl:clvl]) + '",'

         return csvline


"So, it is good to know about join(), but write the code the way that  
is clearest to you."

One of my thoughts in posting this exercise is to first understand  
alternatives before I decide what I like best :~)
Your points, together with Javier's points on presentation and  
thoroughness, and Liam's point on making the logic more obvious are  
appreciated.

Thank you all for taking the time to offer your comments,
Lee C



On Jun 5, 2005, at 5:47 AM, Kent Johnson wrote:

> Lee Cullens wrote:
>
> <snip>
> OK, let's try to get this right.
>
> Given a list of strings and a divider string, join() returns the  
> strings from the list 'joined' by the divider string. For example,
>
>>>> pl = ['a', 'b', 'c']
>>>> '","'.join(pl)
>>>>
> 'a","b","c'
>
> The syntax for this takes a little getting used to; join() is  
> actually a string method; you call it on the divider string and  
> pass it the list of strings to be joined.
>
> To make a list of quoted, comma separated values, you also need  
> initial and final quotes:
>
>>>> '"' + '","'.join(pl) + '"'
>>>>
> '"a","b","c"'
>
> In your example you need '","'.join(pl[0:2])
>
>
>> Saving one line of code in this module doesn't mean much,  but if it
>> were a efficiency in processing it would?
>>
>
> Conventional wisdom is that it is more efficient to use the join  
> method. More enlightened wisdom says, don't optimize until you know  
> you have a problem, and the only way to know what is fastest in  
> your program with your data is to test. And of course working code  
> always beats broken code :-)
>
> I did some experiments with these alternatives and concluded that  
> if the length of the resulting string is less than about 500-800,  
> the version using += is faster than the one using join():
> http://www.pycs.net/users/0000323/weblog/2004/08/27.html
>
> So, it is good to know about join(), but write the code the way  
> that is clearest to you.
>
> Kent

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20050605/516200fd/attachment.html


More information about the Tutor mailing list