pytoolbox.throttles module

Throttling classes implementing various throttling policies.

class pytoolbox.throttles.TimeThrottle(min_time_delta)[source]

Bases: object

Time based throttling class.

>>> import datetime
>>> def slow_range(*args):
...     for i in range(*args):
...         time.sleep(0.5)
...         yield i
>>> t1, t2 = (TimeThrottle(t) for t in (datetime.timedelta(minutes=1), 0.2))
>>> list(t1.throttle_iterable((i, i) for i in range(10)))
[(0, 0), (9, 9)]
>>> list(t2.throttle_iterable(slow_range(3)))
[0, 1, 2]
__init__(min_time_delta)[source]

Initialize self. See help(type(self)) for accurate signature.

is_throttled()[source]

Return a boolean indicating if you should throttle.

throttle_iterable(objects, callback=<function TimeThrottle.<lambda>>)[source]

Consume and skips some objects to yield them at defined min_delay. First and last objects are always returned.

  • Set callback to a callable with the signature is_throttled_args = callback(object). Used by subclasses.
class pytoolbox.throttles.TimeAndRatioThrottle(min_ratio_delta, min_time_delta, max_time_delta)[source]

Bases: pytoolbox.throttles.TimeThrottle

Time and ratio based throttling class.

>>> import datetime
>>> def slow_range(*args):
...     for i in range(*args):
...         time.sleep(0.5)
...         yield i
>>> t1, t2 = (TimeAndRatioThrottle(0.3, t, 10*t) for t in (datetime.timedelta(minutes=1), 0.4))
>>> list(t1.throttle_iterable(list(range(9)), lambda i: [i/9]))
[0, 8]
>>> list(t2.throttle_iterable(slow_range(9), lambda i: [i/9]))
[0, 3, 6, 8]
__init__(min_ratio_delta, min_time_delta, max_time_delta)[source]

Initialize self. See help(type(self)) for accurate signature.

is_throttled(ratio)[source]

Return a boolean indicating if you should throttle.