mosaik.util — Utility classes and functions

This module contains some utility functions and classes.

mosaik.util.sync_process(generator, world, *, errback=None, ignore_errors=False)

Synchronously execute a SimPy process defined by the generator object generator.

A world instance is required to run the event loop.

You can optionally provide a errback (error callback) which will be called with no arguments if an error occurs.

If ignore_errors is set to True, no errors will be printed.

mosaik.util.sync_call(sim, funcname, args, kwargs)

Start a SimPy process to make the func() call to a simulator behave like it was synchronous.

Return the result of the func() call.

Raise an SimulationError if an exception occurs.

mosaik.util.connect_many_to_one(world, src_set, dest, *attrs, async_requests=False)

connect() each entity in src_set to dest.

See the connect() for more details.

mosaik.util.connect_randomly(world, src_set, dest_set, *attrs, evenly=True, max_connects=inf)

Randomly connect() the entities from src_set to the entities from dest_set and return a subset of dest_set containing all entities with a connection.

world is an instance of the World to which the entities belong.

src_set and dest_set are iterables containing Entity instances. src_set may be empty, dest_set must not be empty. Each entity of src_set will be connected to an entity of dest_set, but not every entity of dest_set will necessarily have a connection (e.g., if you connect a set of three entities to a set of four entities). A set of all entities from dest_set, to which at least one entity from src_set was connected, will be returned.

attrs is a list of attribute names of pairs as in connect().

If the flag evenly is set to True, entities connections will be distributed as evenly as possible. That means if you connect a set of three entities to a set of three entities, there will be three 1:1 connections; if you connect four entities to three entities, there will be one 2:1 and two 1:1 connections. If evenly is set to False, connections will be truly random. That means if you connect three entities to three entities, you may either have three 1:1 connections, one 2:1 and two 1:1 connections or just one 3:1 connection.

max_connects lets you set the maximum number of connections that an entity of dest_set may receive. This argument is only taken into account if evenly is set to False.