Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 523d5bc

Browse files
authored
Merge branch 'talkpython:master' into master
2 parents dd481f3 + 3780f9e commit 523d5bc

File tree

35 files changed

+103
-75
lines changed

35 files changed

+103
-75
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,3 +140,5 @@ src/11-cython/.idea/modules.xml
140140
src/11-cython/.idea/workspace.xml
141141
greeter.c
142142
math_core.c
143+
144+
.idea/

src/04-asyncio/loops/loops_asyncio/loop_program.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@
55

66
def main():
77
lim = 250_000
8-
print("Running standard loop with {:,} actions.".format(lim*2))
8+
print(f"Running standard loop with {lim * 2:,} actions.")
99
t0 = datetime.datetime.now()
1010

11-
loop = asyncio.get_event_loop()
11+
# Changed this from the video due to changes in Python 3.10:
12+
# DeprecationWarning: There is no current event loop, loop = asyncio.get_event_loop()
13+
loop = asyncio.new_event_loop()
14+
1215
data = asyncio.Queue()
1316

1417
task1 = loop.create_task(generate_data(lim, data))
@@ -19,7 +22,7 @@ def main():
1922
loop.run_until_complete(final_task)
2023

2124
dt = datetime.datetime.now() - t0
22-
print(colorama.Fore.WHITE + "App exiting, total time: {:,.2f} sec.".format(dt.total_seconds()), flush=True)
25+
print(colorama.Fore.WHITE + f"App exiting, total time: {dt.total_seconds():,.2f} sec.", flush=True)
2326

2427

2528
async def generate_data(num: int, data: asyncio.Queue):

src/04-asyncio/loops/loops_uv/loop_program_uv.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@
88

99
def main():
1010
lim = 250000
11-
print("Running standard loop with {:,} actions.".format(lim * 2))
11+
print(f"Running standard loop with {lim * 2:,} actions.")
1212
t0 = datetime.datetime.now()
1313

14-
loop = asyncio.get_event_loop()
14+
# Changed this from the video due to changes in Python 3.10:
15+
# DeprecationWarning: There is no current event loop, loop = asyncio.get_event_loop()
16+
loop = asyncio.new_event_loop()
1517
data = asyncio.Queue()
1618

1719
task1 = loop.create_task(generate_data(lim, data))
@@ -22,7 +24,7 @@ def main():
2224
loop.run_until_complete(final_task)
2325

2426
dt = datetime.datetime.now() - t0
25-
print(colorama.Fore.WHITE + "App exiting, total time: {:,.2f} sec.".format(dt.total_seconds()), flush=True)
27+
print(colorama.Fore.WHITE + f"App exiting, total time: {dt.total_seconds():,.2f} sec.", flush=True)
2628

2729

2830
async def generate_data(num: int, data: asyncio.Queue):

src/04-asyncio/producer_consumer/prod_async/async_program.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55

66

77
def main():
8-
9-
loop = asyncio.get_event_loop()
8+
# Changed this from the video due to changes in Python 3.10:
9+
# DeprecationWarning: There is no current event loop, loop = asyncio.get_event_loop()
10+
loop = asyncio.new_event_loop()
1011

1112
t0 = datetime.datetime.now()
1213
print(colorama.Fore.WHITE + "App started.", flush=True)
@@ -21,15 +22,15 @@ def main():
2122
loop.run_until_complete(final_task)
2223

2324
dt = datetime.datetime.now() - t0
24-
print(colorama.Fore.WHITE + "App exiting, total time: {:,.2f} sec.".format(dt.total_seconds()), flush=True)
25+
print(colorama.Fore.WHITE + f"App exiting, total time: {dt.total_seconds():,.2f} sec.", flush=True)
2526

2627

2728
async def generate_data(num: int, data: asyncio.Queue):
2829
for idx in range(1, num + 1):
2930
item = idx*idx
3031
await data.put((item, datetime.datetime.now()))
3132

32-
print(colorama.Fore.YELLOW + " -- generated item {}".format(idx), flush=True)
33+
print(colorama.Fore.YELLOW + f" -- generated item {idx}", flush=True)
3334
await asyncio.sleep(random.random() + .5)
3435

3536

@@ -44,7 +45,7 @@ async def process_data(num: int, data: asyncio.Queue):
4445
dt = datetime.datetime.now() - t
4546

