[Python-checkins] Be consistent about the use of from-imports in random module (GH-11837)

Miss Islington (bot) webhook-mailer at python.org
Wed Feb 13 05:04:23 EST 2019


https://github.com/python/cpython/commit/cfd31f0af20f76849bbec6185efea01c72349f2a
commit: cfd31f0af20f76849bbec6185efea01c72349f2a
branch: master
author: Raymond Hettinger <rhettinger at users.noreply.github.com>
committer: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
date: 2019-02-13T02:04:17-08:00
summary:

Be consistent about the use of from-imports in random module (GH-11837)



Minor code clean-up.

files:
M Lib/random.py

diff --git a/Lib/random.py b/Lib/random.py
index 8925b52c4730..754b2952a0f7 100644
--- a/Lib/random.py
+++ b/Lib/random.py
@@ -43,8 +43,8 @@
 from os import urandom as _urandom
 from _collections_abc import Set as _Set, Sequence as _Sequence
 from hashlib import sha512 as _sha512
-import itertools as _itertools
-import bisect as _bisect
+from itertools import accumulate as _accumulate
+from bisect import bisect as _bisect
 import os as _os
 
 __all__ = ["Random","seed","random","uniform","randint","choice","sample",
@@ -390,12 +390,12 @@ def choices(self, population, weights=None, *, cum_weights=None, k=1):
                 _int = int
                 n += 0.0    # convert to float for a small speed improvement
                 return [population[_int(random() * n)] for i in range(k)]
-            cum_weights = list(_itertools.accumulate(weights))
+            cum_weights = list(_accumulate(weights))
         elif weights is not None:
             raise TypeError('Cannot specify both weights and cumulative weights')
         if len(cum_weights) != n:
             raise ValueError('The number of weights does not match the population')
-        bisect = _bisect.bisect
+        bisect = _bisect
         total = cum_weights[-1] + 0.0   # convert to float
         hi = n - 1
         return [population[bisect(cum_weights, random() * total, 0, hi)]



More information about the Python-checkins mailing list