-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext_managers.py
59 lines (45 loc) · 1.71 KB
/
context_managers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# This program demonstrates different uses of context managers in Python.
# Using a context manager with the 'with' statement
with open('example.txt', 'w') as file:
file.write("This is a sample text file.\n")
file.write("It is used to demonstrate context managers in Python.\n")
# Using a custom context manager class
class MyContextManager:
def __enter__(self):
print("Entering the context")
def __exit__(self, exc_type, exc_value, traceback):
print("Exiting the context")
# Using the custom context manager with 'with' statement
with MyContextManager():
print("Inside the context")
# Using contextlib for a simple context manager
from contextlib import contextmanager
@contextmanager
def simple_context_manager():
print("Entering the context")
yield
print("Exiting the context")
# Using the simple context manager with 'with' statement
with simple_context_manager():
print("Inside the context")
# Using the 'closing' context manager for resources that need closing
from contextlib import closing
from urllib.request import urlopen
from urllib.error import URLError
url = 'https://www.example.com'
try:
with closing(urlopen(url)) as response:
html_content = response.read()
print("Length of content:", len(html_content))
except URLError as e:
print(f"Error accessing URL: {e}")
# Using the 'redirect_stdout' context manager for redirecting stdout
from contextlib import redirect_stdout
import io
# Redirect stdout to a variable
output_buffer = io.StringIO()
with redirect_stdout(output_buffer):
print("This will be captured by the buffer")
# Access the captured output
captured_output = output_buffer.getvalue()
print("\nCaptured output:", captured_output)