Skip to content

Commit e3ee75a

Browse files
committed
Merge branch 'master' of github.com:fluentpython/example-code-2e
2 parents f152417 + 6fb0832 commit e3ee75a

29 files changed

+149
-71
lines changed

02-array-seq/lispy/py3.10/examples_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ def test_factorial():
147147

148148
gcd_src = """
149149
(define (mod m n)
150-
(- m (* n (// m n))))
150+
(- m (* n (quotient m n))))
151151
(define (gcd m n)
152152
(if (= n 0)
153153
m

02-array-seq/lispy/py3.9/lis.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ def standard_env() -> Environment:
8080
'-': op.sub,
8181
'*': op.mul,
8282
'/': op.truediv,
83-
'//': op.floordiv,
83+
'quotient': op.floordiv,
8484
'>': op.gt,
8585
'<': op.lt,
8686
'>=': op.ge,

18-with-match/lispy/py3.10/examples_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ def test_factorial():
146146

147147
gcd_src = """
148148
(define (mod m n)
149-
(- m (* n (// m n))))
149+
(- m (* n (quotient m n))))
150150
(define (gcd m n)
151151
(if (= n 0)
152152
m

18-with-match/lispy/py3.10/lis.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ def standard_env() -> Environment:
8383
'-': op.sub,
8484
'*': op.mul,
8585
'/': op.truediv,
86-
'//': op.floordiv,
86+
'quotient': op.floordiv,
8787
'>': op.gt,
8888
'<': op.lt,
8989
'>=': op.ge,

18-with-match/lispy/py3.9/examples_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ def test_factorial():
147147

148148
gcd_src = """
149149
(define (mod m n)
150-
(- m (* n (// m n))))
150+
(- m (* n (quotient m n))))
151151
(define (gcd m n)
152152
(if (= n 0)
153153
m
@@ -255,4 +255,4 @@ def test_closure_with_change(capsys):
255255
def test_closure_averager():
256256
got = run(closure_averager_src)
257257
assert got == 12.0
258-
# end::RUN_AVERAGER[]
258+
# end::RUN_AVERAGER[]

18-with-match/lispy/py3.9/lis.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ def standard_env() -> Environment:
8080
'-': op.sub,
8181
'*': op.mul,
8282
'/': op.truediv,
83-
'//': op.floordiv,
83+
'quotient': op.floordiv,
8484
'>': op.gt,
8585
'<': op.lt,
8686
'>=': op.ge,

19-concurrency/primes/procs.py

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -31,46 +31,46 @@ def worker(jobs: JobQueue, results: ResultQueue) -> None: # <7>
3131
while n := jobs.get(): # <8>
3232
results.put(check(n)) # <9>
3333
results.put(PrimeResult(0, False, 0.0)) # <10>
34-
# end::PRIMES_PROC_TOP[]
3534

36-
# tag::PRIMES_PROC_MIDDLE[]
37-
def start_jobs(workers: int, jobs: JobQueue, results: ResultQueue) -> None:
35+
def start_jobs(
36+
procs: int, jobs: JobQueue, results: ResultQueue # <11>
37+
) -> None:
3838
for n in NUMBERS:
39-
jobs.put(n) # <1>
40-
for _ in range(workers):
41-
proc = Process(target=worker, args=(jobs, results)) # <2>
42-
proc.start() # <3>
43-
jobs.put(0) # <4>
44-
45-
def report(workers: int, results: ResultQueue) -> int:
46-
checked = 0
47-
workers_done = 0
48-
while workers_done < workers:
49-
n, prime, elapsed = results.get()
50-
if n == 0:
51-
workers_done += 1
52-
else:
53-
checked += 1
54-
label = 'P' if prime else ' '
55-
print(f'{n:16} {label} {elapsed:9.6f}s')
56-
return checked
57-
# end::PRIMES_PROC_MIDDLE[]
39+
jobs.put(n) # <12>
40+
for _ in range(procs):
41+
proc = Process(target=worker, args=(jobs, results)) # <13>
42+
proc.start() # <14>
43+
jobs.put(0) # <15>
44+
# end::PRIMES_PROC_TOP[]
5845

5946
# tag::PRIMES_PROC_MAIN[]
6047
def main() -> None:
61-
if len(sys.argv) < 2:
62-
workers = cpu_count()
48+
if len(sys.argv) < 2: # <1>
49+
procs = cpu_count()
6350
else:
64-
workers = int(sys.argv[1])
51+
procs = int(sys.argv[1])
6552

66-
print(f'Checking {len(NUMBERS)} numbers with {workers} processes:')
53+
print(f'Checking {len(NUMBERS)} numbers with {procs} processes:')
6754
t0 = perf_counter()
68-
jobs: JobQueue = SimpleQueue()
55+
jobs: JobQueue = SimpleQueue() # <2>
6956
results: ResultQueue = SimpleQueue()
70-
start_jobs(workers, jobs, results)
71-
checked = report(workers, results)
57+
start_jobs(procs, jobs, results) # <3>
58+
checked = report(procs, results) # <4>
7259
elapsed = perf_counter() - t0
73-
print(f'{checked} checks in {elapsed:.2f}s')
60+
print(f'{checked} checks in {elapsed:.2f}s') # <5>
61+
62+
def report(procs: int, results: ResultQueue) -> int: # <6>
63+
checked = 0
64+
procs_done = 0
65+
while procs_done < procs: # <7>
66+
n, prime, elapsed = results.get() # <8>
67+
if n == 0: # <9>
68+
procs_done += 1
69+
else:
70+
checked += 1 # <10>
71+
label = 'P' if prime else ' '
72+
print(f'{n:16} {label} {elapsed:9.6f}s')
73+
return checked
7474

7575
if __name__ == '__main__':
7676
main()
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#!/usr/bin/env python3
2+
3+
"""
4+
procs.py: shows that multiprocessing on a multicore machine
5+
can be faster than sequential code for CPU-intensive work.
6+
"""
7+
8+
# tag::PRIMES_PROC_TOP[]
9+
import sys
10+
from time import perf_counter
11+
from typing import NamedTuple
12+
from multiprocessing import Process, SimpleQueue, cpu_count # <1>
13+
from multiprocessing import queues # <2>
14+
15+
from primes import is_prime, NUMBERS
16+
17+
class PrimeResult(NamedTuple): # <3>
18+
n: int
19+
prime: bool
20+
elapsed: float
21+
22+
JobQueue = queues.SimpleQueue[int] # <4>
23+
ResultQueue = queues.SimpleQueue[PrimeResult] # <5>
24+
25+
def check(n: int) -> PrimeResult: # <6>
26+
t0 = perf_counter()
27+
res = is_prime(n)
28+
return PrimeResult(n, res, perf_counter() - t0)
29+
30+
def worker(jobs: JobQueue, results: ResultQueue) -> None: # <7>
31+
while n := jobs.get(): # <8>
32+
results.put(check(n)) # <9>
33+
results.put(PrimeResult(0, False, 0.0))
34+
# end::PRIMES_PROC_TOP[]
35+
36+
def start_jobs(workers: int) -> ResultQueue:
37+
jobs: JobQueue = SimpleQueue() # <2>
38+
results: ResultQueue = SimpleQueue()
39+
40+
for n in NUMBERS: # <3>
41+
jobs.put(n)
42+
43+
for _ in range(workers):
44+
proc = Process(target=worker, args=(jobs, results)) # <4>
45+
proc.start() # <5>
46+
jobs.put(0) # <6>
47+
48+
return results
49+
50+
def report(workers: int, results: ResultQueue) -> int:
51+
workers_done = 0
52+
checked = 0
53+
while workers_done < workers:
54+
n, prime, elapsed = results.get() # <7>
55+
if n == 0:
56+
workers_done += 1
57+
else:
58+
checked += 1
59+
label = 'P' if prime else ' '
60+
print(f'{n:16} {label} {elapsed:9.6f}s') # <8>
61+
return checked
62+
63+
64+
# tag::PRIMES_PROC_MAIN[]
65+
def main() -> None:
66+
if len(sys.argv) < 2: # <1>
67+
workers = cpu_count()
68+
else:
69+
workers = int(sys.argv[1])
70+
71+
print(f'Checking {len(NUMBERS)} numbers with {workers} processes:')
72+
t0 = perf_counter()
73+
results = start_jobs(workers)
74+
checked = report(workers, results)
75+
elapsed = perf_counter() - t0
76+
print(f'{checked} checks in {elapsed:.2f}s')
77+
78+
if __name__ == '__main__':
79+
main()
80+
# end::PRIMES_PROC_MAIN[]
File renamed without changes.

20-executors/getflags/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
flags/
2+
downloaded/

0 commit comments

Comments
 (0)