Message171706
The implementation of nsmallest in heapq contains an optimization for when n is an order of magnitude less than the size of the data, which uses bisect to find the n-smallest elements. This optimization is guarded by a check to ensure that the data iterable has a length method.
This method is then decorated to add support for the key kwarg. The decorator creates a zip object and passes the zip object to the decorated nsmallest. As zip objects are generators, they do not have a __len__ attribute, and the bisect optimization is never used.
The attached patch file detects whether the data passed to the decorator has a length attribute, and if it does, it creates a list with the data before passing it to the decorated nsmallest. This is my first patch, so if I've done something wrong please let me know. Thanks! |
|
Date |
User |
Action |
Args |
2012-10-01 11:39:26 | haldean | set | recipients:
+ haldean |
2012-10-01 11:39:26 | haldean | set | messageid: <1349091566.51.0.299551598321.issue16098@psf.upfronthosting.co.za> |
2012-10-01 11:39:26 | haldean | link | issue16098 messages |
2012-10-01 11:39:26 | haldean | create | |
|