Gareth Rees added the comment:
In order for this to work, the __getitem__ method needs to be:
def __getitem__(self, key):
if 0 <= key < self.n:
return self.elem
else:
raise IndexError(key)
But unfortunately this is very bad for the performance of the test. The
original code, with [1]*n:
Ran 1 test in 5.256s
With RepeatedSequence(1, n):
Ran 1 test in 33.620s
So that's no good. However, I notice that although the documentation of choices
specifies that weights is a sequence, in fact it seems only to require an
iterable:
cum_weights = list(_itertools.accumulate(weights))
so itertools.repeat works, and is faster than the original code:
Ran 1 test in 4.991s
Patch attached, in case it's acceptable to pass an iterable here.
----------
keywords: +patch
Added file: http://bugs.python.org/file45546/issue28743.patch
_______________________________________
Python tracker <[email protected]>
<http://bugs.python.org/issue28743>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com