eta.util.buffer

Utilities for manipulating priority queues, termed “buffers” in Eta’s architecture.

NOTE: although importance values are positive, the values stored in the underlying priority queues are negated, since heapq implements a min heap.

Functions

clear

Empty a buffer.

enqueue

Enqueue an item (either an object or an (importance, object) tuple) in a buffer.

enqueue_ordered

Enqueue a list of items, preserving original order unless an importance value is explicitly specified.

get_item

Get the top item from a buffer without popping it.

is_empty

Check whether a buffer is empty.

iterate

Return a buffer as a list (optionally applying some function to each value).

max_importance

Get the maximum importance value from a buffer.

pop_all

Pop all items from a buffer.

pop_item

Pop the top item from a buffer.

enqueue(item, buffer)[source]

Enqueue an item (either an object or an (importance, object) tuple) in a buffer.

Parameters:
  • item (object or tuple[float, object]) – If item is an object, enqueue using a default importance value. Otherwise, item is an (importance, object) tuple, and the object is enqueued with the given importance.

  • buffer (list[tuple[float, object]]) –

enqueue_ordered(items, buffer, inc_val=0.0001)[source]

Enqueue a list of items, preserving original order unless an importance value is explicitly specified.

Parameters:
  • items (list) – A list where each item is either an object or (importance, object) tuple. For each item where an importance is not specified, a default importance value is generated using an increment so as to preserve the original order.

  • buffer (list[tuple[float, object]]) –

  • inc_val (float, default=0.0001) – The increment to use for generated importance values.

is_empty(buffer)[source]

Check whether a buffer is empty.

Parameters:

buffer (list[tuple[float, object]]) –

Return type:

bool

pop_item(buffer, importance=False)[source]

Pop the top item from a buffer.

Parameters:
  • buffer (list[tuple[float, object]]) –

  • importance (bool, default=False) – If True is given, return the importance as well as the object.

Return type:

object or tuple[float, object]

pop_all(buffer, importance=False)[source]

Pop all items from a buffer.

Parameters:
  • buffer (list[tuple[float, object]]) –

  • importance (bool, default=False) – If True is given, return the importances as well as the objects.

Return type:

list[object] or list[tuple[float, object]]

get_item(buffer, importance=False)[source]

Get the top item from a buffer without popping it.

Parameters:
  • buffer (list[tuple[float, object]]) –

  • importance (bool, default=False) – If True is given, return the importance as well as the object.

Return type:

object or tuple[float, object]

max_importance(buffer)[source]

Get the maximum importance value from a buffer.

Parameters:

buffer (list[tuple[float, object]]) –

Return type:

float

clear(buffer)[source]

Empty a buffer.

Parameters:

buffer (list[tuple[float, object]]) –

iterate(buffer, func=None)[source]

Return a buffer as a list (optionally applying some function to each value).

Parameters:
  • buffer (list[tuple[float, object]]) –

  • func (function, optional) – A function to apply to each value in the buffer.

Return type:

list[object]