cohydra.context¶
Contexts to tear down the simulation.
Functions
Defer a function call. |
Classes
A context can be used for deferring function calls. |
|
A DeferredItem is used for storing functions calls that need to be executed later on. |
|
The NoContext is a Null-Object and therefore does nothing. |
|
The simple context executes deferred items like it is intented. |
|
-
class
cohydra.context.
Context
[source]¶ Bases:
object
A context can be used for deferring function calls.
In this project, it is used for deferring teardowns of the simulation.
You can use it like this:
with SimpleContext() as ctx: defer('Call afunction', afunction, args) defer('Call another function', anotherfunction, args) ctx.cleanup()
-
fails
= None¶ The number of failed cleanups.
-
defer
(item)[source]¶ Store a
DeferredItem
for running it later.- Parameters
item (
DeferredItem
) – The function to execute later.
-
cancel
(item)[source]¶ Cancel a specific item of the current context.
- Parameters
item (
DeferredItem
) – The function to cancel.
-
-
class
cohydra.context.
DeferredItem
(ctx: cohydra.context.Context, name: str, func: callable, args, kwargs)[source]¶ Bases:
object
A DeferredItem is used for storing functions calls that need to be executed later on.
- Parameters
ctx – The context the item belongs to.
name – A name for this item (for logging purposes).
func – The function to defer.
args (list) – The positional arguments to be passed to the function.
kwargs (dict) – The keyword arguments to be passed to the function.
-
ctx
= None¶ The context to execute this item in.
-
name
= None¶ The name of the item (and description).
-
func
= None¶ The callable.
-
args
= None¶ (Positional) Arguments to be passed to the callable.
-
kwargs
= None¶ Keyword arguments to be passed to the callable.
-
cohydra.context.
defer
(name, func, *args, **kwargs)[source]¶ Defer a function call.
The function call be assigned to the current context.
- Parameters
func (callable) – The function to execute.
-
class
cohydra.context.
NoContext
[source]¶ Bases:
cohydra.context.Context
The NoContext is a Null-Object and therefore does nothing.
It does not do any cleanups. This is useful if you do not want your simulated containers to be torn down.
-
defer
(item)[source]¶ Store a
DeferredItem
for running it later.- Parameters
item (
DeferredItem
) – The function to execute later.
-
cancel
(item)[source]¶ Cancel a specific item of the current context.
- Parameters
item (
DeferredItem
) – The function to cancel.
-
add_error
(err: Exception)¶ Add an error (to be implemented).
-
static
current
()¶ Return the current context.
-
-
class
cohydra.context.
SimpleContext
[source]¶ Bases:
cohydra.context.Context
The simple context executes deferred items like it is intented.
-
defer
(item)[source]¶ Store a
DeferredItem
for running it later.- Parameters
item (
DeferredItem
) – The function to execute later.
-
cancel
(item)[source]¶ Cancel a specific item of the current context.
- Parameters
item (
DeferredItem
) – The function to cancel.
-
add_error
(err: Exception)¶ Add an error (to be implemented).
-
static
current
()¶ Return the current context.
-
Inheritance Diagramm