Python heapq.heappush() Examples
The following are 30
code examples of heapq.heappush().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
heapq
, or try the search function
.
![](https://www.programcreek.com/common/static/images/search.png)
Example #1
Source File: gc.py From lambda-packs with MIT License | 6 votes |
def largest_export_versions(n): """Creates a filter that keeps the largest n export versions. Args: n: number of versions to keep. Returns: A filter function that keeps the n largest paths. """ def keep(paths): heap = [] for idx, path in enumerate(paths): if path.export_version is not None: heapq.heappush(heap, (path.export_version, idx)) keepers = [paths[i] for _, i in heapq.nlargest(n, heap)] return sorted(keepers) return keep
Example #2
Source File: merge_k_Sorted_lists.py From Competitive_Programming with MIT License | 6 votes |
def merge(lists): merged_list = [] heap = [(lst[0], i, 0) for i, lst in enumerate(lists) if lst] heapq.heapify(heap) while heap: #print(heap) val, list_ind, element_ind = heapq.heappop(heap) merged_list.append(val) if element_ind + 1 < len(lists[list_ind]): next_tuple = (lists[list_ind][element_ind + 1], list_ind, element_ind + 1) heapq.heappush(heap, next_tuple) return merged_list #Test Cases
Example #3
Source File: dev_appserver.py From browserscope with Apache License 2.0 | 6 votes |
def UpdateEvent(self, service, event_id, eta): """Update a runnable event in the heap with a new eta. TODO: come up with something better than a linear scan to update items. For the case this is used for now -- updating events to "time out" channels -- this works fine because those events are always soon (within seconds) and thus found quickly towards the front of the heap. One could easily imagine a scenario where this is always called for events that tend to be at the back of the heap, of course... Args: service: the service that owns this event. event_id: the id of the event. eta: the new eta of the event. """ for id in xrange(len(self._events)): item = self._events[id] if item[2] == service and item[3] == event_id: item = (eta, item[1], item[2], item[3]) del(self._events[id]) heapq.heappush(self._events, item) break
Example #4
Source File: gc.py From auto-alt-text-lambda-api with MIT License | 6 votes |
def largest_export_versions(n): """Creates a filter that keeps the largest n export versions. Args: n: number of versions to keep. Returns: A filter function that keeps the n largest paths. """ def keep(paths): heap = [] for idx, path in enumerate(paths): if path.export_version is not None: heapq.heappush(heap, (path.export_version, idx)) keepers = [paths[i] for _, i in heapq.nlargest(n, heap)] return sorted(keepers) return keep
Example #5
Source File: gc.py From auto-alt-text-lambda-api with MIT License | 6 votes |
def largest_export_versions(n): """Creates a filter that keeps the largest n export versions. Args: n: number of versions to keep. Returns: A filter function that keeps the n largest paths. """ def keep(paths): heap = [] for idx, path in enumerate(paths): if path.export_version is not None: heapq.heappush(heap, (path.export_version, idx)) keepers = [paths[i] for _, i in heapq.nlargest(n, heap)] return sorted(keepers) return keep
Example #6
Source File: Learning.py From pneumothorax-segmentation with MIT License | 6 votes |
def post_processing(self, score, epoch, model): if self.freeze_model: return checkpoints_history_path = Path( self.checkpoints_history_folder, '{}_epoch{}.pth'.format(self.calculation_name, epoch) ) torch.save(self.get_state_dict(model), checkpoints_history_path) heapq.heappush(self.score_heap, (score, checkpoints_history_path)) if len(self.score_heap) > self.checkpoints_topk: _, removing_checkpoint_path = heapq.heappop(self.score_heap) removing_checkpoint_path.unlink() self.logger.info('Removed checkpoint is {}'.format(removing_checkpoint_path)) if score > self.best_score: self.best_score = score self.best_epoch = epoch torch.save(self.get_state_dict(model), self.best_checkpoint_path) self.logger.info('best model: {} epoch - {:.5}'.format(epoch, score)) if self.scheduler.__class__.__name__ == 'ReduceLROnPlateau': self.scheduler.step(score) else: self.scheduler.step()
Example #7
Source File: a_star_tree_manhattan_distance.py From pynpuzzle with MIT License | 6 votes |
def search(state, goal_state): """A* tree search using manhattan distance heuristic""" def gn(node): return node.gn() tiles_places = [] for i in range(len(goal_state)): for j in range(len(goal_state)): heapq.heappush(tiles_places, (goal_state[i][j], (i, j))) def hn(node): cost = 0 for i in range(len(node.state)): for j in range(len(node.state)): tile_i, tile_j = tiles_places[node.state[i][j]][1] if i != tile_i or j != tile_j: cost += abs(tile_i - i) + abs(tile_j - j) return cost def fn(node): return gn(node) + hn(node) return bfs.search(state, goal_state, fn)
Example #8
Source File: best_first_seach.py From pynpuzzle with MIT License | 6 votes |
def search(state, goal_state, fn): """Best-first search""" queue = [] entrance = 0 node = Node(state) while not node.is_goal(goal_state): node.expand() for child in node.children: queue_item = (fn(child), entrance, child) heapq.heappush(queue, queue_item) entrance += 1 node = heapq.heappop(queue)[2] output = [] output.append(node.state) for parent in node.parents(): output.append(parent.state) output.reverse() return output
Example #9
Source File: Dijkstra.py From Competitive_Programming with MIT License | 6 votes |
def calculate_distances(graph, starting_vertex): distances = {vertex: float('inf') for vertex in graph} distances[starting_vertex] = 0 pq = [(0, starting_vertex)] visit = [False for vertex in graph] visit[starting_vertex] = True while len(pq) > 0: # print (pq) current_distance, current_vertex = heapq.heappop(pq) visit[current_vertex] = True for neighbor, weight in graph[current_vertex].items(): distance = current_distance + weight # Only consider this new path if it's better than any path we've already found. if not visit[neighbor] and distance < distances[neighbor]: distances[neighbor] = distance heapq.heappush(pq, (distance, neighbor)) return sorted(distances.items())
Example #10
Source File: generate.py From justcopy-backend with MIT License | 6 votes |
def search_docs(inputs, max_ex=5, opts=None): """Given a set of document ids (returned by ranking for a question), search for top N best matching (by heuristic) paragraphs that contain the answer. """ if not opts: raise RuntimeError('Options dict must be supplied.') doc_ids, q_tokens, answer = inputs examples = [] for i, doc_id in enumerate(doc_ids): for j, paragraph in enumerate(re.split(r'\n+', fetch_text(doc_id))): found = find_answer(paragraph, q_tokens, answer, opts) if found: # Reverse ranking, giving priority to early docs + paragraphs score = (found[0], -i, -j, random.random()) if len(examples) < max_ex: heapq.heappush(examples, (score, found[1])) else: heapq.heappushpop(examples, (score, found[1])) return [e[1] for e in examples]
Example #11
Source File: skyline.py From FunUtils with MIT License | 6 votes |
def get_skyline(lrh): """ Wortst Time Complexity: O(NlogN) :type buildings: List[List[int]] :rtype: List[List[int]] """ skyline, live = [], [] i, n = 0, len(lrh) while i < n or live: if not live or i < n and lrh[i][0] <= -live[0][1]: x = lrh[i][0] while i < n and lrh[i][0] == x: heapq.heappush(live, (-lrh[i][2], -lrh[i][1])) i += 1 else: x = -live[0][1] while live and -live[0][1] <= x: heapq.heappop(live) height = len(live) and -live[0][0] if not skyline or height != skyline[-1][1]: skyline += [x, height], return skyline
Example #12
Source File: gc.py From lambda-packs with MIT License | 6 votes |
def largest_export_versions(n): """Creates a filter that keeps the largest n export versions. Args: n: number of versions to keep. Returns: A filter function that keeps the n largest paths. """ def keep(paths): heap = [] for idx, path in enumerate(paths): if path.export_version is not None: heapq.heappush(heap, (path.export_version, idx)) keepers = [paths[i] for _, i in heapq.nlargest(n, heap)] return sorted(keepers) return keep
Example #13
Source File: Dijkstra_Optimized.py From Competitive_Programming with MIT License | 6 votes |
def calculate_distances(graph, starting_vertex): distances = {vertex: float('inf') for vertex in graph} distances[starting_vertex] = 0 pq = [(0, starting_vertex)] visited = 0 visit = [False] * len(graph) while len(pq) > 0: if visited == len(graph): break # print (pq) current_distance, current_vertex = heapq.heappop(pq) if not visit[current_vertex]: visit[current_vertex] = True visited += 1 for neighbor, weight in graph[current_vertex].items(): distance = current_distance + weight if not visit[neighbor] and distance < distances[neighbor]: distances[neighbor] = distance heapq.heappush(pq, (distance, neighbor)) return distances
Example #14
Source File: NetworkDelayTime_Optimized.py From Competitive_Programming with MIT License | 6 votes |
def calculate_distances(graph, starting_vertex): distances = {vertex: float('inf') for vertex in graph} distances[starting_vertex] = 0 pq = [(0, starting_vertex)] visited = 0 visit = [False] * len(graph) while len(pq) > 0: if visited == len(graph): break # print (pq) current_distance, current_vertex = heapq.heappop(pq) if not visit[current_vertex]: visit[current_vertex] = True visited += 1 for neighbor, weight in graph[current_vertex].items(): distance = current_distance + weight if not visit[neighbor] and distance < distances[neighbor]: distances[neighbor] = distance heapq.heappush(pq, (distance, neighbor)) return distances
Example #15
Source File: scheduler.py From classification-of-encrypted-traffic with MIT License | 6 votes |
def run(self): self.__running = True while self.__running: self.__signal.acquire() if not self.__jobs: self.__signal.wait() else: now = datetime.datetime.now() while (self.__jobs[0] < now): job = heapq.heappop(self.__jobs) action = job() if action is not False: self.__runner(action) heapq.heappush(self.__jobs, job) logging.getLogger("scheduler").debug("Sleeping %s seconds", (self.__jobs[0] - now)) self.__signal.wait((self.__jobs[0] - now).total_seconds()) self.__signal.release()
Example #16
Source File: river_network.py From terrain-erosion-3-ways with MIT License | 6 votes |
def compute_height(points, neighbors, deltas, get_delta_fn=None): if get_delta_fn is None: get_delta_fn = lambda src, dst: deltas[dst] dim = len(points) result = [None] * dim seed_idx = min_index([sum(p) for p in points]) q = [(0.0, seed_idx)] while len(q) > 0: (height, idx) = heapq.heappop(q) if result[idx] is not None: continue result[idx] = height for n in neighbors[idx]: if result[n] is not None: continue heapq.heappush(q, (get_delta_fn(idx, n) + height, n)) return util.normalize(np.array(result)) # Same as above, but computes height taking into account river downcutting. # `max_delta` determines the maximum difference in neighboring points (to # give the effect of talus slippage). `river_downcutting_constant` affects how # deeply rivers cut into terrain (higher means more downcutting).
Example #17
Source File: colorizer.py From ec2-api with Apache License 2.0 | 6 votes |
def _addResult(self, test, *args): try: name = test.id() except AttributeError: name = 'Unknown.unknown' test_class, test_name = name.rsplit('.', 1) elapsed = (self._now() - self.start_time).total_seconds() item = (elapsed, test_class, test_name) if len(self.slow_tests) >= self.num_slow_tests: heapq.heappushpop(self.slow_tests, item) else: heapq.heappush(self.slow_tests, item) self.results.setdefault(test_class, []) self.results[test_class].append((test_name, elapsed) + args) self.last_time[test_class] = self._now() self.writeTests()
Example #18
Source File: priority_heap.py From exopy with BSD 3-Clause "New" or "Revised" License | 6 votes |
def push(self, priority, obj): """Push a task with a given priority on the queue. Parameters ---------- priority : int Priority associated with the object to push. obj : Object to push on the heap. """ task = [priority, self._counter, obj] heapq.heappush(self._heap, task) self._map[obj] = task self._counter += 1
Example #19
Source File: launchpad.py From concurrency2017 with MIT License | 6 votes |
def run_until_complete(self): # Start all the coroutines. for coro in self._new: wait_for = coro.send(None) heapq.heappush(self._waiting, Task(wait_for, coro)) # Keep running until there is no more work to do. while self._waiting: now = datetime.datetime.now() # Get the coroutine with the soonest resumption time. task = heapq.heappop(self._waiting) if now < task.waiting_until: # We're ahead of schedule; wait until it's time to resume. delta = task.waiting_until - now time.sleep(delta.total_seconds()) now = datetime.datetime.now() try: # It's time to resume the coroutine. wait_until = task.coro.send(now) heapq.heappush(self._waiting, Task(wait_until, task.coro)) except StopIteration: # The coroutine is done. pass
Example #20
Source File: cluster.py From lingvo with Apache License 2.0 | 6 votes |
def _AssignVar(self, var_op): size = var_op.get_attr('dtype').size shape = tf.TensorShape(var_op.get_attr('shape')) assert self._var_space_pq, ('No ps devices to use.') allocated, device = heapq.heappop(self._var_space_pq) if shape.num_elements() is None: assert var_op.name.endswith( 'wb/var'), 'Unexpected name pattern: %s' % var_op.name # CuDNN RNN vars shape aren't known statically, decide to make a constant # estimate to avoid introducing more complexities. allocated += 10 * 1024**2 * size else: allocated += shape.num_elements() * size heapq.heappush(self._var_space_pq, (allocated, device)) tf.logging.info('Place variable %s on %s %d', var_op.name, device, allocated) return device
Example #21
Source File: Queue.py From meddle with MIT License | 5 votes |
def _put(self, item, heappush=heapq.heappush): heappush(self.queue, item)
Example #22
Source File: Queue.py From ironpython2 with Apache License 2.0 | 5 votes |
def _put(self, item, heappush=heapq.heappush): heappush(self.queue, item)
Example #23
Source File: crossover.py From gaps with MIT License | 5 votes |
def _add_buddy_piece_candidate(self, piece_id, position, relative_piece): piece_candidate = (BUDDY_PIECE_PRIORITY, (position, piece_id), relative_piece) heapq.heappush(self._candidate_pieces, piece_candidate)
Example #24
Source File: sched.py From meddle with MIT License | 5 votes |
def run(self): """Execute events until the queue is empty. When there is a positive delay until the first event, the delay function is called and the event is left in the queue; otherwise, the event is removed from the queue and executed (its action function is called, passing it the argument). If the delay function returns prematurely, it is simply restarted. It is legal for both the delay function and the action function to to modify the queue or to raise an exception; exceptions are not caught but the scheduler's state remains well-defined so run() may be called again. A questionable hack is added to allow other threads to run: just after an event is executed, a delay of 0 is executed, to avoid monopolizing the CPU when other threads are also runnable. """ # localize variable access to minimize overhead # and to improve thread safety q = self._queue delayfunc = self.delayfunc timefunc = self.timefunc pop = heapq.heappop while q: time, priority, action, argument = checked_event = q[0] now = timefunc() if now < time: delayfunc(time - now) else: event = pop(q) # Verify that the event was not removed or altered # by another thread after we last looked at q[0]. if event is checked_event: action(*argument) delayfunc(0) # Let other threads run else: heapq.heappush(q, event)
Example #25
Source File: utils.py From lrrbot with Apache License 2.0 | 5 votes |
def pick_weighted_random_elements(iterable, k): queue = [] for elem, weight in iterable: if not weight: continue r = random.random() ** (1 / weight) if len(queue) < k: heapq.heappush(queue, (r, elem)) elif queue[0][0] < r: heapq.heapreplace(queue, (r, elem)) return [elem for r, elem in queue]
Example #26
Source File: crossover.py From gaps with MIT License | 5 votes |
def _add_best_match_piece_candidate(self, piece_id, position, priority, relative_piece): piece_candidate = (priority, (position, piece_id), relative_piece) heapq.heappush(self._candidate_pieces, piece_candidate)
Example #27
Source File: caption_generator.py From densecap-tensorflow with MIT License | 5 votes |
def push(self, x): """Pushes a new element.""" assert self._data is not None if len(self._data) < self._n: heapq.heappush(self._data, x) else: heapq.heappushpop(self._data, x)
Example #28
Source File: sched.py From meddle with MIT License | 5 votes |
def enterabs(self, time, priority, action, argument): """Enter a new event in the queue at an absolute time. Returns an ID for the event which can be used to remove it, if necessary. """ event = Event(time, priority, action, argument) heapq.heappush(self._queue, event) return event # The ID
Example #29
Source File: sched.py From ironpython2 with Apache License 2.0 | 5 votes |
def enterabs(self, time, priority, action, argument): """Enter a new event in the queue at an absolute time. Returns an ID for the event which can be used to remove it, if necessary. """ event = Event(time, priority, action, argument) heapq.heappush(self._queue, event) return event # The ID
Example #30
Source File: sched.py From ironpython2 with Apache License 2.0 | 5 votes |
def run(self): """Execute events until the queue is empty. When there is a positive delay until the first event, the delay function is called and the event is left in the queue; otherwise, the event is removed from the queue and executed (its action function is called, passing it the argument). If the delay function returns prematurely, it is simply restarted. It is legal for both the delay function and the action function to modify the queue or to raise an exception; exceptions are not caught but the scheduler's state remains well-defined so run() may be called again. A questionable hack is added to allow other threads to run: just after an event is executed, a delay of 0 is executed, to avoid monopolizing the CPU when other threads are also runnable. """ # localize variable access to minimize overhead # and to improve thread safety q = self._queue delayfunc = self.delayfunc timefunc = self.timefunc pop = heapq.heappop while q: time, priority, action, argument = checked_event = q[0] now = timefunc() if now < time: delayfunc(time - now) else: event = pop(q) # Verify that the event was not removed or altered # by another thread after we last looked at q[0]. if event is checked_event: action(*argument) delayfunc(0) # Let other threads run else: heapq.heappush(q, event)