From 48ccd6baad35fceec1170bc0311dcd32fd96ba6d Mon Sep 17 00:00:00 2001 From: Mahmoud Hashemi Date: Wed, 13 Mar 2013 03:42:51 -0700 Subject: [PATCH] remove ifilter dependency (using generator expression) --- boltons/setutils.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/boltons/setutils.py b/boltons/setutils.py index ce05ad8..5bf39b9 100644 --- a/boltons/setutils.py +++ b/boltons/setutils.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- -from bisect import insort_left, bisect, bisect_left -from itertools import ifilter, chain +from bisect import insort, bisect +from itertools import chain from collections import MutableSet _MISSING = object() @@ -74,10 +74,11 @@ class IndexedSet(MutableSet): return item in self.item_index_map def __iter__(self): - return ifilter(lambda e: e is not _MISSING, iter(self.item_list)) + return (item for item in self.item_list if item is not _MISSING) def __reversed__(self): - return ifilter(lambda e: e is not _MISSING, reversed(self.item_list)) + item_list = self.item_list + return (item for item in reversed(item_list) if item is not _MISSING) def __repr__(self): return '%s(%r)' % (self.__class__.__name__, list(self)) @@ -95,7 +96,7 @@ class IndexedSet(MutableSet): def remove(self, item): # O(1) + (amortized O(n) cull) try: dead_index = self.item_index_map.pop(item) - insort_left(self.dead_indices, dead_index) + insort(self.dead_indices, dead_index) self.item_list[dead_index] = _MISSING self._cull() except KeyError: @@ -240,7 +241,7 @@ class IndexedSet(MutableSet): if index < 0: index += len_self skip = bisect(self.dead_indices, index) - insort_left(self.dead_indices, index) + insort(self.dead_indices, index) real_index = index + skip ret = self.item_list[real_index] self.item_list[real_index] = _MISSING