Procházet zdrojové kódy

Implement a ton more in SmartList.

tags/v0.1
Ben Kurtovic před 12 roky
rodič
revize
13066ff189
2 změnil soubory, kde provedl 127 přidání a 25 odebrání
  1. +122
    -20
      mwparserfromhell/smart_list.py
  2. +5
    -5
      mwparserfromhell/string_mixin.py

+ 122
- 20
mwparserfromhell/smart_list.py Zobrazit soubor

@@ -32,29 +32,72 @@ class SmartList(list):
super(SmartList, self).__init__()
self._children = {}

def __getslice__(self, start, stop):
sublist = super(SmartList, self).__getslice__(start, stop)
sliceinfo = [start, stop, 1]
def __getitem__(self, key):
if not isinstance(key, slice):
return super(SmartList, self).__getitem__(key)
sliceinfo = [key.start, key.stop, 1 if not key.step else key.step]
child = _ListProxy(self, sliceinfo)
self._children[id(child)] = (child, sliceinfo)
return child

def __setitem__(self, key, item):
if not isinstance(key, slice):
return super(SmartList, self).__setitem__(key, item)
item = list(item)
super(SmartList, self).__setitem__(key, item)
diff = len(item) - key.stop + key.start
if diff:
for child, (start, stop, step) in self._children.itervalues():
if start >= key.stop:
self._children[id(child)][1][0] += diff
if stop >= key.stop and stop != sys.maxint:
self._children[id(child)][1][1] += diff

__delitem__

def __getslice__(self, start, stop):
return self.__getitem__(slice(start, stop))

def __setslice__(self, start, stop, iterable):
obj = list(iterable)
super(SmartList, self).__setslice__(start, stop, obj)
diff = len(obj) - stop + start
for child, (ch_start, ch_stop, step) in self._children.itervalues():
if ch_start >= stop:
self._children[id(child)][1][0] += diff
if ch_stop >= stop and ch_stop != sys.maxint:
self._children[id(child)][1][1] += diff
self.__setitem__(slice(start, stop), iterable)

def append(self, obj):
super(SmartList, self).append(obj)
def __delslice__(self, start, stop):
self.__delitem__(slice(start, stop))

__add__

__radd__

__iadd__

__mul__

__rmul__

__imul__

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

index

extend

insert

pop

remove

reverse

sort


class _ListProxy(list):
def __init__(self, parent, sliceinfo):
@@ -65,26 +108,71 @@ class _ListProxy(list):
def __repr__(self):
return repr(self._render())

__lt__

__le__

__eq__

__ne__

__gt__

__ge__

__nonzero__

def __len__(self):
return (self._stop - self._start) / self._step

def __getitem__(self, key):
return self._render()[key]

def __setitem__(self, key, item):
if isinstance(key, slice):
adjusted = slice(key.start + self._start, key.stop + self._stop,
key.step)
self._parent[adjusted] = item
else:
self._parent[self._start + index] = item

__delitem__

def __iter__(self):
i = self._start
while i < self._stop:
yield self._parent[i]
i += self._step

def __getitem__(self, index):
return self._render()[index]
def __reversed__(self):
i = self._stop - 1
while i >= self._start:
yield self._parent[i]
i -= self._step

def __getslice__(self, start, stop):
return self._render()[start:stop]
def __contains__(self, item):
return item in self._render()

def __setitem__(self, index, obj):
self._parent[self._start + index] = obj
def __getslice__(self, start, stop):
return self.__getitem__(slice(start, stop))

def __setslice__(self, start, stop, iterable):
self._parent[self._start + start:self._start + stop] = iterable
self.__setitem__(slice(start, stop), iterable)

def __delslice__(self, start, stop):
self.__delitem__(slice(start, stop))

__add__

__radd__

__iadd__

__mul__

__rmul__

__imul__

@property
def _start(self):
@@ -104,5 +192,19 @@ class _ListProxy(list):
def append(self, obj):
self._parent.insert(self._stop, obj)

count

index

extend

def insert(self, index, obj):
self._parent.insert(self._start + index, obj)

pop

remove

reverse

sort

+ 5
- 5
mwparserfromhell/string_mixin.py Zobrazit soubor

@@ -34,9 +34,6 @@ class StringMixIn(object):
def __repr__(self):
return repr(unicode(self))

def __unicode__(self):
raise NotImplementedError()

def __lt__(self, other):
if isinstance(other, StringMixIn):
return unicode(self) < unicode(other)
@@ -70,6 +67,9 @@ class StringMixIn(object):
def __nonzero__(self):
return bool(unicode(self))

def __unicode__(self):
raise NotImplementedError()

def __len__(self):
return len(unicode(self))

@@ -77,8 +77,8 @@ class StringMixIn(object):
for char in unicode(self):
yield char

def __getitem__(self, index):
return unicode(self)[index]
def __getitem__(self, key):
return unicode(self)[key]

def __contains__(self, item):
if isinstance(item, StringMixIn):


Načítá se…
Zrušit
Uložit