4647
print(colorama.Fore.CYAN +
47-
" +++ Processed value {} after {:,.2f} sec.".format(value, dt.total_seconds()), flush=True)
48+
f" +++ Processed value {value} after {dt.total_seconds():,.2f} sec.", flush=True)
4849
await asyncio.sleep(.5)
4950

5051

src/04-asyncio/producer_consumer/prod_sync/sync_program.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ def main():
1212
process_data(20, data)
1313

1414
dt = datetime.datetime.now() - t0
15-
print(colorama.Fore.WHITE + "App exiting, total time: {:,.2f} sec.".format(dt.total_seconds()), flush=True)
15+
print(colorama.Fore.WHITE + f"App exiting, total time: {dt.total_seconds():,.2f} sec.", flush=True)
1616

1717

1818
def generate_data(num: int, data: list):
1919
for idx in range(1, num + 1):
2020
item = idx*idx
2121
data.append((item, datetime.datetime.now()))
2222

23-
print(colorama.Fore.YELLOW + " -- generated item {}".format(idx), flush=True)
23+
print(colorama.Fore.YELLOW + f" -- generated item {idx}", flush=True)
2424
time.sleep(random.random() + .5)
2525

2626

@@ -38,7 +38,7 @@ def process_data(num: int, data: list):
3838
dt = datetime.datetime.now() - t
3939

4040
print(colorama.Fore.CYAN +
41-
" +++ Processed value {} after {:,.2f} sec.".format(value, dt.total_seconds()), flush=True)
41+
f" +++ Processed value {value} after {dt.total_seconds():,.2f} sec.", flush=True)
4242
time.sleep(.5)
4343

4444

src/04-asyncio/web_scraping/async_scrape/program.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import asyncio
2+
import datetime
23

34
import aiohttp
45
import bs4
@@ -32,10 +33,16 @@ def get_title(html: str, episode_number: int) -> str:
3233

3334

3435
def main():
36+
t0 = datetime.datetime.now()
37+
3538
global loop
36-
loop = asyncio.get_event_loop()
39+
# Changed this from the video due to changes in Python 3.10:
40+
# DeprecationWarning: There is no current event loop, loop = asyncio.get_event_loop()
41+
loop = asyncio.new_event_loop()
3742
loop.run_until_complete(get_title_range())
38-
print("Done.")
43+
44+
dt = datetime.datetime.now() - t0
45+
print(f"Done in {dt.total_seconds():.2f} sec.")
3946

4047

4148
async def get_title_range_old_version():
@@ -50,7 +57,7 @@ async def get_title_range():
5057
# Please keep this range pretty small to not DDoS my site. ;)
5158

5259
tasks = []
53-
for n in range(150, 160):
60+
for n in range(375, 385):
5461
tasks.append((n, loop.create_task(get_html(n))))
5562

5663
for n, t in tasks:

src/04-asyncio/web_scraping/sync_scrape/program.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import datetime
2+
13
import requests
24
import bs4
35
from colorama import Fore
@@ -24,13 +26,15 @@ def get_title(html: str, episode_number: int) -> str:
2426

2527

2628
def main():
29+
t0 = datetime.datetime.now()
2730
get_title_range()
28-
print("Done.")
31+
dt = datetime.datetime.now() - t0
32+
print(f"Done in {dt.total_seconds():.2f} sec.")
2933

3034

3135
def get_title_range():
3236
# Please keep this range pretty small to not DDoS my site. ;)
33-
for n in range(150, 160):
37+
for n in range(375, 385):
3438
html = get_html(n)
3539
title = get_title(html, n)
3640
print(Fore.WHITE + f"Title found: {title}", flush=True)

src/05-threads/basic_threads/sync_prod.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def main():
1414
process_data(40, data)
1515

1616
dt = datetime.datetime.now() - t0
17-
print(colorama.Fore.WHITE + "App exiting, total time: {:,.2f} sec.".format(dt.total_seconds()), flush=True)
17+
print(colorama.Fore.WHITE + f"App exiting, total time: {dt.total_seconds():,.2f} sec.", flush=True)
1818

1919

2020
def generate_data(num: int, data: list):
@@ -40,7 +40,7 @@ def process_data(num: int, data: list):
4040
dt = datetime.datetime.now() - t
4141

