smart_list Package¶
smart_list
Package¶
This module contains the SmartList
type, as well as its
ListProxy
child, which together implement a list whose sublists
reflect changes made to the main list, and vice-versa.
list_proxy
Module¶
- class mwparserfromhell.smart_list.list_proxy.ListProxy(parent, sliceinfo)[source]¶
Implement the
list
interface by getting elements from a parent.This is created by a
SmartList
object when slicing. It does not actually store the list at any time; instead, whenever the list is needed, it builds it dynamically using the_render()
method.- index(item, start=None, stop=None)[source]¶
Return first index of value.
Raises ValueError if the value is not present.
- pop(index=None)[source]¶
Remove and return item at index (default last).
Raises IndexError if list is empty or index is out of range.
- remove(item)[source]¶
Remove first occurrence of value.
Raises ValueError if the value is not present.
- sort(key=None, reverse=None)[source]¶
Sort the list in ascending order and return None.
The sort is in-place (i.e. the list itself is modified) and stable (i.e. the order of two equal elements is maintained).
If a key function is given, apply it once to each list item and sort them, ascending or descending, according to their function values.
The reverse flag can be set to sort in descending order.
smart_list
Module¶
- class mwparserfromhell.smart_list.smart_list.SmartList(*args, **kwargs)[source]¶
Implements the
list
interface with special handling of sublists.When a sublist is created (by
list[i:j]
), any changes made to this list (such as the addition, removal, or replacement of elements) will be reflected in the sublist, or vice-versa, to the greatest degree possible. This is implemented by having sublists - instances of theListProxy
type - dynamically determine their elements by storing their slice info and retrieving that slice from the parent. Methods that change the size of the list also change the slice info. For example:>>> parent = SmartList([0, 1, 2, 3]) >>> parent [0, 1, 2, 3] >>> child = parent[2:] >>> child [2, 3] >>> child.append(4) >>> child [2, 3, 4] >>> parent [0, 1, 2, 3, 4]
- pop(index=None)[source]¶
Remove and return item at index (default last).
Raises IndexError if list is empty or index is out of range.
- remove(item)[source]¶
Remove first occurrence of value.
Raises ValueError if the value is not present.
- sort(key=None, reverse=None)[source]¶
Sort the list in ascending order and return None.
The sort is in-place (i.e. the list itself is modified) and stable (i.e. the order of two equal elements is maintained).
If a key function is given, apply it once to each list item and sort them, ascending or descending, according to their function values.
The reverse flag can be set to sort in descending order.