cohydra.context

Contexts to tear down the simulation.

Functions

defer

Defer a function call.

Classes

Context

A context can be used for deferring function calls.

DeferredItem

A DeferredItem is used for storing functions calls that need to be executed later on.

NoContext

The NoContext is a Null-Object and therefore does nothing.

SimpleContext

The simple context executes deferred items like it is intented.

ThreadLocalStack


class cohydra.context.ThreadLocalStack[source]

Bases: object

property stack
push(item)[source]
pop()[source]
top()[source]
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()
static current()[source]

Return the current context.

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.

cleanup()[source]

Do whatever is needed to cleanup the context.

add_error(err: Exception)[source]

Add an error (to be implemented).

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.

cancel()[source]

Cancel the execution of the item.

cleanup()[source]

Execute the function call.

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.

cleanup()[source]

Do whatever is needed to cleanup the context.

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.

cleanup()[source]

Do whatever is needed to cleanup the context.

add_error(err: Exception)

Add an error (to be implemented).

static current()

Return the current context.

Inheritance Diagramm

Inheritance diagram of cohydra.context