m4us.core.components — Message For You Sir! v0.1.1 documentation

### Navigation

- index
- modules |
- next |
- previous |
- Message For You Sir! v0.1.1 documentation »
- API Documentation »

# `m4us.core.components`

Provides various kinds of Component classes.

Components are coroutines with context. That is, they are classes that implement `ICoroutine`, but can also store and access instance attributes.

## Inheritance Diagram

## Members

class `Component`(**kwargs)

  Bases: `object`

  Base class for components.

  Components act like coroutines but can store and retrieve context via instance attributes. This corresponds in concept to Kamaelia's `Component` class.

  Components have an internal coroutine defined in the `_main()` method, which sub-classes should implement.

  Parameters: | kwargs (`dict`) -- Any keyword arguments given at instantiation are automatically set as instance attributes for convenience.
  Implements : | `ICoroutine`
  Provides : | `ICoroutineFactory`

  `_main`()

    The component's coroutine.

    Sub-classes are expected to override this method. It should be a coroutine which will automatically be activated upon instantiation.

    This coroutine should work just like regular coroutines, but has access to `self` and any context that the instance contains.

  `close`()

    Terminate the component.

    This method just calls `close()` on the component's internal coroutine defined in `_main()`.

    See also

    The `ICoroutine` interface for details about this method.

  `send`(message)

    Send the message to the component.

    This method just passes the message on to the internal coroutine defined in `_main()`, returning any resulting message.

    See also

    The `ICoroutine` interface for details about this method.

  `throw`(exception)

    Send the exception to the component.

    This method just passes the exception on to the internal coroutine defined in `_main()`, returning any resulting message or raising the uncaught exception.

    See also

    The `ICoroutine` interface for details about this method.

class `SampleComponent`(**kwargs)

  Bases: `m4us.core.components.Component`

  Component that passes all messages through.

  This component is meant to provide a canonical example of what a component used with this project looks like.

  Like `sample_coroutine()`, any messages sent to it on any inbox will be sent back out on it's `outbox` outbox. It is also well behaved in that it will shutdown on any `IShutdown` message, forwarding it on before quitting.

  The full code for this component is:

       1
       2
       3
       4
       5
       6
       7
       8
       9
      10
      11
      12
      13
      14
      15
      class SampleComponent(Component):

          """Component that passes all messages through."""

          classProvides(ICoroutineFactory)

          def _main(self):
              """Pass all messages through."""
              inbox, message = (yield)
              while True:
                  if is_shutdown(inbox, message):
                      yield 'signal', message
                      break
                  ## Your code goes here.
                  inbox, message = (yield 'outbox', message)

  Implements : | `ICoroutine`
  Provides : | `ICoroutineFactory`

  See also

  The `sample_coroutine()` coroutine for details on why the `_main()` method is written the way it is.

### Navigation

- index
- modules |
- next |
- previous |
- Message For You Sir! v0.1.1 documentation »
- API Documentation »