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.
- class mwparserfromhell.smart_list.SmartList(*args, **kwargs)[source]
Implements the
listinterface 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 theListProxytype - 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]
- append(item)[source]
Append object to the end of the list.
- extend(item)[source]
Extend list by appending elements from the iterable.
- insert(index, item)[source]
Insert object before index.
- 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.
- reverse()[source]
Reverse IN PLACE.
- 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.
list_proxy Module¶
- class mwparserfromhell.smart_list.list_proxy.ListProxy(parent, sliceinfo)[source]
Implement the
listinterface by getting elements from a parent.This is created by a
SmartListobject 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.- append(item)[source]
Append object to the end of the list.
- count(item)[source]
Return number of occurrences of value.
- extend(item)[source]
Extend list by appending elements from the iterable.
- index(item, start=None, stop=None)[source]
Return first index of value.
Raises ValueError if the value is not present.
- insert(index, item)[source]
Insert object before index.
- 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.
- reverse()[source]
Reverse IN PLACE.
- 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.