communication.buffer module

This module contains a Buffer class for storing incoming or expected messages.

class communication.buffer.Buffer[source]

Bases: Generic[T]

A class to store messages in buffers for clients. It stores elements of a type T.

__init__()[source]

Initialise an empty buffer.

add_client(client_name)[source]

Add a new buffer for a client with name client_name.

Parameters:

client_name (str) – The name of the client to add.

Raises:

ValueError – When a buffer already exists for the client.

Return type:

None

empty(client_name=None)[source]

Clear the buffer for the given client. If no client is specified, clear all buffers. If the client does not have a buffer, a new buffer will be created.

Parameters:

client_name (str | None) – If specified, clear the buffer for this client. If not specified, clear all buffers.

Return type:

None

get_client(client_name)[source]

Return the buffer for the given client.

Parameters:

client_name (str) – The name of the client for which to return the buffer.

Raises:

KeyError – If the specified client does not have a buffer.

Return type:

dict[str, TypeVar(T)]

Returns:

Either the specified buffer or all the buffers.

has_buffer(client_name)[source]

Whether a buffer exists for the given client.

Parameters:

client_name (str) – The name of the client to check for.

Return type:

bool

Returns:

Whether a buffer exists for the given client.

has_message(client_name, msg_id=None)[source]

Whether a message with given id is present in the buffer for the given client. If no message id is specified, it returns whether any message is in the buffer for the given client.

Parameters:
  • client_name (str) – The client to check for.

  • msg_id (str | None) – The message id to check the existence for. If None, it will check whether any message is available for the given client.

Return type:

bool

Returns:

Boolean indicating whether the given / any message is available for the client.

pop(client_name, msg_id)[source]

Pop a message with given id from the client’s buffer.

Parameters:
  • client_name (str) – The client for which the message must be popped.

  • msg_id (str) – The identifier for the message.

Raises:
  • KeyError – If no buffer exists for the given client.

  • KeyError – If the message with given ID does not exist.

Return type:

TypeVar(T)

Returns:

The content of the message.

push(client_name, msg_id, content)[source]

Add a message for a client to the buffer.

Parameters:
  • client_name (str) – the client under which to store the message.

  • msg_id (str) – The message identifier.

  • content (TypeVar(T)) – The content of the message.

Raises:
  • KeyError – If no buffer exists for the given client.

  • AttributeError – If a message with msg_id already exists in the buffer.

Return type:

None