Repository navigation
Expand file tree
/
Copy pathpriority_queue.py
More file actions
72 lines (59 loc) · 2.14 KB
/
Copy pathpriority_queue.py
File metadata and controls
72 lines (59 loc) · 2.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import heapq
import itertools
from dataclasses import dataclass, field
from queue import PriorityQueue
from typing import Any
import bisect
tasks = []
heapq.heappush(tasks, (2, "send report"))
heapq.heappush(tasks, (1, "fix outage"))
first = heapq.heappop(tasks) # (1, 'fix outage')
print('first', '=', repr(first))
jobs = [(5, "backup"), (1, "deploy"), (3, "email")]
heapq.heapify(jobs)
order = [heapq.heappop(jobs)[1] for _ in range(3)] # ['deploy', 'email', 'backup']
print('order', '=', repr(order))
counter = itertools.count()
heap = []
for prio, task in [(2, {"name": "a"}), (1, {"name": "b"}), (2, {"name": "c"})]:
heapq.heappush(heap, (prio, next(counter), task))
names = [heapq.heappop(heap)[2]["name"] for _ in range(3)] # ['b', 'a', 'c']
print('names', '=', repr(names))
scores = []
for s in [40, 95, 70]:
heapq.heappush_max(scores, s)
best = heapq.heappop_max(scores) # 95 (Python 3.14+)
print('best', '=', repr(best))
old_style = []
for s in [40, 95, 70]:
heapq.heappush(old_style, -s)
best_old = -heapq.heappop(old_style) # 95
print('best_old', '=', repr(best_old))
@dataclass(order=True)
class Job:
priority: int
item: Any = field(compare=False)
q = PriorityQueue()
for p, name in [(5, "how"), (4, "to"), (1, "do"), (3, "in"), (2, "java")]:
q.put(Job(p, name))
served = [q.get().item for _ in range(5)] # ['do', 'java', 'in', 'to', 'how']
print('served', '=', repr(served))
ordered = []
for job in [(3, "in"), (1, "do"), (2, "java")]:
bisect.insort(ordered, job)
# ordered = [(1, 'do'), (2, 'java'), (3, 'in')]
lowest = ordered.pop(0) # (1, 'do')
print('lowest', '=', repr(lowest))
def run(initial):
heap, seq = [], itertools.count()
for prio, name in initial:
heapq.heappush(heap, (prio, next(seq), name))
done = []
while heap:
prio, _, name = heapq.heappop(heap)
done.append(name)
if name == "deploy": # deploying creates a follow-up task
heapq.heappush(heap, (1, next(seq), "smoke test"))
return done
log = run([(2, "deploy"), (3, "email"), (2, "backup")])
# ['deploy', 'smoke test', 'backup', 'email']