4242
print(colorama.Fore.CYAN +
43-
" +++ Processed value {} after {:,.2f} sec.".format(value, dt.total_seconds()), flush=True)
43+
f" +++ Processed value {value} after {dt.total_seconds():,.2f} sec.", flush=True)
4444
time.sleep(.5)
4545

4646

src/05-threads/basic_threads/threaded_prod.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def main():
2828
break
2929

3030
dt = datetime.datetime.now() - t0
31-
print(colorama.Fore.WHITE + "App exiting, total time: {:,.2f} sec.".format(dt.total_seconds()), flush=True)
31+
print(colorama.Fore.WHITE + f"App exiting, total time: {dt.total_seconds():,.2f} sec.", flush=True)
3232

3333

3434
def check_cancel():
@@ -62,7 +62,7 @@ def process_data(num: int, data: list):
6262
dt = datetime.datetime.now() - t
6363

6464
print(colorama.Fore.CYAN +
65-
" +++ Processed value {} after {:,.2f} sec.".format(value, dt.total_seconds()), flush=True)
65+
f" +++ Processed value {value} after {dt.total_seconds():,.2f} sec.", flush=True)
6666
time.sleep(.5)
6767

6868

src/05-threads/cpu_attempt/compute_it.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ def main():
1010
do_math(num=30000000)
1111

1212
dt = datetime.datetime.now() - t0
13-
print("Done in {:,.2f} sec.".format(dt.total_seconds()))
13+
print(f"Done in {dt.total_seconds():,.2f} sec.")
1414

1515

1616
def do_math(start=0, num=10):

src/05-threads/cpu_attempt/compute_threaded.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ def main():
1010
t0 = datetime.datetime.now()
1111

1212
# do_math(num=30000000)
13-
print("Doing math on {:,} processors.".format(multiprocessing.cpu_count()))
13+
print(f"Doing math on {multiprocessing.cpu_count():,} processors.")
1414

1515
processor_count = multiprocessing.cpu_count()
1616
threads = []
@@ -25,7 +25,7 @@ def main():
2525
[t.join() for t in threads]
2626

2727
dt = datetime.datetime.now() - t0
28-
print("Done in {:,.2f} sec.".format(dt.total_seconds()))
28+
print(f"Done in {dt.total_seconds():,.2f} sec.")
2929

3030

3131
def do_math(start=0, num=10):

src/05-threads/hello_threads/hello.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def main():
2222

2323
def greeter(name: str, times: int):
2424
for n in range(0, times):
25-
print("{}. Hello there {}".format(n, name))
25+
print(f"{n}. Hello there {name}")
2626
time.sleep(1)
2727

2828

src/06-thread-safety/safe_bank.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def main():
3232

3333
dt = datetime.datetime.now() - t0
3434

35-
print("Transfers complete ({:,.2f}) sec".format(dt.total_seconds()))
35+
print(f"Transfers complete ({dt.total_seconds():,.2f}) sec")
3636
validate_bank(accounts, total)
3737

3838

@@ -87,8 +87,7 @@ def validate_bank(accounts: List[Account], total: int, quiet=False):
8787
current, total
8888
), flush=True)
8989
elif not quiet:
90-
print("All good: Consistent account balance: ${:,}".format(
91-
total), flush=True)
90+
print(f"All good: Consistent account balance: ${total:,}", flush=True)
9291

9392

9493
def get_two_accounts(accounts):

src/06-thread-safety/safe_bank_fine_grained.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def main():
3333

3434
dt = datetime.datetime.now() - t0
3535

36-
print("Transfers complete ({:,.2f}) sec".format(dt.total_seconds()))
36+
print(f"Transfers complete ({dt.total_seconds():,.2f}) sec")
3737
validate_bank(accounts, total)
3838

3939

@@ -91,7 +91,7 @@ def validate_bank(accounts: List[Account], total: int, quiet=False):
9191
# with transfer_lock:
9292
# current = sum(a.balance for a in accounts)
9393

94-
[a.lock.acquire() for a in accounts]
94+
[a.lock.acquire() for a in sorted(accounts, key=lambda x: id(x))]
9595
current = sum(a.balance for a in accounts)
9696
[a.lock.release() for a in accounts]
9797

