-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_contextmanager.py
52 lines (41 loc) · 1.24 KB
/
example_contextmanager.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
"""The example of Context manager
"""
from contextlib import contextmanager
# regular open file
def regular_open_file(file_path):
f = open(file_path, "w")
try:
f.write("maybe can write something that i not really understand.")
pass
except Exception:
raise
finally:
f.close()
# used context manager to open file
def contextmanager_example1(file_path):
with open(file_path, "w") as f:
f.write("maybe can write something that related with contextmanager_example1.")
# handmade context manager
class File:
def __init__(self, file_path, mode):
self.fpath = file_path
self.mode = mode
def __enter__(self):
self.open_file = open(self.fpath, self.mode)
return self.open_file
def __exit__(self, tpye, val, traceback):
print(f"Closing file: {self.fpath}")
self.open_file.close()
# another handmade context manager
@contextmanager
def open_file(fpath, mode):
try:
f = open(fpath, mode)
yield f
f.close()
except Exception:
raise
# example of handmade context manager
def contextmanager_example2(file_path):
with open_file(file_path, "w") as f:
f.write("write to test context manager decorator.")