From 227ac924eee40e5044a591f5e9989e6582cf0335 Mon Sep 17 00:00:00 2001 From: Ben Kurtovic Date: Sun, 5 Aug 2012 19:23:26 -0400 Subject: [PATCH] Finish implementing all of SmartList. --- mwparserfromhell/smart_list.py | 52 +++++++++++++++++++++++++++++------------- 1 file changed, 36 insertions(+), 16 deletions(-) diff --git a/mwparserfromhell/smart_list.py b/mwparserfromhell/smart_list.py index 5bf5261..855aaa2 100644 --- a/mwparserfromhell/smart_list.py +++ b/mwparserfromhell/smart_list.py @@ -83,26 +83,46 @@ class SmartList(list): self.extend(other) def append(self, item): - super(SmartList, self).append(item) - for child, (start, stop, step) in self._children.itervalues(): - if stop >= len(self) - 1 and stop != sys.maxint: - self._children[id(child)][1][1] += 1 - - #count + head = len(self) + self[head:head] = [item] - #index - - #extend + def extend(self, item): + head = len(self) + self[head:head] = item - #insert + def insert(self, index, item): + self[index:index] = [item] - #pop + def pop(self, index=None): + if index is None: + index = len(self) - 1 + item = self[index] + del self[index] + return item - #remove + def remove(item): + del self[self.index(item)] - #reverse + def reverse(self): + copy = list(self) + for child in self._children: + child._parent = copy + super(SmartList, self).reverse() - #sort + def sort(self, cmp=None, key=None, reverse=None): + copy = list(self) + for child in self._children: + child._parent = copy + if cmp is not None: + if key is not None: + if reverse is not None: + super(SmartList, self).sort(cmp, key, reverse) + else: + super(SmartList, self).sort(cmp, key) + else: + super(SmartList, self).sort(cmp) + else: + super(SmartList, self).sort() class _ListProxy(list): @@ -237,8 +257,8 @@ class _ListProxy(list): self._parent.insert(self._start + index, item) def pop(self, index=None): - if not index: - index = len(self) + if index is None: + index = len(self) - 1 return self._parent.pop(self._start + index) def remove(self, item):