Flexable Collating (feedback please)

Ron Adam rrr at ronadam.com
Wed Oct 18 10:35:58 EDT 2006


georgeryoung at gmail.com wrote:
> 
> On Oct 18, 2:42 am, Ron Adam <r... at ronadam.com> wrote:
>> I put together the following module today and would like some feedback on any
>> obvious problems.  Or even opinions of weather or not it is a good approach.
> ,,,
>      def __call__(self, a, b):
>          """ This allows the Collate class work as a sort key.
> 
>                  USE: list.sort(key=Collate(flags))
>          """
>          return cmp(self.transform(a), self.transform(b))
> 
> You document _call__ as useful for the "key" keyword to sort, but you
> implement it for the "cmp" keyword.  The "key" allows much better
> performance, since it's called only once per value.  Maybe just :
> return self.transform(a)
> 
> -- George
> 


Thanks,  I changed it to the following...



     def __call__(self, a):
         """ This allows the Collate class work as a sort key.

                 USE: list.sort(key=Collate(flags))
         """
         return self.transform(a)



And also changed the sort call here ...


def collate(slist, flags=0):
     """ Collate list of strings in place.
     """
     slist.sort(key=Collate(flags))               <<<


Today I'll do some performance tests to see how much faster it is for moderate 
sized lists.


Cheers,
    Ron







More information about the Python-list mailing list