10000 Rename weighted_choices() to just choices() · python/cpython@28aa4a0 · GitHub
[go: up one dir, main page]

Skip to content

Commit 28aa4a0

Browse files
committed
Rename weighted_choices() to just choices()
1 parent c98b26a commit 28aa4a0

File tree

4 files changed

+32
-32
lines changed

4 files changed

+32
-32
lines changed

Doc/library/random.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ Functions for sequences:
124124
Return a random element from the non-empty sequence *seq*. If *seq* is empty,
125125
raises :exc:`IndexError`.
126126

127-
.. function:: weighted_choices(k, population, weights=None, *, cum_weights=None)
127+
.. function:: choices(k, population, weights=None, *, cum_weights=None)
128128

129129
Return a *k* sized list of elements chosen from the *population* with replacement.
130130
If the *population* is empty, raises :exc:`IndexError`.

Lib/random.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
"randrange","shuffle","normalvariate","lognormvariate",
5252
"expovariate","vonmisesvariate","gammavariate","triangular",
5353
"gauss","betavariate","paretovariate","weibullvariate",
54-
"getstate","setstate", "getrandbits", "weighted_choices",
54+
"getstate","setstate", "getrandbits", "choices",
5555
"SystemRandom"]
5656

5757
NV_MAGICCONST = 4 * _exp(-0.5)/_sqrt(2.0)
@@ -337,7 +337,7 @@ def sample(self, population, k):
337337
result[i] = population[j]
338338
return result
339339

