Author amper
Recipients amper
Date 2018-07-24.15:06:20
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1532444780.82.0.56676864532.issue34210@psf.upfronthosting.co.za>
In-reply-to
Content
I would like to make three small improvements to the "heapq" module.

1) The "nsmallest" function has the following code (a similar code exists in the "nlargest" function):

    # When n>=size, it's faster to use sorted()
    try:
        size = len(iterable)
    except (TypeError, AttributeError):
        pass
    else:
        if n >= size:
            return sorted(iterable, key=key)[:n]

   I think "[:n]" is redundant, because "iterable" contains no more than n elements.
   Therefore, that code can be rewritten as follows:
   
    # When n>=size, it's faster to use sorted()
    try:
        size = len(iterable)
    except (TypeError, AttributeError):
        pass
    else:
        if n >= size:
            return sorted(iterable, key=key)

2) It seems to me that the line:

    for i in reversed(range(n//2)):

   will be more optimal in this version:

    for i in range(n//2 + 1, -1, -1):


3) Top-level functions can be surrounded with two blank lines for greater compliance with PEP-8.
History
Date User Action Args
2018-07-24 15:06:20ampersetrecipients: + amper
2018-07-24 15:06:20ampersetmessageid: <1532444780.82.0.56676864532.issue34210@psf.upfronthosting.co.za>
2018-07-24 15:06:20amperlinkissue34210 messages
2018-07-24 15:06:20ampercreate