def test_key(): slt = SortedKeyList(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_delitem_slice(): slt = SortedKeyList(range(100), key=modulo) slt._reset(17) del slt[10:40:1] del slt[10:40:-1] del slt[10:40:2] del slt[10:40:-2]
def test_delitem(): random.seed(0) slt = SortedKeyList(range(100), key=negate) slt._reset(17) while len(slt) > 0: del slt[random.randrange(len(slt))] slt._check()
def __init__(self, n, cols, rows): self.n = n self.cols = cols self.rows = rows self.LFBs = SortedKeyList([], key=lambda x: (x[1], x[0])) self.LFBs.add((0, 0)) self.grid = self.initialize_grid()
def setup(cavern, is_part2=False, elves_power=3): global units global elves global goblins global elves_atk global part2 global G units = SortedKeyList(key=lambda u: u[:2]) elves = {} goblins = {} elves_atk = elves_power part2 = is_part2 G = nx.Graph() for i, row in enumerate(cavern): for j, cell in enumerate(row): if cell == FREE: G.add_node((i, j)) elif cell != WALL: u = [i, j, 200, False, cell] units.add(u) if cell == ELF: elves[i, j] = u elif cell == GOBLIN: goblins[i, j] = u for u in units: u[3] = is_near_enemies(u) for i, j in G.nodes: for ni, nj in adjacent(i, j): if (ni, nj) in G: # assumes borders are all walls G.add_edge((i, j), (ni, nj))
def __init__(self, bids: bool): if bids: self.data = SortedKeyList(key=lambda val: -val[0]) else: self.data = SortedKeyList(key=lambda val: val[0]) self.is_bids = bids self.time = None
class ConstraintList: ''' List of constraints for a single rom variant ''' def __init__(self, constraints: List[Constraint], rom_variant: RomVariant) -> None: self.constraints = SortedKeyList(key=lambda x: x.addr) for constraint in constraints: if constraint.romA == rom_variant: self.constraints.add( RomConstraint(constraint.addressA, constraint)) elif constraint.romB == rom_variant: self.constraints.add( RomConstraint(constraint.addressB, constraint)) def get_constraints_at(self, local_address: int) -> List[Constraint]: constraints = [] index = self.constraints.bisect_key_left(local_address) while index < len(self.constraints ) and self.constraints[index].addr == local_address: constraints.append(self.constraints[index].constraint) index += 1 return constraints
def from_seconds(cls, events: List[BPMAtSecond]) -> TimeMap: """Create a time map from a list of BPM changes with time positions given in seconds. The first BPM implicitely happens at beat zero""" if not events: raise ValueError("No BPM defined") grouped_by_time = group_by(events, key=lambda e: e.seconds) for time, events_at_time in grouped_by_time.items(): if len(events_at_time) > 1: raise ValueError(f"Multiple BPMs defined at {time} seconds : {events}") # take the first BPM change then compute from there sorted_events = sorted(events, key=lambda e: e.seconds) first_event = sorted_events[0] current_beat = Fraction(0) bpm_changes = [BPMChange(current_beat, first_event.seconds, first_event.BPM)] for previous, current in windowed(sorted_events, 2): if previous is None or current is None: continue seconds_since_last_event = current.seconds - previous.seconds beats_since_last_event = ( previous.BPM * seconds_since_last_event ) / Fraction(60) current_beat += beats_since_last_event bpm_change = BPMChange(current_beat, current.seconds, current.BPM) bpm_changes.append(bpm_change) return cls( events_by_beats=SortedKeyList(bpm_changes, key=lambda b: b.beats), events_by_seconds=SortedKeyList(bpm_changes, key=lambda b: b.seconds), )
class MaxStack: def __init__(self): self.by_time = SortedKeyList(key=lambda t: -t[1]) self.by_val = SortedKeyList(key=lambda t: (-t[0], -t[1])) self.time = -1 def push(self, x: int) -> None: self.time += 1 rec = (x, self.time) self.by_time.add(rec) self.by_val.add(rec) def pop(self) -> int: rec = self.by_time.pop(0) self.by_val.remove(rec) return rec[0] def top(self) -> int: rec = self.by_time[0] return rec[0] def peekMax(self) -> int: rec = self.by_val[0] return rec[0] def popMax(self) -> int: rec = self.by_val.pop(0) self.by_time.remove(rec) return rec[0]
def sorted_list_get_with_key(sorted_list: SortedKeyList, key: object) -> object: idx = sorted_list.bisect_key_left(key) if idx < len(sorted_list) and sorted_list.key(sorted_list[idx]) == key: return sorted_list[idx] else: raise ValueError("key '{}' not present in sorted list '{}'".format( key, sorted_list))
def __init__(self): self.bids = SortedKeyList(key=lambda x: -x.price) self.asks = SortedKeyList(key=lambda x: x.price) self.best_bid: Optional[BaseOrder] = None self.best_ask: Optional[BaseOrder] = None self.attempt_match = False self.trades: deque = deque() self.complete_orders: deque = deque()
def __init__(self): self._anchor_points = SortedKeyList(key=lambda x: x.reading_id) self._anchor_streams = {} self._break_streams = set() self._known_converters = { 'rtc': UTCAssigner.convert_rtc }
def __init__(self, max_cache_size, nodes_url, nodes_endpoints): self.memmory = dict() self.max_cache_size = max_cache_size self.usage_list = dllist() self.sorted_expiration_time_list = SortedKeyList( key=lambda item: item.expiration_datetime) self.nodes_url = nodes_url self.nodes_endpoints = nodes_endpoints
def test_copy_copy(): import copy slt = SortedKeyList(range(100), key=modulo) slt._reset(7) two = copy.copy(slt) slt.add(100) assert len(slt) == 101 assert len(two) == 100
def test_pickle(): import pickle alpha = SortedKeyList(range(10000), key=negate) alpha._reset(500) beta = pickle.loads(pickle.dumps(alpha)) assert alpha == beta assert alpha._key == beta._key assert alpha._load == beta._load
def _run_clustering(self): print("try to use clustering") # here we need to run clustering # first of all we need to choose features max_feature_count = int(self._max_freq * len(self._urls)) min_feature_count = int(self._min_freq * len(self._urls)) start_index = bisect.bisect_left(self._features_count_list, (min_feature_count, '')) end_index = bisect.bisect_right(self._features_count_list, (max_feature_count, 'ZZZ')) if start_index >= end_index: print("not enough features") return self._next_queue_fallback() chosen_features = SortedSet() for i in range(start_index, end_index): chosen_features.add(self._features_count_list[i][1]) # then we need to build features matrix X = np.empty((len(self._urls), len(chosen_features))) for i in range(len(self._urls)): features = self._urls[self._urls_keys[i]][self._i_features] for j, fname in enumerate(chosen_features): if fname in features: X[i][j] = 1 else: X[i][j] = 0 # now we can run clustering y = self._clusterizer.fit_predict(X) # and we need to create uniform distributed queue def get_list_of_2_sets(): return [set(), set(), 0] # 0 is for used urls, # 1 is for unused # 3 is for total count url_in_cluster = defaultdict(get_list_of_2_sets) for i in range(len(y)): url = self._urls_keys[i] if self._urls[url][self._i_is_used]: url_in_cluster[y[i]][self._i_list_for_used].add(url) else: url_in_cluster[y[i]][self._i_list_for_unused].add(url) url_in_cluster[y[i]][self._i_list_total] += 1 limit = self._subqueue_len cluster_keys = SortedKeyList(url_in_cluster.keys(), key=lambda x: -len(url_in_cluster[x][self._i_list_for_used])) while limit > 0: # Todo: optimize if len(cluster_keys) > 0: less_index = cluster_keys.pop() unused_urls = url_in_cluster[less_index][self._i_list_for_unused] if len(unused_urls) > 0: url = unused_urls.pop() self._subqueue.put(url) limit -= 1 if len(unused_urls) > 0: url_in_cluster[less_index][self._i_list_for_used].add(url) cluster_keys.add(less_index) else: break
def top_lemmas(self, n=100): """ Returns a list of (lemma, username, number of events) tuples for the n lemmas with the most events. """ lemmas_lengths = SortedKeyList(key=lambda key_val: -key_val[1]) for key, val in self.lemmas_to_logs.items(): lemmas_lengths.add((key, len(val))) return list(lemmas_lengths)[0:n]
def test_pickle(): import pickle alpha = SortedKeyList(range(10000), key=negate) alpha._reset(500) beta = pickle.loads(pickle.dumps(alpha)) assert alpha == beta assert alpha._key == beta._key assert alpha._load == 500 assert beta._load == 1000
def test_key2(): class Incomparable: pass a = Incomparable() b = Incomparable() slt = SortedKeyList(key=lambda val: 1) slt.add(a) slt.add(b) assert slt == [a, b]
def inicia_LEP(red): LEP = SortedKeyList(key=lambda evento: evento.tiempo) for nodo in red.nodos: if nodo.tipo_n == "llegada": evento = Evento( random.expovariate(1 / nodo.t_llegadas), nodo, "llegada_e" ) LEP.add(evento) return LEP
def test_gte(): this = SortedKeyList(range(10), key=negate) this._reset(4) that = SortedKeyList(range(10), key=negate) that._reset(5) assert this >= that assert that >= this del this[-1] assert that >= this assert not (this >= that)
def __init__(self, timecodes={}, name=None): SortedKeyList.__init__(self, key=lambda x: int(x)) self.name = name if type(timecodes) is dict: self._from_dict(timecodes) elif type(timecodes) is list: self._from_list(timecodes) else: raise TypeError(type(timecodes))
def test_delete(): slt = SortedKeyList(range(20), key=modulo) slt._reset(4) slt._check() for val in range(20): slt.remove(val) slt._check() assert len(slt) == 0 assert slt._maxes == [] assert slt._lists == []
def test_op_add(): this = SortedKeyList(range(10), key=modulo) this._reset(4) assert (this + this + this) == (this * 3) that = SortedKeyList(range(10), key=modulo) that._reset(4) that += that that += that assert that == (this * 4)
def inicia_LEP(red): #metodo para leer la red de nodos LEP = SortedKeyList( key=lambda evento: evento.tiempo) #ordena la lisra de eventos for nodo in red.nodos: #recorre la red de nodos if nodo.tipo_n == "llegada": evento = Evento(random.expovariate(1 / nodo.t_llegadas), nodo, "llegada_e") LEP.add(evento) return LEP
def solve(self, initial_state: State, heuristic: callable) -> State: def smart_heuristic(state: State): value = len(state.board_history) / 10 + heuristic(state) return value state_list = SortedKeyList(key=smart_heuristic) state_list.add(initial_state) visited = dict() while len(state_list) > 0: curr: State = state_list.pop(0) key = list_to_string(curr.current_board.content) visited[key] = True if curr.current_board.content == sorted(curr.current_board.content): return curr for child in curr.next_states(): new_key = list_to_string(child.current_board.content) if not visited.get(new_key, False): while len(state_list) > 100: state_list.pop(-1) state_list.add(child) return None
def __init__(self): self._anchor_points = SortedKeyList(key=lambda x: x.reading_id) self._prepared = False self._anchor_streams = {} self._break_streams = set() self._logger = logging.getLogger(__name__) self._known_converters = { 'rtc': UTCAssigner._convert_rtc_anchor, 'epoch': UTCAssigner._convert_epoch_anchor }
def nexttablerow(self): dbrec = next(self.rows) clientrec = self.dte.get_response_data(dbrec) results = SortedKeyList(dbrec.rt_results, key=lambda a: a.eventdate) results = list(results) # may be one or more empty results while len(results) < 2: results.append(RacingTeamResult()) for ndx in range(2): clientrec.update(self._result2client(results[ndx], ndx + 1)) return clientrec
def test_update(): slt = SortedKeyList(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 __init__(self, data, key): SortedKeyList.__init__(self, key=lambda s: int(Timecode(s.offset))) if type(data) is not list: raise TypeError(type(data)) self.twitch = key self.games = [] self.timecodes = Timecodes(timecodes.get(key) or {}) for segment in data: Segment(self, **segment)
def test_eq(): this = SortedKeyList(range(10), key=negate) this._reset(4) that = SortedKeyList(range(20), key=negate) that._reset(4) assert not (this == that) that.clear() that.update(range(10)) assert this == that
def __init__(self, func, bounds, loss_per_simplex=None): self._vdim = None self.loss_per_simplex = loss_per_simplex or default_loss self.data = OrderedDict() self.pending_points = set() if isinstance(bounds, scipy.spatial.ConvexHull): hull_points = bounds.points[bounds.vertices] self._bounds_points = sorted(list(map(tuple, hull_points))) self._bbox = tuple( zip(hull_points.min(axis=0), hull_points.max(axis=0))) self._interior = scipy.spatial.Delaunay(self._bounds_points) else: self._bounds_points = sorted( list(map(tuple, itertools.product(*bounds)))) self._bbox = tuple(tuple(map(float, b)) for b in bounds) self.ndim = len(self._bbox) self.function = func self._tri = None self._losses = dict() self._pending_to_simplex = dict() # vertex → simplex # triangulation of the pending points inside a specific simplex self._subtriangulations = dict() # simplex → triangulation # scale to unit hypercube # for the input self._transform = np.linalg.inv(np.diag(np.diff(self._bbox).flat)) # for the output self._min_value = None self._max_value = None self._output_multiplier = 1 # If we do not know anything, do not scale the values self._recompute_losses_factor = 1.1 # create a private random number generator with fixed seed self._random = random.Random(1) # all real triangles that have not been subdivided and the pending # triangles heap of tuples (-loss, real simplex, sub_simplex or None) # _simplex_queue is a heap of tuples (-loss, real_simplex, sub_simplex) # It contains all real and pending simplices except for real simplices # that have been subdivided. # _simplex_queue may contain simplices that have been deleted, this is # because deleting those items from the heap is an expensive operation, # so when popping an item, you should check that the simplex that has # been returned has not been deleted. This checking is done by # _pop_highest_existing_simplex self._simplex_queue = SortedKeyList(key=_simplex_evaluation_priority)
def abs_end(self) -> Timecode: subrefs = SortedKeyList(key=lambda x: x.abs_start) for segment in self.parent.parent.stream: for ref in segment.references: for subref in ref.subrefs: if subref.abs_start > self.abs_start: subrefs.add(subref) if len(subrefs) > 0: return subrefs[0].abs_start else: return self.parent.parent.abs_end
def test_contains(): slt = SortedKeyList(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 assert -1 not in slt slt._check()
def test_delitem(): random.seed(0) slt = SortedKeyList(range(100), key=modulo) slt._reset(17) while len(slt) > 0: del slt[random.randrange(len(slt))] slt._check() slt = SortedKeyList(range(100), key=modulo) slt._reset(17) del slt[:] assert len(slt) == 0 slt._check()
def test_gt(): this = SortedKeyList(range(10), key=negate) this._reset(4) that = SortedKeyList(range(10, 20), key=negate) that._reset(5) assert that > this assert not (this > that) that = SortedKeyList(range(1, 20), key=negate) that._reset(6) assert that > this that = SortedKeyList(range(1, 10), key=negate) that._reset(4) assert not (that > this)
def test_count(): slt = SortedKeyList(key=negate) slt._reset(7) assert slt.count(0) == 0 for iii in range(100): for jjj in range(iii): slt.add(iii) slt._check() for iii in range(100): assert slt.count(iii) == iii
def test_contains(): slt = SortedKeyList(key=modulo) slt._reset(7) assert 0 not in slt slt.update(range(100)) for val in range(100): assert val in slt assert 100 not in slt slt._check() slt = SortedKeyList(range(100), key=modulo) slt._reset(4) assert all(val not in slt for val in range(100, 200))
def test_copy(): slt = SortedKeyList(range(100), key=negate) slt._reset(7) two = slt.copy() slt.add(100) assert len(slt) == 101 assert len(two) == 100
def test_getitem_slice(): random.seed(0) slt = SortedKeyList(key=modulo) slt._reset(17) lst = list() for rpt in range(100): val = random.random() slt.add(val) lst.append(val) lst.sort(key=modulo) assert all(slt[start:] == lst[start:] for start in [-75, -25, 0, 25, 75]) assert all(slt[:stop] == lst[:stop] for stop in [-75, -25, 0, 25, 75]) assert all(slt[::step] == lst[::step] for step in [-5, -1, 1, 5]) assert all(slt[start:stop] == lst[start:stop] for start in [-75, -25, 0, 25, 75] for stop in [-75, -25, 0, 25, 75]) assert all(slt[:stop:step] == lst[:stop:step] for stop in [-75, -25, 0, 25, 75] for step in [-5, -1, 1, 5]) assert all(slt[start::step] == lst[start::step] for start in [-75, -25, 0, 25, 75] for step in [-5, -1, 1, 5]) assert all(slt[start:stop:step] == lst[start:stop:step] for start in [-75, -25, 0, 25, 75] for stop in [-75, -25, 0, 25, 75] for step in [-5, -1, 1, 5])
def test_irange_key(): values = sorted(range(100), key=modulo) for load in range(5, 16): slt = SortedKeyList(range(100), key=modulo) slt._reset(load) for start in range(10): for end in range(start, 10): temp = list(slt.irange_key(start, end)) assert temp == values[(start * 10):((end + 1) * 10)] temp = list(slt.irange_key(start, end, reverse=True)) assert temp == values[(start * 10):((end + 1) * 10)][::-1] for start in range(10): for end in range(start, 10): temp = list(slt.irange_key(start, end, inclusive=(True, False))) assert temp == values[(start * 10):(end * 10)] for start in range(10): for end in range(start, 10): temp = list(slt.irange_key(start, end, (False, True))) assert temp == values[((start + 1) * 10):((end + 1) * 10)] for start in range(10): for end in range(start, 10): temp = list(slt.irange_key(start, end, inclusive=(False, False))) assert temp == values[((start + 1) * 10):(end * 10)] for start in range(10): temp = list(slt.irange_key(min_key=start)) assert temp == values[(start * 10):] for end in range(10): temp = list(slt.irange_key(max_key=end)) assert temp == values[:(end + 1) * 10]
def test_getitem(): random.seed(0) slt = SortedKeyList(key=negate) slt._reset(17) slt.add(5) assert slt[0] == 5 slt.clear() 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_add(): random.seed(0) slt = SortedKeyList(key=modulo) for val in range(1000): slt.add(val) slt._check() slt = SortedKeyList(key=modulo) for val in range(1000, 0, -1): slt.add(val) slt._check() slt = SortedKeyList(key=modulo) for val in range(1000): slt.add(random.random()) slt._check()
def test_index(): slt = SortedKeyList(range(100), key=modulo) slt._reset(7) for pos, val in enumerate(sorted(range(100), key=modulo)): assert val == slt.index(pos) assert slt.index(9, 0, 1000) == 90 slt = SortedKeyList((0 for rpt in range(100)), key=modulo) slt._reset(7) for start in range(100): for stop in range(start, 100): assert slt.index(0, start, stop + 1) == start for start in range(100): assert slt.index(0, -(100 - start)) == start assert slt.index(0, -1000) == 0
def stress_index2(slt): values = list(slt)[:3] * 200 slt = SortedKeyList(values) for idx, val in enumerate(slt): assert slt.index(val, idx) == idx
def test_index_valueerror3(): slt = SortedKeyList([0] * 10, key=modulo) slt._reset(4) with pytest.raises(ValueError): slt.index(0, 7, 3)
def test_index_valueerror9(): slt = SortedKeyList(key=modulo) slt._reset(4) with pytest.raises(ValueError): slt.index(5)
def test_index_valueerror5(): slt = SortedKeyList(key=modulo) with pytest.raises(ValueError): slt.index(1)
def test_check(): slt = SortedKeyList(range(10), key=modulo) slt._reset(4) slt._len = 5 with pytest.raises(AssertionError): slt._check()
def test_index_valueerror7(): slt = SortedKeyList([0] * 10 + [1] * 10 + [2] * 10, key=modulo) slt._reset(4) with pytest.raises(ValueError): slt.index(1, 0, 10)
def test_index_valueerror10(): slt = SortedKeyList(range(10), key=modulo) slt._reset(4) with pytest.raises(ValueError): slt.index(19)
def test_stress(repeat=1000): slt = SortedKeyList((random.random() for rpt in range(1000))) for rpt in range(repeat): action = random.choice(actions) action(slt) slt._check() while len(slt) > 2000: # Shorten the sortedlist. This maintains the "jaggedness" # of the sublists which helps coverage. pos = random.randrange(len(slt._maxes)) del slt._maxes[pos] del slt._keys[pos] del slt._lists[pos] slt._len = sum(len(sublist) for sublist in slt._lists) slt._index = [] slt._check() slt._check() stress_update(slt) while len(slt) > 0: pos = random.randrange(len(slt)) del slt[pos] slt._check()