Ver a proveniência

Implement SmartList.setslice, ListProxy.{repr,setitem,setslice}

tags/v0.1
Ben Kurtovic há 12 anos
ascendente
cometimento
713a3c9944
1 ficheiros alterados com 18 adições e 1 eliminações
  1. +18
    -1
      mwparserfromhell/smart_list.py

+ 18
- 1
mwparserfromhell/smart_list.py Ver ficheiro

@@ -39,7 +39,15 @@ class SmartList(list):
self._children[id(child)] = (child, sliceinfo)
return child

# def __setslice__(self, 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

def append(self, obj):
super(SmartList, self).append(obj)
@@ -54,6 +62,9 @@ class _ListProxy(list):
self._parent = parent
self._sliceinfo = sliceinfo

def __repr__(self):
return repr(self._render())

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

@@ -69,6 +80,12 @@ class _ListProxy(list):
def __getslice__(self, start, stop):
return self._render()[start:stop]

def __setitem__(self, index, obj):
self._parent[self._start + index] = obj

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

@property
def _start(self):
return self._sliceinfo[0]


Carregando…
Cancelar
Guardar