Evented Containers#
These classes provide "evented" versions of mutable python containers. They each have an events
attribute (SignalGroup
) that has a variety of signals that will emit whenever the container is mutated. See Container SignalGroups for the corresponding container type for details on the available signals.
psygnal.containers.EventedDict
#
Bases: TypedMutableMapping[_K, _V]
Mutable mapping that emits events when altered.
This class is designed to behave exactly like the builtin dict
, but will emit events before and after all mutations (addition, removal, and changing).
Parameters:
- data (
Union[Mapping[_K, _V], Iterable[Tuple[_K, _V]], None], optional
) –Data suitable of passing to dict(). Mapping of {key: value} pairs, or Iterable of two-tuples [(key, value), ...], or None to create an
- basetype (
TypeOrSequenceOfTypes, optional
) –Type or Sequence of Type objects. If provided, values entered into this Mapping must be an instance of one of the provided types. by default ().
Attributes:
- events (
DictEvents
) –The
SignalGroup
object that emits all events available on anEventedDict
.
Source code in psygnal/containers/_evented_dict.py
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
|
psygnal.containers.EventedList
#
Bases: MutableSequence[_T]
Mutable Sequence that emits events when altered.
This class is designed to behave exactly like the builtin list
, but will emit events before and after all mutations (insertion, removal, setting, and moving).
Parameters:
- data (
iterable, optional
) –Elements to initialize the list with.
- hashable (
bool
) –Whether the list should be hashable as id(self). By default
True
. - child_events (
bool
) –Whether to re-emit events from emitted from evented items in the list (i.e. items that have SignalInstances). If
True
, child events can be connected atEventedList.events.child_event
. By default,False
.
Attributes:
- events (
ListEvents
) –SignalGroup that with events related to list mutation. (see ListEvents)
Source code in psygnal/containers/_evented_list.py
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 |
|
copy()
#
Return a shallow copy of the list.
Source code in psygnal/containers/_evented_list.py
217 218 219 |
|
insert(index, value)
#
Insert value
before index.
Source code in psygnal/containers/_evented_list.py
137 138 139 140 141 142 143 |
|
move(src_index, dest_index=0)
#
Insert object at src_index
before dest_index
.
Both indices refer to the list prior to any object removal (pre-move space).
Source code in psygnal/containers/_evented_list.py
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 |
|
move_multiple(sources, dest_index=0)
#
Move a batch of sources
indices, to a single destination.
Note, if dest_index
is higher than any of the sources
, then the resulting position of the moved objects after the move operation is complete will be lower than dest_index
.
Parameters:
- sources (
Iterable[Union[int, slice]]
) –A sequence of indices
- dest_index (
int, optional
) –The destination index. All sources will be inserted before this index (in pre-move space), by default 0... which has the effect of "bringing to front" everything in
sources
, or acting as a "reorder" method ifsources
contains all indices.
Returns:
-
int
–The number of successful move operations completed.
Raises:
-
TypeError
–If the destination index is a slice, or any of the source indices are not
int
orslice
.
Source code in psygnal/containers/_evented_list.py
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 |
|
reverse(*, emit_individual_events=False)
#
Reverse list IN PLACE.
Source code in psygnal/containers/_evented_list.py
263 264 265 266 267 268 269 |
|
psygnal.containers.EventedSet
#
Bases: _BaseMutableSet[_T]
A set with an items_changed
signal that emits when items are added/removed.
Parameters:
- iterable (
Iterable[_T]
) –Data to populate the set. If omitted, an empty set is created.
Attributes:
- events (
SetEvents
) –SignalGroup that with events related to set mutation. (see SetEvents)
Examples:
>>> from psygnal.containers import EventedSet
>>>
>>> my_set = EventedSet([1, 2, 3])
>>> my_set.events.items_changed.connect(
>>> lambda a, r: print(f"added={a}, removed={r}")
>>> )
>>> my_set.update({3, 4, 5})
added=(4, 5), removed=()
Multi-item events will be reduced into a single emission:
>>> my_set.symmetric_difference_update({4, 5, 6, 7})
added=(6, 7), removed=(4, 5)
>>> my_set
EventedSet({1, 2, 3, 6, 7})
Source code in psygnal/containers/_evented_set.py
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 |
|
clear()
#
Remove all elements from this set.
Source code in psygnal/containers/_evented_set.py
278 279 280 281 |
|
difference_update(*s)
#
Remove all elements of another set from this set.
Source code in psygnal/containers/_evented_set.py
283 284 285 286 |
|
intersection_update(*s)
#
Update this set with the intersection of itself and another.
Source code in psygnal/containers/_evented_set.py
288 289 290 291 |
|
symmetric_difference_update(__s)
#
Update this set with the symmetric difference of itself and another.
This will remove any items in this set that are also in other
, and add any items in others that are not present in this set.
Source code in psygnal/containers/_evented_set.py
293 294 295 296 297 298 299 300 |
|
update(*others)
#
Update this set with the union of this set and others.
Source code in psygnal/containers/_evented_set.py
273 274 275 276 |
|
psygnal.containers.EventedOrderedSet
#
Bases: EventedSet
, OrderedSet[_T]
A ordered variant of EventedSet that maintains insertion order.
Parameters:
- iterable (
Iterable[_T]
) –Data to populate the set. If omitted, an empty set is created.
Attributes:
- events (
SetEvents
) –SignalGroup that with events related to set mutation. (see SetEvents)
Source code in psygnal/containers/_evented_set.py
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 |
|
psygnal.containers.Selection
#
Bases: EventedOrderedSet[_T]
An model of selected items, with a active
and current
item.
There can only be one active
and one current
item, but there can be multiple selected items. An "active" item is defined as a single selected item (if multiple items are selected, there is no active item). The "current" item is mostly useful for (e.g.) keyboard actions: even with multiple items selected, you may only have one current item, and keyboard events (like up and down) can modify that current item. It's possible to have a current item without an active item, but an active item will always be the current item.
An item can be the current item and selected at the same time. Qt views will ensure that there is always a current item as keyboard navigation, for example, requires a current item. This pattern mimics current/selected items from Qt: https://doc.qt.io/qt-5/model-view-programming.html#current-item-and-selected-items
Parameters:
- data (
iterable, optional
) –Elements to initialize the set with.
- parent (
Container, optional
) –The parent container, if any. This is used to provide validation upon mutation in common use cases.
Attributes:
- events (
SelectionEvents
) –SignalGroup that with events related to selection changes. (see SelectionEvents)
- active (
Any, optional
) –The active item, if any. An "active" item is defined as a single selected item (if multiple items are selected, there is no active item)
- _current (
Any, optional
) –The current item, if any. This is used primarily by GUI views when handling mouse/key events.
Source code in psygnal/containers/_selection.py
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
|
active: _T | None
property
writable
#
Return the currently active item or None.
clear(keep_current=False)
#
Clear the selection.
Parameters:
- keep_current (
bool
) –If
False
(the default), the "current" item will also be set to None.
Source code in psygnal/containers/_selection.py
116 117 118 119 120 121 122 123 124 125 126 |
|
select_only(obj)
#
Unselect everything but obj
. Add to selection if not currently selected.
Source code in psygnal/containers/_selection.py
132 133 134 135 |
|
toggle(obj)
#
Toggle selection state of obj.
Source code in psygnal/containers/_selection.py
128 129 130 |
|
psygnal.containers.SelectableEventedList
#
Bases: Selectable[_T]
, EventedList[_T]
EventedList
subclass with a built in selection model.
In addition to all EventedList
properties, this class also has a selection
attribute that manages a set of selected items in the list.
Parameters:
- data (
iterable, optional
) –Elements to initialize the list with.
- hashable (
bool
) –Whether the list should be hashable as id(self). By default
True
. - child_events (
bool
) –Whether to re-emit events from emitted from evented items in the list (i.e. items that have SignalInstances). If
True
, child events can be connected atEventedList.events.child_event
. By default,False
.
Attributes:
- events (
ListEvents
) –SignalGroup that with events related to list mutation. (see ListEvents)
- selection (
Selection
) –An evented set containing the currently selected items, along with an
active
andcurrent
item. (SeeSelection
)
Source code in psygnal/containers/_selectable_evented_list.py
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
|
deselect_all()
#
Deselect all items in the list.
Source code in psygnal/containers/_selectable_evented_list.py
64 65 66 |
|
insert(index, value)
#
Insert item(s) into the list and update the selection.
Source code in psygnal/containers/_selectable_evented_list.py
54 55 56 57 58 |
|
remove_selected()
#
Remove selected items from the list and the selection.
Returns:
-
Tuple[_T, ...]
–The items that were removed.
Source code in psygnal/containers/_selectable_evented_list.py
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
|
select_all()
#
Select all items in the list.
Source code in psygnal/containers/_selectable_evented_list.py
60 61 62 |
|
select_next(step=1, expand_selection=False, wraparound=False)
#
Select the next item in the list.
Parameters:
- step (
int
) –The step size to take when picking the next item, by default 1
- expand_selection (
bool
) –If True, will expand the selection to contain the both the current item and the next item, by default False
- wraparound (
bool
) –Whether to return to the beginning of the list of the end has been reached, by default False
Source code in psygnal/containers/_selectable_evented_list.py
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
|
select_previous(expand_selection=False, wraparound=False)
#
Select the previous item in the list.
Source code in psygnal/containers/_selectable_evented_list.py
102 103 104 105 106 107 108 |
|
Container SignalGroups#
psygnal.containers._evented_dict.DictEvents
#
Bases: SignalGroup
Events available on EventedDict.
Attributes:
- adding (
Signal[Any]
) –(key,)
emitted before an item is added atkey
- added (
Signal[Any, Any]
) –(key, value)
emitted after avalue
is added atkey
- changing (
Signal[Any, Any, Any]
) –(key, old_value, new_value)
emitted beforeold_value
is replaced withnew_value
atkey
- changed (
Signal[Any, Any, Any]
) –(key, old_value, new_value)
emitted beforeold_value
is replaced withnew_value
atkey
- removing (
Signal[Any]
) –(key,)
emitted before an item is removed atkey
- removed (
Signal[Any, Any]
) –(key, value)
emitted aftervalue
is removed atindex
psygnal.containers._evented_list.ListEvents
#
Bases: SignalGroup
Events available on EventedList.
Attributes:
- inserting (
Signal[int]
) –(index)
emitted before an item is inserted atindex
- inserted (
Signal[int, Any]
) –(index, value)
emitted aftervalue
is inserted atindex
- removing (
Signal[int]
) –(index)
emitted before an item is removed atindex
- removed (
Signal[int, Any]
) –(index, value)
emitted aftervalue
is removed atindex
- moving (
Signal[int, int]
) –(index, new_index)
emitted before an item is moved fromindex
tonew_index
- moved (
Signal[int, int, Any]
) –(index, new_index, value)
emitted aftervalue
is moved fromindex
tonew_index
- changed (
Signal[Union[int, slice], Any, Any]
) –(index_or_slice, old_value, value)
emitted whenindex
is set fromold_value
tovalue
- reordered (
Signal
) –emitted when the list is reordered (eg. moved/reversed).
- child_event (
Signal[int, Any, SignalInstance, tuple]
) –(index, object, emitter, args)
emitted when an object in the list emits an event. Note that theEventedList
must be created withchild_events=True
in order for this to be emitted.
psygnal.containers._evented_set.SetEvents
#
Bases: SignalGroup
Events available on EventedSet.
Attributes:
- items_changed (added (
Tuple[Any, ...], removed: Tuple[Any, ...])
) –A signal that will emitted whenever an item or items are added or removed. Connected callbacks will be called with
callback(added, removed)
, whereadded
andremoved
are tuples containing the objects that have been added or removed from the set.
psygnal.containers._selection.SelectionEvents
#
Bases: SetEvents
Events available on Selection.
Attributes:
- items_changed (added (
Tuple[_T], removed: Tuple[_T])
) –A signal that will emitted whenever an item or items are added or removed. Connected callbacks will be called with
callback(added, removed)
, whereadded
andremoved
are tuples containing the objects that have been added or removed from the set. - active (value (
_T)
) –Emitted when the active item has changed. An active item is a single selected item.
- _current (value (
_T)
) –Emitted when the current item has changed. (Private event)