duck.utils.eventlist

EventList - A list subclass with event-driven callbacks on item insertion and deletion.

Module Contents

Classes

EventList

A list subclass that supports event hooks for item insertion and deletion.

API

class duck.utils.eventlist.EventList(initlist=None, on_new_item: Optional[Callable[[Any], None]] = None, on_delete_item: Optional[Callable[[Any], None]] = None, skip_initlist_events: bool = False)[source]

Bases: list

A list subclass that supports event hooks for item insertion and deletion.

This class allows you to register callback functions that will be triggered before any item is inserted or deleted (including through methods like append, extend, pop, remove, clear, slice assignment, etc.).

Initialization

Initialize the EventList.

Parameters:
  • initlist – Initial values to populate the list.

  • on_new_item – Callback triggered before adding an item.

  • on_delete_item – Callback triggered before removing an item.

  • skip_initlist_events – Whether to skip calling event handler for each item in initlist.

__delitem__(index)[source]

Handle deletion using del keyword.

Supports both single index and slice deletion.

__iadd__(other)[source]

In-place addition (+=), treated as an extend operation.

__repr__()[source]
__setitem__(index, value)[source]

Set the item at the given index or slice, triggering appropriate events.

For single index assignment, calls on_delete_item (old) and on_new_item (new). For slice assignment, replaces old items individually and adds new ones.

__slots__

None

__str__()[source]
append(item: Any)[source]

Append an item to the end of the list and trigger the on_new_item event.

clear()[source]

Clear the list by removing all items individually (triggering events).

extend(other)[source]

Extend the list with items from another iterable, triggering events for each item.

insert(index: int, item: Any)[source]

Insert an item at a specific index and trigger the on_new_item event.

on_delete_item(item: Any)[source]

Triggered before an item is removed or replaced in the list.

Parameters:

item – The item that is about to be deleted.

on_new_item(item: Any)[source]

Triggered before an item is inserted into the list.

Parameters:

item – The item that is about to be added.

pop(index: int = -1)[source]

Remove and return item at index, triggering on_delete_item event.

remove(item: Any)[source]

Remove the first occurrence of an item, triggering on_delete_item event.