340-
def weighted_choices(self, k, population, weights=None, *, cum_weights=None):
340+
def choices(self, k, population, weights=None, *, cum_weights=None):
341341
"""Return a k sized list of population elements chosen with replacement.
342342
343343
If the relative weights or cumulative weights are not specified,
@@ -749,7 +749,7 @@ def _test(N=2000):
749749
randrange = _inst.randrange
750750
sample = _inst.sample
751751
shuffle = _inst.shuffle
752-
weighted_choices = _inst.weighted_choices
752+
choices = _inst.choices
753753
normalvariate = _inst.normalvariate
754754
lognormvariate = _inst.lognormvariate
755755
expovariate = _inst.expovariate

Lib/test/test_random.py

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -142,72 +142,72 @@ def test_sample_inputs(self):
142142
def test_sample_on_dicts(self):
143143
self.assertRaises(TypeError, self.gen.sample, dict.fromkeys('abcdef'), 2)
144144

145-
def test_weighted_choices(self):
146-
weighted_choices = self.gen.weighted_choices
145+
def test_choices(self):
146+
choices = self.gen.choices
147147
data = ['red', 'green', 'blue', 'yellow']
148148
str_data = 'abcd'
149149
range_data = range(4)
150150
set_data = set(range(4))
151151

152152
# basic functionality
153153
for sample in [
154-
weighted_choices(5, data),
155-
weighted_choices(5, data, range(4)),
156-
weighted_choices(k=5, population=data, weights=range(4)),
157-
weighted_choices(k=5, population=data, cum_weights=range(4)),
154+
choices(5, data),
155+
choices(5, data, range(4)),
156+
choices(k=5, population=data, weights=range(4)),
157+
choices(k=5, population=data, cum_weights=range(4)),
158158
]:
159159
self.assertEqual(len(sample), 5)
160160
self.assertEqual(type(sample), list)
161161
self.assertTrue(set(sample) <= set(data))
162162

163163
# test argument handling
164-
with self.assertRaises(TypeError): # missing arguments
165-
weighted_choices(2)
164+
with self.assertRaises(TypeError): # missing arguments
165+
choices(2)
166166

167-
self.assertEqual(weighted_choices(0, data), []) # k == 0
168-
self.assertEqual(weighted_choices(-1, data), []) # negative k behaves like ``[0] * -1``
167+
self.assertEqual(choices(0, data), []) # k == 0
168+
self.assertEqual(choices(-1, data), []) # negative k behaves like ``[0] * -1``
169169
with self.assertRaises(TypeError):
170-
weighted_choices(2.5, data) # k is a float
170+
choices(2.5, data) # k is a float
171171

172-
self.assertTrue(set(weighted_choices(5, str_data)) <= set(str_data)) # population is a string sequence
173-
self.assertTrue(set(weighted_choices(5, range_data)) <= set(range_data)) # population is a range
172+
self.assertTrue(set(choices(5, str_data)) <= set(str_data)) # population is a string sequence
173+
self.assertTrue(set(choices(5, range_data)) <= set(range_data)) # population is a range
174174
with self.assertRaises(TypeError):
175-
weighted_choices(2.5, set_data) # population is not a sequence
175+
choices(2.5, set_data) # population is not a sequence
176176

177-
self.assertTrue(set(weighted_choices(5, data, None)) <= set(data)) # weights is None
178-
self.assertTrue(set(weighted_choices(5, data, weights=None)) <= set(data))
177+
self.assertTrue(set(choices(5, data, None)) <= set(data)) # weights is None
178+
self.assertTrue(set(choices(5, data, weights=None)) <= set(data))
179179
with self.assertRaises(ValueError):
180-
weighted_choices(5, data, [1,2]) # len(weights) != len(population)
180+
choices(5, data, [1,2]) # len(weights) != len(population)
181181
with self.assertRaises(IndexError):
182-
weighted_choices(5, data, [0]*4) # weights sum to zero
182+
choices(5, data, [0]*4) # weights sum to zero
183183
with self.assertRaises(TypeError):
184-
weighted_choices(5, data, 10) # non-iterable weights
184+
choices(5, data, 10) # non-iterable weights
185185
with self.assertRaises(TypeError):
186-
weighted_choices(5, data, [None]*4) # non-numeric weights
186+
choices(5, data, [None]*4) # non-numeric weights
187187
for weights in [
188188
[15, 10, 25, 30], # integer weights
189189
[15.1, 10.2, 25.2, 30.3], # float weights
190190
[Fraction(1, 3), Fraction(2, 6), Fraction(3, 6), Fraction(4, 6)], # fractional weights
191191
[True, False, True, False] # booleans (include / exclude)
192192
]:
193-
self.assertTrue(set(weighted_choices(5, data, weights)) <= set(data))
193+
self.assertTrue(set(choices(5, data, weights)) <= set(data))
194194

195195
with self.assertRaises(ValueError):
196-
weighted_choices(5, data, cum_weights=[1,2]) # len(weights) != len(population)
196+
choices(5, data, cum_weights=[1,2]) # len(weights) != len(population)
197197
with self.assertRaises(IndexError):
198-
weighted_choices(5, data, cum_weights=[0]*4) # cum_weights sum to zero
198+
choices(5, data, cum_weights=[0]*4) # cum_weights sum to zero
199199
with self.assertRaises(TypeError):
200-
weighted_choices(5, data, cum_weights=10) # non-iterable cum_weights
200+
choices(5, data, cum_weights=10) # non-iterable cum_weights
201201
with self.assertRaises(TypeError):
202-
weighted_choices(5, data, cum_weights=[None]*4) # non-numeric cum_weights
202+
choices(5, data, cum_weights=[None]*4) # non-numeric cum_weights
203203
with self.assertRaises(TypeError):
204-
weighted_choices(5, data, range(4), cum_weights=range(4)) # both weights and cum_weights
204+
choices(5, data, range(4), cum_weights=range(4)) # both weights and cum_weights
205205
for weights in [
206206
[15, 10, 25, 30], # integer cum_weights
207207
[15.1, 10.2, 25.2, 30.3], # float cum_weights
208208
[Fraction(1, 3), Fraction(2, 6), Fraction(3, 6), Fraction(4, 6)], # fractional cum_weights
209209
]:
210-
self.assertTrue(set(weighted_choices(5, data, cum_weights=weights)) <= set(data))
210+
self.assertTrue(set(choices(5, data, cum_weights=weights)) <= set(data))
211211

212212
def test_gauss(self):
213213
# Ensure that the seed() method initializes all the hidden state. In

Misc/NEWS

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ Library
101101
- Issue #27691: Fix ssl module's parsing of GEN_RID subject alternative name
102102
fields in X.509 certs.
103103

104-
- Issue #18844: Add random.weighted_choices().
104+
- Issue #18844: Add random.choices().
105105

106106
- Issue #25761: Improved error reporting about truncated pickle data in
107107
C implementation of unpickler. UnpicklingError is now raised instead of

0 commit comments

Comments
 (0)
0