From d881f435ac7443db32d30c9eecb871ac16a0369c Mon Sep 17 00:00:00 2001 From: Mahmoud Hashemi Date: Wed, 20 Mar 2013 20:39:48 -0700 Subject: [PATCH] cheesy __setitem__() for blist --- boltons/listutils.py | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/boltons/listutils.py b/boltons/listutils.py index 0a0a63f..32bfa75 100644 --- a/boltons/listutils.py +++ b/boltons/listutils.py @@ -12,7 +12,7 @@ except ImportError: _MISSING = object() -# TODO: __delitem__, __setitem__, index +# TODO: index # TODO: sorted version # TODO: inherit from list @@ -90,9 +90,6 @@ class BarrelList(object): self._balance_list(list_idx) return ret - def count(self, item): - return sum([cur.count(item) for cur in self.lists]) - def iter_slice(self, start, stop, step=None): iterable = self # TODO: optimization opportunities abound # start_list_idx, stop_list_idx = 0, len(self.lists) @@ -117,7 +114,7 @@ class BarrelList(object): new_list = chain(self.iter_slice(0, start, step), self.iter_slice(stop, None, step)) self.lists[0][:] = new_list - self._rebalance_list(0) + self._balance_list(0) return if start is None: start = 0 @@ -185,28 +182,41 @@ class BarrelList(object): raise IndexError() del [list_idx][rel_idx] - def __setitem__(self, index, item): try: start, stop, step = index.start, index.stop, index.step except AttributeError: index = operator.index(index) else: - iter_slice = self.iter_slice(start, stop, step) - return self.from_iterable(iter_slice) + if len(self.lists) == 1: + self.lists[0][index] = item + else: + tmp = list(self) + tmp[index] = item + self.lists[0] = tmp + self._balance_list(0) + return + list_idx, rel_idx = self._translate_index(index) + if list_idx is None: + raise IndexError() + self.lists[list_idx][rel_idx] = item def __repr__(self): return '%s(%r)' % (self.__class__.__name__, list(self)) def sort(self): # poor pythonist's mergesort, it's faster than sorted(self) - # when the lists average larger than 512 items + # when the lists' average length is greater than 512. if len(self.lists) == 1: self.lists[0].sort() else: self.lists[0] = sorted(chain(*[sorted(l) for l in self.lists])) self._balance_list(0) + def count(self, item): + return sum([cur.count(item) for cur in self.lists]) + + class SortedBarrelList(object): pass @@ -233,9 +243,9 @@ def main(): for i in range(10000): bl3.insert(0, bl3.pop(len(bl3) / 2)) - del bl3[10:1000] + del bl3[10:5000] import pdb;pdb.set_trace() - del bl3[:5000] + bl3[:20:2] = range(0, -10, -1) import pdb;pdb.set_trace() from collections import defaultdict