text
stringlengths 0
828
|
---|
e.g., `[1, 2, 4, 8, 16]`. |
jitter (float): A factor between `-1.0` and `1.0`, used to |
uniformly randomize and thus spread out timeouts in a distributed |
system, avoiding rhythm effects. Positive values use the base |
backoff curve as a maximum, negative values use the curve as a |
minimum. Set to 1.0 or `True` for a jitter approximating |
Ethernet's time-tested backoff solution. Defaults to `False`. |
"""""" |
start = float(start) |
stop = float(stop) |
factor = float(factor) |
if start < 0.0: |
raise ValueError('expected start >= 0, not %r' % start) |
if factor < 1.0: |
raise ValueError('expected factor >= 1.0, not %r' % factor) |
if stop == 0.0: |
raise ValueError('expected stop >= 0') |
if stop < start: |
raise ValueError('expected stop >= start, not %r' % stop) |
if count is None: |
denom = start if start else 1 |
count = 1 + math.ceil(math.log(stop/denom, factor)) |
count = count if start else count + 1 |
if count != 'repeat' and count < 0: |
raise ValueError('count must be positive or ""repeat"", not %r' % count) |
if jitter: |
jitter = float(jitter) |
if not (-1.0 <= jitter <= 1.0): |
raise ValueError('expected jitter -1 <= j <= 1, not: %r' % jitter) |
cur, i = start, 0 |
while count == 'repeat' or i < count: |
if not jitter: |
cur_ret = cur |
elif jitter: |
cur_ret = cur - (cur * jitter * random.random()) |
yield cur_ret |
i += 1 |
if cur == 0: |
cur = 1 |
elif cur < stop: |
cur *= factor |
if cur > stop: |
cur = stop |
return" |
4967,"def bucketize(src, key=None, value_transform=None, key_filter=None): |
""""""Group values in the *src* iterable by the value returned by *key*, |
which defaults to :class:`bool`, grouping values by truthiness. |
>>> bucketize(range(5)) |
{False: [0], True: [1, 2, 3, 4]} |
>>> is_odd = lambda x: x % 2 == 1 |
>>> bucketize(range(5), is_odd) |
{False: [0, 2, 4], True: [1, 3]} |
Value lists are not deduplicated: |
>>> bucketize([None, None, None, 'hello']) |
{False: [None, None, None], True: ['hello']} |
Bucketize into more than 3 groups |
>>> bucketize(range(10), lambda x: x % 3) |
{0: [0, 3, 6, 9], 1: [1, 4, 7], 2: [2, 5, 8]} |
``bucketize`` has a couple of advanced options useful in certain |
cases. *value_transform* can be used to modify values as they are |
added to buckets, and *key_filter* will allow excluding certain |
buckets from being collected. |
>>> bucketize(range(5), value_transform=lambda x: x*x) |
{False: [0], True: [1, 4, 9, 16]} |
>>> bucketize(range(10), key=lambda x: x % 3, key_filter=lambda k: k % 3 != 1) |
{0: [0, 3, 6, 9], 2: [2, 5, 8]} |
Note in some of these examples there were at most two keys, ``True`` and |
``False``, and each key present has a list with at least one |
item. See :func:`partition` for a version specialized for binary |
use cases. |
"""""" |
if not is_iterable(src): |
raise TypeError('expected an iterable') |
if key is None: |
key = bool |
if not callable(key): |
raise TypeError('expected callable key function') |
if value_transform is None: |
value_transform = lambda x: x |
if not callable(value_transform): |
raise TypeError('expected callable value transform function') |
ret = {} |
for val in src: |
key_of_val = key(val) |
if key_filter is None or key_filter(key_of_val): |
ret.setdefault(key_of_val, []).append(value_transform(val)) |
return ret" |
Subsets and Splits