[Tutor] is there a better way to organise this code

Wayne Werner waynejwerner at gmail.com
Wed Nov 30 23:48:31 CET 2011


On Wed, Nov 30, 2011 at 3:34 PM, Norman Khine <norman at khine.net> wrote:

> hello,
>
> is there a better way to organise this code or optimise it.
>
> http://pastie.org/2944797
>
>
After glancing at it the only change that I would recommend (possibly)
making is lines 58 and 39 - you can wrap the dictionaries (line line 7) and
that will bring all your lines < 80 characters. Other than that it looks
perfectly readable. You could add some docstrings for completeness.

As far as anything else - is this code too slow? If not, why bother trying
to optimise it? Obviously if you were doing something horribly inefficient
like a bubble sort (especially considering Python has a built-in sort),
then you would want to get rid of that, but I didn't really notice
anything. Though now that I took a few more seconds, it does look like
you're not using rows, aside from looping over it. If you only care about
the values and not the collection as a whole you could (and should) change
the list comprehension to a generator expression:

>>> [x for x in (1,2,3)]
[1, 2, 3]
>>> (x for x in (1,2,3))
<generator object <genexpr> at 0x01BFB6C0>

If you're not familiar with a generators, I highly recommend this set of
slides: http://www.dabeaz.com/generators/Generators.pdf

HTH,
Wayne
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20111130/c05dc21c/attachment-0001.html>


More information about the Tutor mailing list