This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author haldean
Recipients haldean
Date 2012-10-01.11:39:25
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1349091566.51.0.299551598321.issue16098@psf.upfronthosting.co.za>
In-reply-to
Content
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!
History
Date User Action Args
2012-10-01 11:39:26haldeansetrecipients: + haldean
2012-10-01 11:39:26haldeansetmessageid: <1349091566.51.0.299551598321.issue16098@psf.upfronthosting.co.za>
2012-10-01 11:39:26haldeanlinkissue16098 messages
2012-10-01 11:39:26haldeancreate