Skip to content

Commit a3843d3

Browse files
committed
Now that 3.7 is the oldest supported Python, let's move to f-strings.
1 parent 3059998 commit a3843d3

File tree

25 files changed

+45
-48
lines changed

25 files changed

+45
-48
lines changed

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
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

1111
# Changed this from the video due to changes in Python 3.10:
@@ -22,7 +22,7 @@ def main():
2222
loop.run_until_complete(final_task)
2323

2424
dt = datetime.datetime.now() - t0
25-
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)
2626

2727

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

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
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

1414
# Changed this from the video due to changes in Python 3.10:
@@ -24,7 +24,7 @@ def main():
2424
loop.run_until_complete(final_task)
2525

2626
dt = datetime.datetime.now() - t0
27-
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)
2828

2929

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

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@ def main():
2222
loop.run_until_complete(final_task)
2323

2424
dt = datetime.datetime.now() - t0
25-
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)
2626

2727

2828
async def generate_data(num: int, data: asyncio.Queue):
2929
for idx in range(1, num + 1):
3030
item = idx*idx
3131
await data.put((item, datetime.datetime.now()))
3232

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

3636

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

4747
print(colorama.Fore.CYAN +
48-
" +++ 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)
4949
await asyncio.sleep(.5)
5050

5151

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

+3-3
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/05-threads/basic_threads/sync_prod.py

+2-2
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

+2-2
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

+1-1
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

+2-2
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

+1-1
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

+2-3
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

+2-3
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

@@ -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

+2-3
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

+1-1
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

+2-2
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

+2-2
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

+1-1
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/the_unsync/nosync.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def main():
2020
wait_some()
2121

2222
dt = datetime.datetime.now() - t0
23-
print("Synchronous version done in {:,.2f} seconds.".format(dt.total_seconds()))
23+
print(f"Synchronous version done in {dt.total_seconds():,.2f} seconds.")
2424

2525

2626
def compute_some():
@@ -37,7 +37,7 @@ def download_some():
3737

3838
text = resp.text
3939

40-
print("Downloaded (more) {:,} characters.".format(len(text)))
40+
print(f"Downloaded (more) {len(text):,} characters.")
4141

4242

4343
def download_some_more():
@@ -48,7 +48,7 @@ def download_some_more():
4848

4949
text = resp.text
5050

51-
print("Downloaded {:,} characters.".format(len(text)))
51+
print(f"Downloaded {len(text):,} characters.")
5252

5353

5454
def wait_some():

src/09-built-on-asyncio/the_unsync/presync.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def main():
3030
loop.run_until_complete(asyncio.gather(*tasks))
3131

3232
dt = datetime.datetime.now() - t0
33-
print("Synchronous version done in {:,.2f} seconds.".format(dt.total_seconds()))
33+
print(f"Synchronous version done in {dt.total_seconds():,.2f} seconds.")
3434

3535

3636
async def compute_some():
@@ -48,7 +48,7 @@ async def download_some():
4848

4949
text = await resp.text()
5050

51-
print("Downloaded (more) {:,} characters.".format(len(text)))
51+
print(f"Downloaded (more) {len(text):,} characters.")
5252

5353

5454
async def download_some_more():
@@ -59,7 +59,7 @@ async def download_some_more():
5959

6060
text = resp.text
6161

62-
print("Downloaded {:,} characters.".format(len(text)))
62+
print(f"Downloaded {len(text):,} characters.")
6363

6464

6565
async def wait_some():

src/09-built-on-asyncio/the_unsync/thesync.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def main():
2727
[t.result() for t in tasks]
2828

2929
dt = datetime.datetime.now() - t0
30-
print("Synchronous version done in {:,.2f} seconds.".format(dt.total_seconds()))
30+
print(f"Synchronous version done in {dt.total_seconds():,.2f} seconds.")
3131

3232

3333
@unsync(cpu_bound=True)
@@ -47,7 +47,7 @@ async def download_some():
4747

4848
text = await resp.text()
4949

50-
print("Downloaded (more) {:,} characters.".format(len(text)))
50+
print(f"Downloaded (more) {len(text):,} characters.")
5151

5252

5353
@unsync()
@@ -59,7 +59,7 @@ def download_some_more():
5959

6060
text = resp.text
6161

62-
print("Downloaded {:,} characters.".format(len(text)))
62+
print(f"Downloaded {len(text):,} characters.")
6363

6464

6565
@unsync()

src/10-async-web/acityscape_api/app.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def configure_app():
2121
services.sun_service.use_cached_data = data.get('use_cached_data')
2222
services.location_service.use_cached_data = data.get('use_cached_data')
2323

24-
print("Using cached data? {}".format(data.get('use_cached_data')))
24+
print(f"Using cached data? {data.get('use_cached_data')}")
2525

2626

2727
def run_web_app():

src/10-async-web/cityscape_api/app.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def configure_app():
2121
services.sun_service.use_cached_data = data.get('use_cached_data')
2222
services.location_service.use_cached_data = data.get('use_cached_data')
2323

24-
print("Using cached data? {}".format(data.get('use_cached_data')))
24+
print(f"Using cached data? {data.get('use_cached_data')}")
2525

2626

2727
def run_web_app():

src/11-cython/perf/compute_cython.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
def main():
88
math_core.do_math(1)
99

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

1212
processor_count = multiprocessing.cpu_count()
1313
threads = []

src/11-cython/perf/compute_it.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ def main():
1010
do_math(num=3_000_000)
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/11-cython/perf/compute_multiprocessing.py

+1-1
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()

0 commit comments

Comments
 (0)