A SelectList is simple a list in which some items are marked as selected.
The following examples show usage:
>>> l = SelectList([1, 2, 3, 4, 5])
>>> l.select(3)
SelectList([1, 2, <3>, 4, 5])
>>> l.selection
(3,)
>>> l.select(6)
Traceback (most recent call last):
...
ValueError: 6
>>> l
SelectList([1, 2, <3>, 4, 5])
>>> l.clear()
>>> l.selection
()
>>> l.indexselect(0, 2)
SelectList([<1>, 2, <3>, 4, 5])
>>> l.indexselection
(0, 2)
>>> l.select(5, 3)
SelectList([<1>, 2, <3>, 4, <5>])
>>> l.selection
(1, 3, 5)
The following rules govern the processing of the selections:
If options contains duplicates, the first matching value is selected when using select
>>> l = SelectList([1, 2, 1])
>>> l.select(1)
SelectList([<1>, 2, 1])
However, it is possible to explicitly select a value using indexselect().
>>> l = SelectList([1, 2, 1])
>>> l.indexselect(2)
SelectList([1, 2, <1>])
The selection lists are unordered.
>>> l = SelectList([1, 2, 3, 4, 5])
>>> _ = l.select(3)
>>> _ = l.select(1)
>>> l.selection
(1, 3)
If a selected value is changed in the SelectList, it is removed from the selection.
>>> l = SelectList([1, 2, 3])
>>> l.select(2)
SelectList([1, <2>, 3])
>>> l[1] = 4
>>> l
SelectList([1, 4, 3])
Read only. Return a tuple of selected indexes.
Read only. Return a tuple of the selected values.
Clear the selection.
Select indexes and return the SelectList.
>>> l = SelectList('abcdefg')
>>> l.indexselect(0, 3, 6)
SelectList([<'a'>, 'b', 'c', <'d'>, 'e', 'f', <'g'>])
Remove indexes and return the SelectList.
If the selection does not contains indexes, then an IndexError is raised.
>>> l = SelectList('penguin')
>>> l.select('p', 'g')
SelectList([<'p'>, 'e', 'n', <'g'>, 'u', 'i', 'n'])
>>> l.indexunselect(3)
SelectList([<'p'>, 'e', 'n', 'g', 'u', 'i', 'n'])
>>> l.indexunselect(4)
Traceback (most recent call last):
...
IndexError: Item at position '4' is not selected
Select values and return the SelectList.
>>> l = SelectList('abcdefg')
>>> l.select('a', 'd', 'f')
SelectList([<'a'>, 'b', 'c', <'d'>, 'e', <'f'>, 'g'])
Remove values from the selection list and return the SelectList.
If the selection contains duplicate values, then the first found is removed. If no matching values are found, then a ValueError is raised.
>>> l = SelectList('penguin')
>>> l.indexselect(2, 6)
SelectList(['p', 'e', <'n'>, 'g', 'u', 'i', <'n'>])
>>> l.unselect('n')
SelectList(['p', 'e', 'n', 'g', 'u', 'i', <'n'>])
>>> l.unselect('p')
Traceback (most recent call last):
...
ValueError: 'p' is not selected