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

25 files changed

Lines changed: 45 additions & 48 deletions

File tree

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

Lines changed: 2 additions & 2 deletions
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‎

Lines changed: 2 additions & 2 deletions
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‎

Lines changed: 3 additions & 3 deletions
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‎

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/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):

0 commit comments

Comments
 (0)