mirror of
https://github.com/Mikaela/Limnoria.git
synced 2024-11-23 19:19:32 +01:00
Added a proper random.choice implementation that works with non-sequence iterables.
This commit is contained in:
parent
7ef716f406
commit
04876865f1
17
src/fix.py
17
src/fix.py
@ -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):
|
||||
|
Loading…
Reference in New Issue
Block a user