def test_eq(): this = SortedListWithKey(range(10), load=4, key=negate) that = SortedListWithKey(range(20), load=4, key=negate) assert not (this == that) that.clear() that.update(range(10)) assert this == that
def _characterize_signal(beg, end): """ Characterizes the available signal in a specific time interval. Parameters ---------- beg: Starting time point of the interval. end: Last time point of the interval. Returns ------- out: sortedlist with one entry by lead. Each entry is a 5-size tuple with the lead, the signal samples, the relevant points to represent the samples, the baseline level estimation for the fragment, and the quality of the fragment in that lead. """ siginfo = SortedListWithKey(key=lambda v: -v.quality) for lead in sig_buf.get_available_leads(): baseline, quality = characterize_baseline(lead, beg, end) sig = sig_buf.get_signal_fragment(beg, end, lead=lead)[0] if len(sig) == 0: return None #We build a signal simplification taking at most 9 points, and with #a minimum relevant deviation of 50 uV. points = RDP.arrayRDP(sig, C.RDP_MIN_DIST, C.RDP_NPOINTS) siginfo.add(LeadInfo(lead, sig, points, baseline, quality)) return siginfo
def _find_course_asset(self, asset_key): """ Returns same as _find_course_assets plus the index to the given asset or None. Does not convert to AssetMetadata; thus, is internal. Arguments: asset_key (AssetKey): what to look for Returns: AssetMetadata[] for all assets of the given asset_key's type, & the index of asset in list (None if asset does not exist) """ course_assets = self._find_course_assets(asset_key.course_key) if course_assets is None: return None, None all_assets = SortedListWithKey([], key=itemgetter('filename')) # Assets should be pre-sorted, so add them efficiently without sorting. # extend() will raise a ValueError if the passed-in list is not sorted. all_assets.extend(course_assets.setdefault(asset_key.block_type, [])) # See if this asset already exists by checking the external_filename. # Studio doesn't currently support using multiple course assets with the same filename. # So use the filename as the unique identifier. idx = None idx_left = all_assets.bisect_left({'filename': asset_key.block_id}) idx_right = all_assets.bisect_right({'filename': asset_key.block_id}) if idx_left != idx_right: # Asset was found in the list. idx = idx_left return course_assets, idx
def test_eq(): this = SortedListWithKey(range(10), load=4, key=modulo, value_orderable=False) that = SortedListWithKey(range(20), load=4, key=modulo, value_orderable=False) assert not (this == that) that.clear() that.update(range(10)) assert this == that
def test_copy_copy(): import copy slt = SortedListWithKey(range(100), load=7, key=modulo) two = copy.copy(slt) slt.add(100) assert len(slt) == 101 assert len(two) == 100
def test_key(): slt = SortedListWithKey(range(10000), key=lambda val: val % 10) slt._check() values = sorted(range(10000), key=lambda val: (val % 10, val)) assert slt == values assert all(val in slt for val in range(10000))
def test_key2(): class Incomparable: pass a = Incomparable() b = Incomparable() slt = SortedListWithKey(key=lambda val: 1) slt.add(a) slt.add(b) assert slt == [a, b]
def test_bisect_left(): slt = SortedListWithKey(key=negate) assert slt.bisect_left(0) == 0 slt = SortedListWithKey(range(100), load=17, key=negate) slt.update(range(100)) slt._check() assert slt.bisect_left(50) == 98 assert slt.bisect_left(0) == 198 assert slt.bisect_left(-1) == 200
def get_feasible_next_customers(self, vehicles, count=None): next_pairs = [ (vehicle, customer, Cost.gnnh(self.delta, vehicle, customer)) \ for vehicle in vehicles \ for customer in self.customers \ if vehicle.isFeasible(customer) ] cs = SortedListWithKey(key=itemgetter(2)) cs.update(next_pairs) return cs[:count]
def __mul__(self, other): assert isinstance(other, Set) if len(self.list) == 0: return Set() list = SortedListWithKey(key=self.list._key) for x in self.list: if x in other.list: list.add(x) s = Set(list=list) return s
def test_getitem(): random.seed(0) slt = SortedListWithKey(load=17, key=modulo, value_orderable=False) lst = list(random.random() for rpt in range(100)) slt.update(lst) lst.sort(key=modulo) assert all(slt[idx] == lst[idx] for idx in range(100)) assert all(slt[idx - 99] == lst[idx - 99] for idx in range(100))
def test_setitem(): random.seed(0) slt = SortedListWithKey(range(0, 100, 10), load=4, key=negate) slt[-3] = 20 slt._check() values = list(enumerate(range(95, 5, -10))) random.shuffle(values) for pos, val in values: slt[pos] = val
def test_bisect(): slt = SortedListWithKey(key=modulo, value_orderable=False) assert slt.bisect(10) == 0 slt = SortedListWithKey(range(100), load=17, key=modulo, value_orderable=False) slt.update(range(100)) slt._check() assert slt.bisect(10) == 20 assert slt.bisect(0) == 20
def test_extend(): slt = SortedListWithKey(load=4, key=modulo, value_orderable=False) slt.extend(range(5)) slt._check() slt.extend(range(6, 10)) slt._check()
def test_bisect_right(): slt = SortedListWithKey(key=modulo) assert slt.bisect_right(10) == 0 slt = SortedListWithKey(range(100), load=17, key=modulo) slt.update(range(100)) slt._check() assert slt.bisect_right(10) == 20 assert slt.bisect_right(0) == 20
def test_extend(): slt = SortedListWithKey(load=4, key=modulo) slt.extend(range(5)) slt._check() slt.extend(range(6, 10)) slt._check()
def test_contains(): slt = SortedListWithKey(key=modulo, value_orderable=False) assert 0 not in slt slt.update(range(10000)) for val in range(10000): assert val in slt assert 10000 not in slt slt._check()
def test_contains(): slt = SortedListWithKey(key=negate) assert 0 not in slt slt.update(range(10000)) for val in range(10000): assert val in slt assert 10000 not in slt slt._check()
class LRUCache: READ = 'read' WRITE = 'write' def __init__(self, size, mode): self._size = size self._mode = mode self._cache = {} self._used = {} if mode == self.READ: self._time = 0 elif mode == self.WRITE: self._times = SortedListWithKey(key=self._used.get) def _use(self, key): if self._mode == self.READ: self._used[key] = self._time self._time += 1 elif self._mode == self.WRITE: if self._times: if key in self._used: self._times.discard(key) self._used[key] = self._used[self._times[-1]] + 1 else: self._used[key] = 0 self._times.add(key) def _remove(self): if self._mode == self.READ: lru = min(self._used, key=self._used.get) del self._cache[lru] del self._used[lru] elif self._mode == self.WRITE: lru = self._times.pop(0) del self._cache[lru] del self._used[lru] def get(self, key): item = self._cache.get(key) if item is None: return None self._use(key) return item def set(self, key, value): if key not in self._cache: if len(self._cache) == self._size: self._remove() self._cache[key] = value self._use(key) def __repr__(self): return repr(self._cache)
def __add__(self, other): assert isinstance(other, Set) if len(self.list) == 0 and len(other.list) == 0: return Set() elif len(self.list) == 0: return copy.copy(other) else: list = SortedListWithKey(key=self.list._key) list.update(copy.deepcopy(self.list)) other_list_copy = copy.deepcopy(other.list) for x in other_list_copy: if x not in list: list.add(x) return Set(list=list)
def create_palette(color_depth=8): """ Create palette of all colors for color_depth bit rate. """ palette = SortedListWithKey(load=1000, key=lambda c: c.avg) scale = (MAX_COLOR_DEPTH / 2**color_depth) for x in range(0, 2**color_depth): for y in range(0, 2**color_depth): for z in range(0, 2**color_depth): r = x*scale g = y*scale b = z*scale palette.add( Color(r=r, g=g, b=b, avg=int(avg([r,g,b]))) ) return palette
def test_update(): slt = SortedListWithKey(key=negate) slt.update(range(1000)) assert all(tup[0] == tup[1] for tup in zip(slt, reversed(range(1000)))) assert len(slt) == 1000 slt._check() slt.update(range(10000)) assert len(slt) == 11000 slt._check()
def test_update(): slt = SortedListWithKey(key=modulo) slt.update(range(1000)) assert all(tup[0] == tup[1] for tup in zip(slt, sorted(range(1000), key=modulo))) assert len(slt) == 1000 slt._check() slt.update(range(10000)) assert len(slt) == 11000 slt._check()
def test_getitem(): random.seed(0) slt = SortedListWithKey(load=17, key=negate) lst = list() for rpt in range(100): val = random.random() slt.add(val) lst.append(val) lst.sort(reverse=True) assert all(slt[idx] == lst[idx] for idx in range(100)) assert all(slt[idx - 99] == lst[idx - 99] for idx in range(100))
def test_remove(): slt = SortedListWithKey(key=modulo) assert slt.discard(0) == None assert len(slt) == 0 slt._check() slt = SortedListWithKey([1, 2, 2, 2, 3, 3, 5], load=4, key=modulo) slt.remove(2) slt._check() assert all(tup[0] == tup[1] for tup in zip(slt, [1, 2, 2, 3, 3, 5]))
def __preinit__(self): """Called by :meth:`__init__` and :meth:`__setstate__`.""" self.max_gauge = self.min_gauge = None self.momenta = SortedListWithKey(key=by_until) self._determination = None self._events = SortedList() # a weak set of gauges that refer the gauge as a limit gauge. self._limited_gauges = WeakSet()
def sorted_iterable(iterable, key=None, buffer=100): """sorts an "almost sorted" (infinite) iterable :param iterable: iterable :param key: function used as sort key :param buffer: int size of buffer. elements to swap should not be further than that """ key=key or identity from sortedcontainers import SortedListWithKey b=SortedListWithKey(key=key) for x in iterable: if buffer and len(b)>=buffer: res=b.pop(0) yield res b.add(x) for x in b: # this never happens if iterable is infinite yield x
def anysegmentsintersect(segments): """returns True or False :param segments: line segments :type segments: list of pairs of tuples representing endpoints :return: whether there are any intersections """ l_endpoints = {seg[0]:seg[1] for seg in segments} r_endpoints = {seg[1]:seg[0] for seg in segments} if (len(l_endpoints) < len(segments)) or (len(r_endpoints) < len(segments)): return True endpoints = sorted(reduce(lambda xs,x:xs+[(x[0],0,x[0][1])]+[(x[1],1,x[1][1])],segments,[])) sweep = SortedListWithKey(endpoints[0], key=itemgetter(1)) for e in endpoints[1:]: if e in l_endpoints and e in r_endpoints: return True elif e in l_endpoints: sweep.add(e) # well this is dumb ind = sweep.index(e) try: if segmentsintersect((e,l_endpoints[e]), (sweep[ind+1],l_endpoints[sweep[ind+1]])): return True except IndexError: try: if segmentsintersect((e,l_endpoints[e]), (sweep[ind-1],l_endpoints[sweep[ind-1]])): return True except IndexError: pass elif e in r_endpoints: # well this is dumb ind = sweep.index(e) try: if (segmentsintersect((e,l_endpoints[e]), (sweep[ind+1],l_endpoints[sweep[ind+1]])) and segmentsintersect((e,l_endpoints[e]), (sweep[ind-1],l_endpoints[sweep[ind-1]]))): return True except IndexError: pass del sweep[ind] return False
def __init__(self, size, mode): self._size = size self._mode = mode self._cache = {} self._used = {} if mode == self.READ: self._time = 0 elif mode == self.WRITE: self._times = SortedListWithKey(key=self._used.get)
def __init__(self, name, tweet): """initialize the node class. name:string tweet:Tweet """ self.__nodename__ = name self.__tweets__ = SortedListWithKey(key=lambda d: d.created_at) self.add(tweet)
def test_repr(): this = SortedListWithKey(range(10), key=modulo) this._reset(4) assert repr(this).startswith( 'SortedListWithKey([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], key=<function modulo at ' )
def test_check(): slt = SortedListWithKey(range(10), key=modulo) slt._reset(4) slt._len = 5 slt._check()
def test_remove_valueerror1(): slt = SortedListWithKey(key=modulo) slt.remove(0)
def test_init(): slt = SortedListWithKey(key=modulo) slt._check() slt = SortedListWithKey(load=10000, key=modulo) assert slt._load == 10000 assert slt._twice == 20000 assert slt._half == 5000 slt._check() slt = SortedListWithKey(range(10000), key=modulo) assert all(tup[0] == tup[1] for tup in zip(slt, sorted(range(10000), key=modulo))) slt.clear() assert slt._len == 0 assert slt._maxes == [] assert slt._lists == [] assert isinstance(slt, SortedList) assert isinstance(slt, SortedListWithKey) slt._check()
def test_remove_valueerror3(): slt = SortedListWithKey([1, 2, 2, 2, 3, 3, 5], key=modulo) slt.remove(4)
def test_getitem(): random.seed(0) slt = SortedListWithKey(load=17, key=modulo) slt.append(5) slt._build_index() slt._check() slt.clear() lst = list(random.random() for rpt in range(100)) slt.update(lst) lst.sort(key=modulo) assert all(slt[idx] == lst[idx] for idx in range(100)) assert all(slt[idx - 99] == lst[idx - 99] for idx in range(100))
def test_getitem_indexerror1(): slt = SortedListWithKey(key=modulo) slt[5]
def test_iter(): slt = SortedListWithKey(range(10000), key=modulo) itr = iter(slt) assert all(tup[0] == tup[1] for tup in zip(sorted(range(10000), key=modulo), itr))
def test_eq(): this = SortedListWithKey(range(10), key=modulo) this._reset(4) assert this == list(range(10)) assert this == tuple(range(10)) assert not (this == list(range(9)))
def test_bisect_left(): slt = SortedListWithKey(key=modulo) assert slt.bisect_left(0) == 0 slt = SortedListWithKey(range(100), load=17, key=modulo) slt.update(range(100)) slt._check() assert slt.bisect_left(50) == 0 assert slt.bisect_left(0) == 0
def test_len(): slt = SortedListWithKey(key=modulo) for val in range(10000): slt.add(val) assert len(slt) == (val + 1)
def test_add(): random.seed(0) slt = SortedListWithKey(key=modulo) for val in range(1000): slt.add(val) slt._check() slt = SortedListWithKey(key=modulo) for val in range(1000, 0, -1): slt.add(val) slt._check() slt = SortedListWithKey(key=modulo) for val in range(1000): slt.add(random.random()) slt._check()
def test_islice(): sl = SortedListWithKey(load=7, key=modulo) assert [] == list(sl.islice()) values = sorted(range(100), key=modulo) sl.update(values) for start in range(53): for stop in range(53): assert list(sl.islice(start, stop)) == values[start:stop] for start in range(53): for stop in range(53): assert list(sl.islice(start, stop, reverse=True)) == values[start:stop][::-1] for start in range(53): assert list(sl.islice(start=start)) == values[start:] assert list(sl.islice(start=start, reverse=True)) == values[start:][::-1] for stop in range(53): assert list(sl.islice(stop=stop)) == values[:stop] assert list(sl.islice(stop=stop, reverse=True)) == values[:stop][::-1]
def test_setitem_valueerror2(): slt = SortedListWithKey(range(10), key=modulo) slt[0] = 9
def test_getitem_slicezero(): slt = SortedListWithKey(range(100), load=17, key=modulo) slt[::0]
def test_setitem_extended_slice_bad2(): slt = SortedListWithKey(range(100), load=17, key=modulo) slt[40:90:5] = list(range(10))
def test_remove_valueerror5(): slt = SortedListWithKey([1, 1, 1, 2, 2, 2], key=modulo) slt.remove(12)
def test_setitem_extended_slice_bad1(): slt = SortedListWithKey(range(100), load=17, key=modulo) slt[20:80:3] = list(range(10))
def test_remove_valueerror2(): slt = SortedListWithKey(range(100), load=10, key=modulo) slt.remove(100)
def test_setitem_slice_bad2(): slt = SortedListWithKey(range(100), load=17, key=modulo) slt[20:30] = range(10, 20)
def test_setitem_slice_bad1(): slt = SortedListWithKey(range(100), load=17, key=modulo) slt[10:20] = range(20, 30)
def test_setitem_slice_bad(): slt = SortedListWithKey(range(100), load=17, key=modulo) slt[:10] = list(reversed(range(10)))
def test_setitem(): random.seed(0) slt = SortedListWithKey(range(0, 100), load=17, key=modulo) slt[0] = 100 slt[99] = 99 slt[55] = 45
def test_repr_recursion(): this = SortedListWithKey([[1], [2], [3], [4]], key=lambda val: val) this._lists[-1].append(this) assert repr(this).startswith( 'SortedListWithKey([[1], [2], [3], [4], ...], key=<function ')
def test_delitem_slice(): slt = SortedListWithKey(range(100), load=17, key=modulo) del slt[10:40:1] del slt[10:40:-1] del slt[10:40:2] del slt[10:40:-2]
def test_gt(): this = SortedListWithKey(range(10, 15), key=modulo) this._reset(4) assert this > [10, 11, 11, 13, 14] assert this > [10, 11, 12, 13] assert this > [9]
def test_getitem_indexerror3(): slt = SortedListWithKey(range(100), key=modulo) slt[-101]
def test_imul(): this = SortedListWithKey(range(10), key=modulo) this._reset(4) this *= 5 this._check() assert this == sorted(list(range(10)) * 5, key=modulo)
def test_reversed(): slt = SortedListWithKey(range(10000), key=modulo) rev = reversed(slt) assert all(tup[0] == tup[1] for tup in zip(reversed(sorted(range(10000), key=modulo)), rev))