Skip to content

Commit e36ef8f

Browse files
committed
add queue tutorial
1 parent 4e52d2a commit e36ef8f

File tree

4 files changed

+79
-1
lines changed

4 files changed

+79
-1
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ Python Notes 學習筆記 📝
8888

8989
[__iter__tutorial](https://github.com/twtrubiks/python-notes/blob/master/__iter__tutorial.py) - `__iter__` tutorial
9090

91-
[_str_tutorial.py](https://github.com/twtrubiks/python-notes/blob/master/_str_tutorial.py) - `__str__` tutorial
91+
[__str__tutorial.py](https://github.com/twtrubiks/python-notes/blob/master/__str__tutorial.py) - `__str__` tutorial
9292

9393
[strtobool_tutorial.py](https://github.com/twtrubiks/python-notes/blob/master/strtobool_tutorial.py) - strtobool
9494

@@ -160,6 +160,8 @@ Python Notes 學習筆記 📝
160160

161161
[__call__tutorial.py](https://github.com/twtrubiks/python-notes/blob/master/__call__tutorial.py) - `__call__` tutorial - [youtube tutorial - What is the `__call__` in python](https://youtu.be/YIstPYG15IA)
162162

163+
[queue_tutorial.py](queue_tutorial.py) - queue tutorial
164+
163165
## 觀念
164166

165167
[python_circular_import](https://github.com/twtrubiks/python-notes/tree/master/python_circular_import) - [youtube tutorial - What is Python Circular Imports](https://youtu.be/RQhN24QtDAE)
File renamed without changes.

queue_tutorial.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
from queue import Queue
2+
3+
4+
# if the operation cannot successfully complete because the queue is either empty (cant get) or full ( cant put).
5+
# The default behavior is to block or idly wait until the Queue object has data or room available to
6+
# complete the operation.
7+
# You can have it raise exceptions instead by passing the block=False parameter.
8+
# Or you can have it wait a defined amount of time before raising an exception by passing a timeout parameter
9+
10+
def fifo_ex():
11+
# FIFO
12+
lineup = Queue(maxsize=3)
13+
# lineup.get(block=False)
14+
lineup.put(1)
15+
lineup.put(2)
16+
lineup.put(3)
17+
# lineup.put(4, timeout=1)
18+
print('lineup.full():', lineup.full())
19+
print(lineup.get())
20+
print(lineup.get())
21+
print(lineup.get())
22+
print('lineup.empty():', lineup.empty())
23+
24+
25+
def lifo_ex():
26+
# LIFO, also called stacks ()
27+
# Applicable situation - concurrent
28+
# why not use list
29+
from queue import LifoQueue
30+
stack = LifoQueue(maxsize=3)
31+
stack.put(1)
32+
stack.put(2)
33+
stack.put(3)
34+
# stack.put(4, block=False)
35+
print(stack.get())
36+
print(stack.get())
37+
print(stack.get())
38+
# stack.get(timeout=1)
39+
40+
41+
def priority_queue_ex():
42+
# data structure - heap
43+
# Applicable situation - product recommendation
44+
from queue import PriorityQueue
45+
pq = PriorityQueue()
46+
pq.put((5, 'c'))
47+
pq.put((2, 'a'))
48+
pq.put((1, 'b'))
49+
pq.put((4, 'd'))
50+
while not pq.empty():
51+
print(pq.get())
52+
53+
54+
def priority_queue_ex_2():
55+
from queue import PriorityQueue
56+
pq = PriorityQueue()
57+
pq.put((-5, 'c'))
58+
pq.put((-2, 'a'))
59+
pq.put((-1, 'b'))
60+
pq.put((-4, 'd'))
61+
while not pq.empty():
62+
print(pq.get())
63+
64+
65+
if __name__ == "__main__":
66+
fifo_ex()
67+
# lifo_ex()
68+
# priority_queue_ex()
69+
# priority_queue_ex_2()

set_tutorial.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,10 @@
1111
print('set_seqs:', set_seqs)
1212
set_seqs.remove('test1')
1313
print('set_seqs:', set_seqs)
14+
15+
set_1 = {"a", "b", "c", "d"}
16+
set_2 = {"d", "e", "f", "g"}
17+
print("All: {}".format(set_1.union(set_2)))
18+
print("Both: {}".format(set_1.intersection(set_2)))
19+
print("Either but not both: {}".format(
20+
set_1.symmetric_difference(set_2)))

0 commit comments

Comments
 (0)