Comment 13 for bug 231106

On Sat, May 17, 2008 at 12:19 AM, Jan Wijbrand Kolman
<email address hidden> wrote:
> It is late and I'm tired so I might very well be missing the point,
> however... I think you both overlook the fact that the sort() method is
> expected to return a list of (name, viewlet) tuples which
> util.sort_components() won't.

Ah, true, you are right. How annoying.

> So, I have to jump through some hoops to get this list of (name,
> viewlet) tuples sorted just like util.sort_components() would sort the
> list of just viewlets.

I think you should consider writing it like this instead:

s_viewlets = viewlets
for name, viewlet in viewlets:
     # stuff away viewlet name so we can later retrieve it
     viewlet.__viewlet_name__ = name

s_viewlets = util.sort_components(s_viewlets)

return [(viewlet.__viewlet_name__, viewlet) for viewlet in s_viewlets]

still ugly of course, but viewlet instances will never be persistent,
and are created on the fly each time you view them, so there's little
risk. It avoids the necessity of the 'index' call, which Python
implements by looping through sorted_components (and this is in an
inner loop, so there'll be lots of those loops). I don't think
performance-wise it would matter very much as the list of viewlets is
usually probably going to be small, but it's good to avoid such
potential scalability snags anyway. The code might also become
slightly easier to read. I didn't read the code very well before and
had no clue what was really going on, actually, silly me. Anyway, it
definitely needs a few comments, no matter what we end up writing;
this code is non-trivial either.

Note that there's a risk with both pieces of code: the same viewlet
instance might be in the list of viewlets twice. This would confuse
this procedure, as the name would be overwritten. It would also
confuse the .index() procedure, however. Perhaps a comment about this
risk should be in the code too.