Added a proper random.choice implementation that works with non-sequence iterables.

This commit is contained in:
Jeremy Fincher 2004-04-17 16:35:31 +00:00
parent 7ef716f406
commit 04876865f1

View File

@ -33,6 +33,8 @@
Fixes stuff that Python should have but doesn't.
"""
from __future__ import division
__revision__ = "$Id$"
__all__ = []
@ -44,6 +46,21 @@ import new
import string
string.ascii = string.maketrans('', '')
import random
_choice = random.choice
def choice(iterable):
if isinstance(iterable, list):
return _choice(iterable)
else:
n = 1
ret = None
for x in iterable:
if random.random() < 1/n:
ret = x
n += 1
return ret
random.choice = choice
curry = new.instancemethod
def ignore(*args, **kwargs):