ControlFlowMixin¶
-
class
support.control_flow.
ControlFlowMixin
¶ Bases:
object
Mixin class for controlling when and until when functions are called. Examples assume this class has been mixed into some child class:
MixedIn(ControlFlowMixin): pass mixed_in = MixedIn()
-
tz
¶ timezone. Defaults to UTC
- Type
pytz.timezone
Attributes Summary
Methods Summary
at
(when)Created a _DelayedCall instance that will run or loop at a specified time.
next_up
()Return an empty _DelayedCall instance.
now
()- returns
timezone aware datetime object
until
([cond])Run some generator until some condition, or list of conditions is met.
Attributes Documentation
-
tz
= <UTC>¶
Methods Documentation
-
at
(when)¶ Created a _DelayedCall instance that will run or loop at a specified time.
Examples:
def runner(): yield for e in mixed_in.at( MixedIn.now() + datetime.timedelta(seconds=5)).run(runner): yield e
- Parameters
when (datetime.datetime) – timezone aware datetime object indicating when to _DelayedCall methods.
- Returns
_DelayedCall
-
next_up
()¶ Return an empty _DelayedCall instance.
-
classmethod
now
()¶ - Returns
timezone aware datetime object
- Return type
datetime.datetime
-
until
(cond=None)¶ Run some generator until some condition, or list of conditions is met.
Examples:
Say we have some function
runner
defined as follows:def runner(): print("runner called") yield
We can use until in a variety of ways:
import datetime import random # run until runner is complete, or for next 5 seconds, # which ever comes first for e in mixed_in.until( MixedIn.now() + datetime.timedelta(seconds=5)).run(runner): yield e # run until the following function evaluates to True def condition_factory(): def condition(): rand = random.random() if rand > 0.5: return True return False return condition for e in mixed_in.until(condition_factory()).run(runner): yield e # we can pass a list of conditions if we want: for e in mixed_in.until( [condition_factory(), condition_factory()]).run(runner): yield e
- Parameters
cond (list/callable/datetime.datetime) – List of callables that return booleans, a single boolean-returning callable, or a datetime.datetime object. If the latter is passed, the resulting _DelayedCall object will run, loop, or wait until the specified time.
- Returns
_DelayedCall
-