@@ -100,8 +100,7 @@ def validate_bank(accounts: List[Account], total: int, quiet=False):
100100
current, total
101101
), flush=True)
102102
elif not quiet:
103-
print("All good: Consistent account balance: ${:,}".format(
104-
total), flush=True)
103+
print(f"All good: Consistent account balance: ${total:,}", flush=True)
105104

106105

107106
def get_two_accounts(accounts):

src/06-thread-safety/unsafe_bank.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def main():
3232

3333
dt = datetime.datetime.now() - t0
3434

35-
print("Transfers complete ({:,.2f}) sec".format(dt.total_seconds()))
35+
print(f"Transfers complete ({dt.total_seconds():,.2f}) sec")
3636
validate_bank(accounts, total)
3737

3838

@@ -71,8 +71,7 @@ def validate_bank(accounts: List[Account], total: int, quiet=False):
7171
current, total
7272
), flush=True)
7373
elif not quiet:
74-
print("All good: Consistent account balance: ${:,}".format(
75-
total), flush=True)
74+
print(f"All good: Consistent account balance: ${total:,}", flush=True)
7675

7776

7877
def get_two_accounts(accounts):

src/07-multiprocessing/cpu_attempt/compute_it.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ def main():
1010
do_math(num=30000000)
1111

1212
dt = datetime.datetime.now() - t0
13-
print("Done in {:,.2f} sec.".format(dt.total_seconds()))
13+
print(f"Done in {dt.total_seconds():,.2f} sec.")
1414

1515

1616
def do_math(start=0, num=10):

src/07-multiprocessing/cpu_attempt/compute_multiprocessing.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ def main():
88

99
t0 = datetime.datetime.now()
1010

11-
print("Doing math on {:,} processors.".format(multiprocessing.cpu_count()))
11+
print(f"Doing math on {multiprocessing.cpu_count():,} processors.")
1212

1313
pool = multiprocessing.Pool()
1414
processor_count = multiprocessing.cpu_count()
@@ -22,7 +22,7 @@ def main():
2222
pool.join()
2323

2424
dt = datetime.datetime.now() - t0
25-
print("Done in {:,.2f} sec.".format(dt.total_seconds()))
25+
print(f"Done in {dt.total_seconds():,.2f} sec.")
2626
print("Our results: ")
2727
for t in tasks:
2828
print(t.get())

src/07-multiprocessing/cpu_attempt/compute_threaded.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ def main():
1010
t0 = datetime.datetime.now()
1111

1212
# do_math(num=30000000)
13-
print("Doing math on {:,} processors.".format(multiprocessing.cpu_count()))
13+
print(f"Doing math on {multiprocessing.cpu_count():,} processors.")
1414

1515
processor_count = multiprocessing.cpu_count()
1616
threads = []
@@ -25,7 +25,7 @@ def main():
2525
[t.join() for t in threads]
2626

2727
dt = datetime.datetime.now() - t0
28-
print("Done in {:,.2f} sec.".format(dt.total_seconds()))
28+
print(f"Done in {dt.total_seconds():,.2f} sec.")
2929

3030

3131
def do_math(start=0, num=10):

src/08-execution-pools/program.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def main():
2929

3030
print("Done", flush=True)
3131
for f in work:
32-
print("{}".format(f.result()), flush=True)
32+
print(f"{f.result()}", flush=True)
3333

3434

3535
def get_title(url: str) -> str:

src/09-built-on-asyncio/requirements.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
unsync
2-
trio
3-
trio_asyncio
2+
trio==0.16.0
3+
trio_asyncio==0.11.0
44

55
aiohttp
66
cchardet

src/09-built-on-asyncio/the_trio/prod_asyncio.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ def main():
88
t0 = datetime.datetime.now()
99
print(colorama.Fore.WHITE + "App started.", flush=True)
1010

11-
loop = asyncio.get_event_loop()
11+
# Changed this from the video due to changes in Python 3.10:
12+
# DeprecationWarning: There is no current event loop, loop = asyncio.get_event_loop()
13+
loop = asyncio.new_event_loop()
1214
data = asyncio.Queue()
1315

1416
task1 = loop.create_task(generate_data(20, data))

0 commit comments

Comments
 (0)