]> granicus.if.org Git - python/commitdiff
Issue #22186: Fix typos in Lib/.
authorBerker Peksag <berker.peksag@gmail.com>
Sun, 19 Oct 2014 15:07:05 +0000 (18:07 +0300)
committerBerker Peksag <berker.peksag@gmail.com>
Sun, 19 Oct 2014 15:07:05 +0000 (18:07 +0300)
Patch by FĂ©vry Thibault.

1  2 
Lib/heapq.py
Lib/inspect.py
Lib/ipaddress.py
Lib/pdb.py
Lib/socketserver.py
Lib/ssl.py
Lib/unittest/test/test_case.py
Tools/clinic/clinic.py

diff --cc Lib/heapq.py
index 258c0baeb62de0b63e3c357a5ad2f5da78ab084e,d615239b94608a89ffd570724953347d7e0cd675..07af37e717e47f3a619d16ea4711ac9f0ba13e3d
@@@ -331,133 -360,33 +331,133 @@@ def merge(*iterables, key=None, reverse
  
      h = []
      h_append = h.append
 -    for itnum, it in enumerate(map(iter, iterables)):
 +
 +    if reverse:
 +        _heapify = _heapify_max
 +        _heappop = _heappop_max
 +        _heapreplace = _heapreplace_max
 +        direction = -1
 +    else:
 +        _heapify = heapify
 +        _heappop = heappop
 +        _heapreplace = heapreplace
 +        direction = 1
 +
 +    if key is None:
 +        for order, it in enumerate(map(iter, iterables)):
 +            try:
 +                next = it.__next__
 +                h_append([next(), order * direction, next])
 +            except StopIteration:
 +                pass
 +        _heapify(h)
 +        while len(h) > 1:
 +            try:
 +                while True:
 +                    value, order, next = s = h[0]
 +                    yield value
 +                    s[0] = next()           # raises StopIteration when exhausted
 +                    _heapreplace(h, s)      # restore heap condition
 +            except StopIteration:
 +                _heappop(h)                 # remove empty iterator
 +        if h:
 +            # fast case when only a single iterator remains
 +            value, order, next = h[0]
 +            yield value
 +            yield from next.__self__
 +        return
 +
 +    for order, it in enumerate(map(iter, iterables)):
          try:
              next = it.__next__
 -            h_append([next(), itnum, next])
 -        except _StopIteration:
 +            value = next()
 +            h_append([key(value), order * direction, value, next])
 +        except StopIteration:
              pass
 -    heapify(h)
 -
 -    while _len(h) > 1:
 +    _heapify(h)
 +    while len(h) > 1:
          try:
              while True:
 -                v, itnum, next = s = h[0]
 -                yield v
 -                s[0] = next()               # raises StopIteration when exhausted
 -                _heapreplace(h, s)          # restore heap condition
 -        except _StopIteration:
 -            _heappop(h)                     # remove empty iterator
 +                key_value, order, value, next = s = h[0]
 +                yield value
 +                value = next()
 +                s[0] = key(value)
 +                s[2] = value
 +                _heapreplace(h, s)
 +        except StopIteration:
 +            _heappop(h)
      if h:
 -        # fast case when only a single iterator remains
 -        v, itnum, next = h[0]
 -        yield v
 +        key_value, order, value, next = h[0]
 +        yield value
          yield from next.__self__
  
 -# Extend the implementations of nsmallest and nlargest to use a key= argument
 -_nsmallest = nsmallest
 +
 +# Algorithm notes for nlargest() and nsmallest()
 +# ==============================================
 +#
 +# Make a single pass over the data while keeping the k most extreme values
 +# in a heap.  Memory consumption is limited to keeping k values in a list.
 +#
 +# Measured performance for random inputs:
 +#
 +#                                   number of comparisons
 +#    n inputs     k-extreme values  (average of 5 trials)   % more than min()
 +# -------------   ----------------  ---------------------   -----------------
 +#      1,000           100                  3,317               231.7%
 +#     10,000           100                 14,046                40.5%
 +#    100,000           100                105,749                 5.7%
 +#  1,000,000           100              1,007,751                 0.8%
 +# 10,000,000           100             10,009,401                 0.1%
 +#
 +# Theoretical number of comparisons for k smallest of n random inputs:
 +#
 +# Step   Comparisons                  Action
 +# ----   --------------------------   ---------------------------
 +#  1     1.66 * k                     heapify the first k-inputs
 +#  2     n - k                        compare remaining elements to top of heap
 +#  3     k * (1 + lg2(k)) * ln(n/k)   replace the topmost value on the heap
 +#  4     k * lg2(k) - (k/2)           final sort of the k most extreme values
 +#
 +# Combining and simplifying for a rough estimate gives:
 +#
 +#        comparisons = n + k * (log(k, 2) * log(n/k) + log(k, 2) + log(n/k))
 +#
 +# Computing the number of comparisons for step 3:
 +# -----------------------------------------------
 +# * For the i-th new value from the iterable, the probability of being in the
 +#   k most extreme values is k/i.  For example, the probability of the 101st
 +#   value seen being in the 100 most extreme values is 100/101.
 +# * If the value is a new extreme value, the cost of inserting it into the
 +#   heap is 1 + log(k, 2).
- # * The probabilty times the cost gives:
++# * The probability times the cost gives:
 +#            (k/i) * (1 + log(k, 2))
 +# * Summing across the remaining n-k elements gives:
 +#            sum((k/i) * (1 + log(k, 2)) for i in range(k+1, n+1))
 +# * This reduces to:
 +#            (H(n) - H(k)) * k * (1 + log(k, 2))
 +# * Where H(n) is the n-th harmonic number estimated by:
 +#            gamma = 0.5772156649
 +#            H(n) = log(n, e) + gamma + 1 / (2 * n)
 +#   http://en.wikipedia.org/wiki/Harmonic_series_(mathematics)#Rate_of_divergence
 +# * Substituting the H(n) formula:
 +#            comparisons = k * (1 + log(k, 2)) * (log(n/k, e) + (1/n - 1/k) / 2)
 +#
 +# Worst-case for step 3:
 +# ----------------------
 +# In the worst case, the input data is reversed sorted so that every new element
 +# must be inserted in the heap:
 +#
 +#             comparisons = 1.66 * k + log(k, 2) * (n - k)
 +#
 +# Alternative Algorithms
 +# ----------------------
 +# Other algorithms were not used because they:
 +# 1) Took much more auxiliary memory,
 +# 2) Made multiple passes over the data.
 +# 3) Made more comparisons in common cases (small k, large n, semi-random input).
 +# See the more detailed comparison of approach at:
 +# http://code.activestate.com/recipes/577573-compare-algorithms-for-heapqsmallest
 +
  def nsmallest(n, iterable, key=None):
      """Find the n smallest elements in a dataset.
  
diff --cc Lib/inspect.py
Simple merge
Simple merge
diff --cc Lib/pdb.py
Simple merge
index f1b0e4a6203c0d80458f01713decbe47b25677cd,73399115b6a89d4a0cc4a053f1fbfabcbbbc47ab..fc4ee4eed0fffd2c599b579d1c8814ba23910d72
@@@ -284,23 -283,11 +284,23 @@@ class BaseServer
              timeout = self.timeout
          elif self.timeout is not None:
              timeout = min(timeout, self.timeout)
 -        fd_sets = _eintr_retry(select.select, [self], [], [], timeout)
 -        if not fd_sets[0]:
 -            self.handle_timeout()
 -            return
 -        self._handle_request_noblock()
 +        if timeout is not None:
 +            deadline = time() + timeout
 +
 +        # Wait until a request arrives or the timeout expires - the loop is
-         # necessary to accomodate early wakeups due to EINTR.
++        # necessary to accommodate early wakeups due to EINTR.
 +        with _ServerSelector() as selector:
 +            selector.register(self, selectors.EVENT_READ)
 +
 +            while True:
 +                ready = selector.select(timeout)
 +                if ready:
 +                    return self._handle_request_noblock()
 +                else:
 +                    if timeout is not None:
 +                        timeout = deadline - time()
 +                        if timeout < 0:
 +                            return self.handle_timeout()
  
      def _handle_request_noblock(self):
          """Handle one request, without blocking.
diff --cc Lib/ssl.py
Simple merge
Simple merge
Simple merge