duck.utils.eventlist¶
EventList - A list subclass with event-driven callbacks on item insertion and deletion.
Module Contents¶
Classes¶
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:
listA 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
delkeyword.Supports both single index and slice deletion.
- __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
- 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.