hio.base.doing
hio.core.doing Module
Module Contents
Classes
Doist is the root coroutine scheduler |
|
Doer base class for hierarchical structured async coroutine like generators. |
|
ReDoer is an example sub class whose .recur is a generator method not a |
|
DoDoer implements Doist like functionality to allow nested scheduling of Doers. |
|
ExDoer is example Doer for testing and demonstration |
|
TryDoer supports testing with methods to record sends and yields |
Functions
|
Returns Doist compatible copy, g, of converted generator function f. |
|
Returns decorator that makes decorated generator function Doist compatible. |
|
Bare bones generator function template as example of generator function |
|
Example generator function for testing and demonstration. |
|
Example decorated generator function for use with doize decorator. |
|
Generator function test example non-class based generator. |
Attributes
- class hio.base.doing.Doist(real=False, limit=None, doers=None, **kwa)[source]
Bases:
hio.base.tyming.TymistDoist is the root coroutine scheduler Provides relative cycle time in seconds with .tyme property to doers it runs The relative cycle time is advanced in .tock size increments by the by the .tick method. The doist may treat .tyme as artificial time or synchonize it to real time.
- .enter method prepares deeds deque of triples (dog, retyme, doer) where
dog is a doer generator returned by calling doer generator instances, functions, or methods.
- .recur method runs its deeds deque of triples (dog, retyme, doer) once per
invocation. This synchronizes their cycle time .tyme to the Doist’s tyme.
- .do method repeatedly runs .recur until generators are complete
it may either repeat as fast as possbile or repeat at real time increments.
- Inherited Class Attributes:
.Tock is default .tock
- real
True means run in real time, Otherwise as fast as possible.
- Type
boolean
- limit
maximum run tyme limit then closes all doers
- Type
float
- done
True means completed due to limit or all deeds completed False is forced complete due to error
- Type
boolean
- doers
Doer class instances, generator methods or function callables with attributes tock, done, and opts dict(). Used throughout the execution lifecycle.
- Type
list
- deeds
Tuples of form (dog, retyme, doer). Where: dog is generator created by doer retyme is tyme (real or simulated) in seconds when dog should run next doer is associated doer in .doers list used to assign its .done state
given completion state of its dog
Used throughout the execution lifecycle. The normal case is use the default empty initialization performed here and update in .enter().
- Type
deque
- Inherited Properties:
tyme: is float relative cycle time, .tyme is artificial time : is float tyme increment of .tick()
Properties:
- Inherited Methods:
.tick increments .tyme by one .tock or provided tock
- .enter prepare deeds, deque of triples (dog, retyme, doer)
- .recur run through all deeds once
- .do repeadedly call .recur until all dogs in deeds are complete or
times out do to reaching time limit
- do(self, doers=None, limit=None, tyme=None)[source]
Readies deeds deque from .doers or doers if any and then iteratively runs .recur over deeds deque until completion of all deeds. Each entry in deeds is a triple (dog, retyme, doer) where:
dog is generator retyme is tyme (real or simulated) in seconds when dog should run next doer is from .doers list used to assign its .done state given associated completion state of its dog
If interrupted by exception call .close on each dog to force exit context.
Keyboard interrupt (cntl-c) forces exit.
Once finally clause closes a generator it must be reinited before it can be run again
- Parameters
doers (iterable) – generator method or function callables with attributes tock, done, and opts dict(). This may be used to update the .doers attribute which is used throughout the execution lifecycle. If not provided uses .doers. Parameterization here of doers enables some special cases. The normal case is to initialize in .__init__ or here.
limit (float) – is real time limit on execution. Forces close of all dogs.
tyme (float) – is optional starting tyme. Resets .tyme to tyme whe provided. If not provided uses current .tyme
- Returns
None
See: https://stackoverflow.com/questions/40528867/setting-attributes-on-func For setting attributes on bound methods.
- enter(self, doers=None)[source]
Enter context Returns (deque): deeds deque of triples (dog, retyme, doer) where:
dog is generator retyme is tyme (real or simulated) in seconds when dog should run next doer is from .doers list used to assign its .done state given
completion state of its dog
Calls each generator callable (instance or function or method) in .doers to create each generator dog. Injects own tymth function closure, and
generator function’s own tock, and opts.
Runs enter context of each dog by calling next(dog)
- Parameters
attributes (doers is list of generator method or function callables with) – .tock is tyme increment in seconds .done is Boolean completion state .opts is dict() of optional parameters If not provided uses .doers. The normal case is to initialize in .__init__. or .do().
triples (deeds is deque of deed) –
- Returns
A deed is tuple of form (dog, retyme, doer). If not provided uses .deeds.
- Return type
deeds deque()
See: https://stackoverflow.com/questions/40528867/setting-attributes-on-func For setting attributes on bound methods.
- recur(self, deeds=None)[source]
Recur once through deeds deque of tuples (triples) of form (dog, retyme, doer) and update in place
- Each deed is deque of tuples of form (dog, retyme, doer) where:
dog is generator retyme is tyme (real or simulated) in seconds when dog should run next doer is from .doers list used to assign its .done state given associated completion state of its dog
Each cycle checks all generators in deeds deque and runs if retyme past. At end of cycle advances .tyme by one .tock by calling .tick()
- Parameters
deeds (deque) – tuples of form (dog, retyme, doer). Parameterization here of deeds enables some special cases.
The Parameterization here of deeds enables some special cases such as manual testing or iteraton. The normal case is to initialize .doers in .__init__. or .do() and to initialize .deeds in .__init__. and then update in .enter()
- exit(self, deeds=None)[source]
Force exit each still opened deed calling .close on the dog generator which throws a GeneratorExit to the generator. This executes the close context (GeneratorExit) which then excecutes the exit context in the finally caluse. Each dogs exit is responsible for releasing resources Previously aborted or closed dogs have already exited Close any running dogs in reverse order so that enters and exits are nested pairs so that the corresponding exits appear in reverse order to their entes. This preserves nested resource dependencies. For example:
- enter A,
- enter B,
enter C, exit C,
exit B,
exit A
- Parameters
deeds (deque) – tuples of form (dog, retyme, doer). If not provided uses .deeds. Parameterization here of deeds enables some special cases.
- hio.base.doing.doify(f, *, name=None, tock=0.0, **opts)[source]
Returns Doist compatible copy, g, of converted generator function f. Each invoction of doify(f) returns a unique copy of doified function f. Imbues copy, g, of converted generator function, f, with attributes used by Doist.enter() or DoDoer.enter(). Allows multiple instances of copy, g, of generator function, f, each with unique attributes.
Usage: def f():
pass
c = doify(f, name=’c’)
- Parameters
function (f is generator) –
copy (name is new function name for returned doified copy g. Default is to) – f.__name__
g (tock is default tock attribute of doified copy) –
attribute (opts is dictionary of remaining parameters that becomes .opts) – of doified copy g
Based on: https://stackoverflow.com/questions/972/adding-a-method-to-an-existing-object-instance
- hio.base.doing.doize(tock=0.0, **opts)[source]
Returns decorator that makes decorated generator function Doist compatible. Imbues decorated generator function with attributes used by Doist.enter() or DoDoer.enter(). Only one instance of decorated function with shared attributes is allowed.
Usage: @doize def f():
pass
- Parameters
f (tock is default tock attribute of doized) –
attribute (opts is dictionary of remaining parameters that becomes .opts) – of doized f
- class hio.base.doing.Doer(*, tymth=None, tock=0.0, **opts)[source]
Bases:
hio.base.tyming.TymeeDoer base class for hierarchical structured async coroutine like generators. Doer.__call__ on instance returns generator. Interface for Doist etc is generator function like object. Doer is generator method instance creator and has extra methods and attributes that a plain generator function does not
The .do method executes other methods each corresponding to one of the six econtexts:
enter, recur, clean, exit, (unforced) close, abort (forced)
- Actual context order may be one of:
enter, recur, clean, exit enter, recur, close, exit enter, recur, abort, exit enter, abort, exit
- .done is Boolean completion state
True means completed Otherwise incomplete. Incompletion maybe due to close or abort.
- .opts is dict of injected options into its .do generator by scheduler
- Inherited Properties:
- .tyme is float relative cycle time of associated Tymist .tyme obtained
via injected .tymth function wrapper closure.
- .tymth is function wrapper closure returned by Tymist .tymeth() method.
When .tymth is called it returns associated Tymist .tyme. .tymth provides injected dependency on Tymist tyme base.
- Properties:
- .tock is float, desired time in seconds between runs or until next run,
non negative, zero means run asap
- Inherited Methods:
.wind injects ._tymth dependency from associated Tymist to get its .tyme
- .__call__ makes instance callable
Appears as generator function that returns generator
- .do is generator method that returns generator
- .enter is enter context action method
- .recur is recur context action method or generator method
- .clean is clean context action method
- .exit is exit context method
- .close is close context method
- .abort is abort context method
- Hidden:
- ._tymth is injected function wrapper closure returned by .tymen() of
associated Tymist instance that returns Tymist .tyme. when called.
._tock is hidden attribute for .tock property
- __call__(self, **kwa)[source]
Returns generator Does not advance to first yield. The advance to first yield effectively invodes the enter or open context on the generator. To enter either call .next or .send(None) on generator
- property tock(self)[source]
tock property getter, get ._tock .tock is float desired .tyme increment in seconds
- do(self, tymth, *, tock=0.0, **opts)[source]
Generator method to run this doer. Calling this method returns generator. Interface matches generator function for compatibility. To customize create subclasses and override the lifecycle methods:
.enter, .recur, .exit, .close, .abort
- Parameters
of (tymth is injected function wrapper closure returned by .tymen()) – Tymist instance. Calling tymth() returns associated Tymist .tyme.
value (tock is injected initial tock) –
parameters (args is dict of injected optional additional) –
- enter(self)[source]
Do ‘enter’ context actions. Override in subclass. Not a generator method. Set up resources. Comparable to context manager enter.
- recur(self, tyme)[source]
Do ‘recur’ context actions. Override in subclass. Regular method that perform repetitive actions once per invocation. Assumes resource setup in .enter() and resource takedown in .exit() (see ReDoer below for example of .recur that is a generator method)
- Returns completion state of recurrence actions.
True means done False means continue
- Parameters
here. (Doist feeds its .tyme through .send to .do yield which passes it) –
.recur maybe implemented by a subclass either as a non-generator method or a generator method. This stub here is as a non-generator method. The base class .do detects which type:
- If non-generator .do method runs .recur method once per iteration
until .recur returns (True)
- If generator .do method runs .recur with (yield from) until .recur
returns (see ReDoer for example of generator .recur)
- clean(self)[source]
Do ‘clean’ context actions. Override in subclass. Not a generator method. Clean up resources that are unique to a clean exit. Called by else after normal return.
- exit(self)[source]
Do ‘exit’ context actions. Override in subclass. Not a generator method. Clean up resources. Comparable to context manager exit. Called by finally after normal return, close, or abort. After .exit() do returns resulting in StopIteration.
- class hio.base.doing.ReDoer(*, tymth=None, tock=0.0, **opts)[source]
Bases:
DoerReDoer is an example sub class whose .recur is a generator method not a plain method. Its .do method detects that its .recur is a generator method and executes it using yield from instead of just calling the method.
- Inherited Attributes:
- .done is Boolean completion state:
True means completed Otherwise incomplete. Incompletion maybe due to close or abort.
.opts is dict of injected options into its .do generator by scheduler
- Inherited Properties:
- .tyme is float relative cycle time of associated Tymist .tyme obtained
via injected .tymth function wrapper closure.
- .tymth is function wrapper closure returned by Tymist .tymeth() method.
When .tymth is called it returns associated Tymist .tyme. .tymth provides injected dependency on Tymist tyme base.
- .tock is float, desired time in seconds between runs or until next run,
non negative, zero means run asap
- Inherited Methods:
.wind injects ._tymth dependency from associated Tymist to get its .tyme .__call__ makes instance callable
Appears as generator function that returns generator
.do is generator method that returns generator .enter is enter context action method .recur is recur context action method or generator method .exit is exit context method .close is close context method .abort is abort context method
- Overidden Methods:
.recur
- Hidden:
- ._tymth is injected function wrapper closure returned by .tymen() of
associated Tymist instance that returns Tymist .tyme. when called.
._tock is hidden attribute for .tock property
- recur(self)[source]
Do ‘recur’ context actions as a generator method. Override in subclass. Assumes resource setup in .enter() and resource takedown in .exit() (see Doer for example of .recur that is a regular method)
yield the current .tock accepts the current tyme returns the .done
- Parameters
yield (tyme is initial output of send fed to do) –
tyme (Doist feeds its) –
- Returns completion state of recurrence actions.
True means done False means continue
Maybe a non-generator method or a generator method. For base class do:
non-generator recur method runs until returns (True) generator recur method runs until returns (yield from)
- class hio.base.doing.DoDoer(doers=None, always=False, **kwa)[source]
Bases:
DoerDoDoer implements Doist like functionality to allow nested scheduling of Doers. Each DoDoer runs a list of doers like a Doist but using the tyme from its
injected tymth for the associated tymist as injected by its ultimate root parent Doist and any intervening parent DoDoer(s).
Scheduling hierarchy: Doist->DoDoer…->DoDoer->Doers
- Inherited Attributes:
- .done is Boolean completion state:
True means completed Otherwise incomplete. Incompletion maybe due to close or abort.
.opts is dict of injected options for its generator .do
Attributes:
- Inherited Properties:
- .tyme is float relative cycle time of associated Tymist .tyme obtained
via injected .tymth function wrapper closure.
- .tymth is function wrapper closure returned by Tymist .tymeth() method.
When .tymth is called it returns associated Tymist .tyme. .tymth provides injected dependency on Tymist tyme base.
- .tock is float, desired time in seconds between runs or until next run,
non negative, zero means run asap
- Properties:
- doers (list): Doer or Doist compatible generator instances,
functions, or methods.
- deeds (deque): tuples of form (dog, retyme, doer) where:
dog is generator created by doer. retyme is tyme in seconds when next should run may be real or simulated. doer is associated doer in .doers list. Used throughout the execution lifecycle. The normal case is use the default empty initialization performed here and update in .enter().
- always (bool): True means keep running even when all dogs in deeds
are complete. Enables dynamically managing extending or removing doers and associated deeds while running.
- Inherited Methods:
.wind injects ._tymth dependency from associated Tymist to get its .tyme .__call__ makes instance callable
Appears as generator function that returns generator
.do is generator method that returns generator .enter is enter context action method .recur is recur context action method or generator method .clean is clean context action method .exit is exit context method .close is close context method .abort is abort context method
- Overidden Methods:
.do .enter .recur .exit
- Hidden:
- ._tymth is injected function wrapper closure returned by .tymen() of
associated Tymist instance that returns Tymist .tyme. when called.
._tock is hidden attribute for .tock property ._always is hidden attribute for .always property ._doers is hidden attribute for .doers property ._deeds is hidden attribute for .deeds property
- property doers(self)[source]
doers property getter, get ._doers .doers is list of doist compatible generator instances, functions, or
methods
- property deeds(self)[source]
deeds property getter, get ._deeds .deeds is deque of triples, each of form (dog, retyme, doer)
- property always(self)[source]
always property getter, get ._always .always is Boolean, True means keep running even when all dogs in deeds
are complete. Enables dynamically managing extending or removing doers and associated deeds while running.
- do(self, tymth, tock=0.0, doers=None, always=None, **opts)[source]
Generator method to run this doer. Equivalent of doist.do Calling this method returns generator Interface matched generator function for compatibility
- Parameters
of (tymth is injected function wrapper closure returned by .tymen()) – Tymist instance. Calling tymth() returns associated Tymist .tyme.
value (tock is injected initial tock) –
attributes (doers is list of generator method or function callables with) – tock, done, and opts dict(). This may be used to update the .doers attribute which is used throughout the execution lifecycle. If not provided uses .doers. Parameterization here of doers enables some special cases. The normal case is to initialize in .__init__.
deeds (always is Boolean. True means keep running even when all dogs in) – are complete. Enables dynamically managing extending or removing doers and associated deeds while running. When not provided use .always.
parameters (opts is dict of injected optional additional) –
- enter(self, doers=None)[source]
Do ‘enter’ context actions. Equivalent of Doist.enter()
- Returns deeds deque of triples (dog, retyme, doer) where:
dog is generator created by doer retyme is tyme in seconds when next should run may be real or simulated doer is doer for dog from doers list
Calls each generator callable (function or method) in .doers to create each generator dog.
Runs enter context of each dog by calling next(dog)
- Parameters
doers (list) – Doer Instance, generator method or function callables with attributes tock, done, and opts dict(). If not provided uses .doers. Parameterization here of doers enables some special cases. The normal case is to initialize in .__init__.
- Returns
A deed is tuple of form (dog, retyme, doer). If not provided uses .deeds.
- Return type
deeds deque()
See: https://stackoverflow.com/questions/40528867/setting-attributes-on-func For setting attributes on bound methods.
- recur(self, tyme, deeds=None)[source]
Do ‘recur’ context actions. Equivalent of Doist.recur
- Parameters
tyme (float) –
- is output of send fed to do yield, The root scheduler
Doist feeds its .tyme which propagates down the chain of DoDoers Because tymist is injected by doist or dodoer, self.tyme is same as tyme. So may use either which is more convenient.
- deeds (deque): tuples of form (dog, retyme, doer).
If not provided uses .deeds. Parameterization here of deeds enables some special cases.
- Returns completion state of recurrence actions.
True means done False means continue
Cycle once through deeds deque and update in place
Each cycle checks all generators dogs in deeds deque and runs if retyme past.
- exit(self, deeds=None)[source]
Do ‘exit’ context actions.
- Parameters
deeds (deque) – of deed tuples of form (dog, retyme, doer) If not provided uses .deeds. Parameterization here of deeds enables some special cases.
See: https://stackoverflow.com/questions/40528867/setting-attributes-on-func For setting attributes on bound methods.
- hio.base.doing.bareDo(tymth=None, tock=0.0, **opts)[source]
Bare bones generator function template as example of generator function suitable for use with either doify wrapper or doize decorator. Make copy and rename for given application. Calling copied renamed function returns basic generator. Wrapping copied renamed function with doify returns yet unique wrapped copy with unique values of injected attributes and parameters and further renamed by wrapper. Decorating copied renamed function with doize returns singleton with injected parameter values.
- Injected Attributes:
g.tock = tock # default tock attributes g.done = None # default done state g.opts
- Parameters
of (tymth is injected function wrapper closure returned by .tymen()) – Tymist instance. Calling tymth() returns associated Tymist .tyme.
value (tock is injected initial tock) –
parameters (opts is dict of injected optional additional) –
The function comments show where the 6 equivalent contexts are performed enter, recur, clean, exit, (unforced) close, abort (forced) So context order may be: enter, recur, clean, exit enter, recur, close, exit enter, recur, abort, exit enter, abort, exit
- class hio.base.doing.ExDoer(**kwa)[source]
Bases:
DoerExDoer is example Doer for testing and demonstration Supports introspection with methods to record sends and yields
See Doer for inherited attributes, properties, and methods.
- .states is list of State namedtuples
- Type
tyme, context, feed, count
- .count is iteration count
- hio.base.doing.doifyExDo(tymth, tock=0.0, states=None, **opts)[source]
Example generator function for testing and demonstration. Example non-class based generator for use with doify wrapper. Calling this function returns generator. Wrapping this function with doify returns copy with unique attributes
- Parameters
of (tymth is injected function wrapper closure returned by .tymen()) – Tymist instance. Calling tymth() returns associated Tymist .tyme.
value (tock is injected initial tock) –
namedtuples (states is list of State) –
parameters (opts is dict of injected optional additional) –
- hio.base.doing.doizeExDo(tymth, tock=0.0, states=None, **opts)[source]
Example decorated generator function for use with doize decorator. Example non-class based generator Calling this function returns generator
- Parameters
of (tymth is injected function wrapper closure returned by .tymen()) – Tymist instance. Calling tymth() returns associated Tymist .tyme. tock is injected initial tock value states is list of State namedtuples (tyme, context, feed, count) opts is dict of injected optional additional parameters
- class hio.base.doing.TryDoer(stop=3, **kwa)[source]
Bases:
DoerTryDoer supports testing with methods to record sends and yields
- Inherited Attributes:
- .done is Boolean completion state:
True means completed Otherwise incomplete. Incompletion maybe due to close or abort.
- .states is list of State namedtuples
- Type
tyme, context, feed, count
- .count is context count
- .stop is stop count where doer completes
- Inherited Properties:
- .tyme is float relative cycle time of associated Tymist .tyme obtained
via injected .tymth function wrapper closure.
- .tymth is function wrapper closure returned by Tymist .tymeth() method.
When .tymth is called it returns associated Tymist .tyme. .tymth provides injected dependency on Tymist tyme base.
- .tock is float, desired time in seconds between runs or until next run,
non negative, zero means run asap
Properties:
- .wind injects ._tymth dependency from associated Tymist to get its .tyme
- .__call__ makes instance callable
Appears as generator function that returns generator
- .do is generator method that returns generator
- .enter is enter context action method
- .recur is recur context action method or generator method
- .exit is exit context method
- .close is close context method
- .abort is abort context method