[issue34210] Small improvements in heapq (refatoring)

2018-07-24 Thread Alexander Marshalov


Change by Alexander Marshalov <_...@marshalov.org>:


--
keywords: +patch
pull_requests: +7965
stage:  -> patch review

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue34210] Small improvements in heapq (refatoring)

2018-07-24 Thread Alexander Marshalov

New submission from Alexander Marshalov <_...@marshalov.org>:

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.

--
messages: 322307
nosy: amper
priority: normal
severity: normal
status: open
title: Small improvements in heapq (refatoring)
type: enhancement
versions: Python 3.8

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com