Skip to content

Commit 04972c3

Browse files
committed
Post-final QA fixes
1 parent 4aa3f7d commit 04972c3

File tree

11 files changed

+80
-43
lines changed

11 files changed

+80
-43
lines changed

python-with-statement/exc_handling.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ def __exit__(self, exc_type, exc_value, exc_tb):
1212
return True
1313

1414

15-
with HelloContextManager() as hello:
16-
print(hello)
17-
hello[100]
15+
if __name__ == "__main__":
16+
with HelloContextManager() as hello:
17+
print(hello)
18+
hello[100]
1819

19-
print("Continue normally from here...")
20+
print("Continue normally from here...")

python-with-statement/hello.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,16 @@ def __enter__(self):
55

66
def __exit__(self, exc_type, exc_value, exc_tb):
77
print("Leaving the context...")
8-
if isinstance(exc_value, IndexError):
9-
print(f"An exception occurred in with block: {exc_type}")
10-
print(f"Exception message: {exc_value}")
11-
return True
12-
return False
8+
print(f"{exc_type = }")
9+
print(f"{exc_value = }")
10+
print(f"{exc_tb = }")
1311

1412

15-
with HelloContextManager() as hello:
16-
print(hello)
17-
# hello[100]
13+
if __name__ == "__main__":
14+
with HelloContextManager() as hello:
15+
print(hello)
1816

19-
print("Continue normally from here...")
17+
print("Continue normally from here...")
18+
19+
with HelloContextManager() as hello:
20+
hello[100]

python-with-statement/indenter.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
class Indenter:
2-
def __init__(self):
2+
def __init__(self, width=2):
3+
self.indentation = " " * width
34
self.level = -1
45

56
def __enter__(self):
@@ -10,4 +11,15 @@ def __exit__(self, *_):
1011
self.level -= 1
1112

1213
def print(self, text):
13-
print(" " * self.level + text)
14+
print(self.indentation * self.level + text)
15+
16+
17+
if __name__ == "__main__":
18+
with Indenter() as indenter:
19+
indenter.print("<div>")
20+
with indenter:
21+
indenter.print("<p>")
22+
with indenter:
23+
indenter.print("Hello, World!")
24+
indenter.print("</p>")
25+
indenter.print("</div>")

python-with-statement/redirect.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,10 @@ def __enter__(self):
1111

1212
def __exit__(self, *_):
1313
sys.stdout = self.std_output
14+
15+
16+
if __name__ == "__main__":
17+
with open("hello.txt", "w") as file:
18+
with StandardOutputRedirector(file):
19+
print("Hello, World!")
20+
print("Back to the standard output...")
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
aiohappyeyeballs==2.6.1
2+
aiohttp==3.12.15
3+
aiosignal==1.4.0
4+
attrs==25.3.0
5+
frozenlist==1.7.0
6+
idna==3.10
7+
multidict==6.6.3
8+
propcache==0.3.2
9+
yarl==1.20.1
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import os
22

3-
with os.scandir(".") as dir_entries:
4-
for entry in dir_entries:
3+
with os.scandir(".") as entries:
4+
for entry in entries:
55
print(entry.name, "->", entry.stat().st_size, "bytes")

python-with-statement/site_checker_v1.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@
33
import aiohttp
44

55

6+
async def main():
7+
await asyncio.gather(
8+
check("https://realpython.com"),
9+
check("https://pycoders.com/"),
10+
)
11+
12+
613
async def check(url):
714
async with aiohttp.ClientSession() as session:
815
async with session.get(url) as response:
@@ -11,11 +18,5 @@ async def check(url):
1118
print(f"{url}: type -> {html[:17].strip()}")
1219

1320

14-
async def main():
15-
await asyncio.gather(
16-
check("https://realpython.com"),
17-
check("https://pycoders.com/"),
18-
)
19-
20-
21-
asyncio.run(main())
21+
if __name__ == "__main__":
22+
asyncio.run(main())

python-with-statement/site_checker_v2.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,19 @@ async def __aexit__(self, *_):
1616
await self.session.close()
1717

1818

19-
async def check(url):
20-
async with AsyncSession(url) as response:
21-
print(f"{url}: status -> {response.status}")
22-
html = await response.text()
23-
print(f"{url}: type -> {html[:17].strip()}")
24-
25-
2619
async def main():
2720
await asyncio.gather(
2821
check("https://realpython.com"),
2922
check("https://pycoders.com"),
3023
)
3124

3225

33-
asyncio.run(main())
26+
async def check(url):
27+
async with AsyncSession(url) as response:
28+
print(f"{url}: status -> {response.status}")
29+
html = await response.text()
30+
print(f"{url}: type -> {html[:17].strip()}")
31+
32+
33+
if __name__ == "__main__":
34+
asyncio.run(main())

python-with-statement/timer.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ def __exit__(self, *_):
1010
print(f"Elapsed time: {end - self.start:.4f} seconds")
1111

1212

13-
with Timer():
14-
# The code to measure goes here...
15-
sleep(0.5)
13+
if __name__ == "__main__":
14+
with Timer():
15+
# The code to measure goes here...
16+
sleep(0.5)

python-with-statement/writable_v1.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@ def __exit__(self, *_):
1111
self.file_obj.close()
1212

1313

14-
with WritableFile("hello.txt") as file:
15-
file.write("Hello, World!\n")
14+
if __name__ == "__main__":
15+
with WritableFile("hello.txt") as file:
16+
file.write("Hello, World!\n")

0 commit comments

Comments
 (0)