Skip to content

Commit f082bd5

Browse files
committed
Update materials after final QA
1 parent 6df3fe7 commit f082bd5

File tree

10 files changed

+53
-93
lines changed

10 files changed

+53
-93
lines changed

python-deque/custom_queue.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ def dequeue(self):
1212
try:
1313
return self._items.popleft()
1414
except IndexError:
15+
# Break exception chain to hide implementation details
1516
raise IndexError("dequeue from an empty queue") from None
1617

1718
def __len__(self):
@@ -20,11 +21,23 @@ def __len__(self):
2021
def __contains__(self, item):
2122
return item in self._items
2223

23-
def __str__(self):
24-
return f"Queue({list(self._items)})"
25-
2624
def __iter__(self):
2725
yield from self._items
2826

2927
def __reversed__(self):
3028
yield from reversed(self._items)
29+
30+
def __repr__(self):
31+
return f"Queue({list(self._items)})"
32+
33+
34+
if __name__ == "__main__":
35+
queue = Queue()
36+
print("Enqueueing items with '.enqueue()'...")
37+
queue.enqueue(1)
38+
queue.enqueue(2)
39+
queue.enqueue(3)
40+
print(queue)
41+
print("Dequeueing one item with '.dequeue()'...")
42+
print(queue.dequeue())
43+
print(queue)

python-deque/page.py

Lines changed: 0 additions & 18 deletions
This file was deleted.

python-deque/page_history.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from collections import deque
2+
3+
page_history = deque(maxlen=3)
4+
5+
urls = ("https://google.com", "https://yahoo.com", "https://www.bing.com")
6+
7+
for url in urls:
8+
page_history.appendleft(url)
9+
10+
print(page_history)
11+
12+
page_history.appendleft("https://youtube.com")
13+
14+
print(page_history)
15+
16+
page_history.appendleft("https://facebook.com")
17+
18+
print(page_history)

python-deque/producer_consumer.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
logging.basicConfig(level=logging.INFO, format="%(message)s")
88

99

10-
def wait_seconds(mins, maxs):
10+
def _wait_seconds(mins, maxs):
1111
time.sleep(mins + random.random() * (maxs - mins))
1212

1313

@@ -19,7 +19,7 @@ def produce(queue, size):
1919
logging.info("Produced: %d -> %s", value, str(queue))
2020
else:
2121
logging.info("Queue is saturated")
22-
wait_seconds(0.1, 0.5)
22+
_wait_seconds(0.1, 0.5)
2323

2424

2525
def consume(queue):
@@ -30,7 +30,7 @@ def consume(queue):
3030
logging.info("Queue is empty")
3131
else:
3232
logging.info("Consumed: %d -> %s", value, str(queue))
33-
wait_seconds(0.2, 0.7)
33+
_wait_seconds(0.2, 0.7)
3434

3535

3636
logging.info("Starting Threads...\n")

python-deque/rotate.py

Lines changed: 0 additions & 21 deletions
This file was deleted.

python-deque/tail.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
def tail(filename, lines=10):
55
try:
66
with open(filename) as file:
7-
return deque(file, lines)
7+
return deque(file, maxlen=lines)
88
except OSError as error:
99
print(f'Opening file "{filename}" failed with error: {error}')
10+
11+
12+
if __name__ == "__main__":
13+
print(tail("./README.md"))

python-deque/threads.py

Lines changed: 0 additions & 42 deletions
This file was deleted.

python-deque/time_append.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from time import perf_counter
33

44
TIMES = 10_000
5+
NANOSECONDS_PER_SECOND = 1e9
56
a_list = []
67
a_deque = deque()
78

@@ -11,7 +12,8 @@ def average_time(func, times):
1112
for i in range(times):
1213
start = perf_counter()
1314
func(i)
14-
total += (perf_counter() - start) * 1e9
15+
# Convert to ns to improve readability
16+
total += (perf_counter() - start) * NANOSECONDS_PER_SECOND
1517
return total / times
1618

1719

python-deque/time_pop.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
from collections import deque
22
from time import perf_counter
33

4-
TIMES = 10000
4+
TIMES = 10_000
5+
NANOSECONDS_PER_SECOND = 1e9
56
a_list = [1] * TIMES
67
a_deque = deque(a_list)
78

@@ -11,13 +12,14 @@ def average_time(func, times):
1112
for _ in range(times):
1213
start = perf_counter()
1314
func()
14-
total += (perf_counter() - start) * 1e9
15+
# Convert to ns to improve readability
16+
total += (perf_counter() - start) * NANOSECONDS_PER_SECOND
1517
return total / times
1618

1719

1820
list_time = average_time(lambda: a_list.pop(0), TIMES)
1921
deque_time = average_time(lambda: a_deque.popleft(), TIMES)
2022
gain = list_time / deque_time
2123

22-
print(f"list.pop() {list_time:.6} ns")
24+
print(f"list.pop(0) {list_time:.6} ns")
2325
print(f"deque.popleft() {deque_time:.6} ns ({gain:.6}x faster)")

python-deque/time_random_access.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from time import perf_counter
33

44
TIMES = 10_000
5+
MICROSECONDS_PER_SECOND = 1e6
56
a_list = [1] * TIMES
67
a_deque = deque(a_list)
78

@@ -11,7 +12,8 @@ def average_time(func, times):
1112
for _ in range(times):
1213
start = perf_counter()
1314
func()
14-
total += (perf_counter() - start) * 1e6
15+
# Convert to μs to improve readability
16+
total += (perf_counter() - start) * MICROSECONDS_PER_SECOND
1517
return total / times
1618

1719

0 commit comments

Comments
 (0)