-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext_lib.py
More file actions
executable file
·50 lines (34 loc) · 829 Bytes
/
context_lib.py
File metadata and controls
executable file
·50 lines (34 loc) · 829 Bytes
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
"""contextlib library
"""
import contextlib
@contextlib.contextmanager
def show(name: str) -> str:
"""show name
Args:
name (str): name of user
Yields:
str: name with format
"""
# __enter__
print('Starting Context Manger...', name)
yield f"name: {name}"
# __exit__
print('Ending Context Manger...', name)
with show('salar') as one, show('ali') as two:
print(one)
print(two)
class MyContextManager(contextlib.ContextDecorator):
"""my custom context manager
"""
def __enter__(self):
print('Starting...')
def __exit__(self, exc_type, exc_val, exc_tb):
print('Exiting...')
with MyContextManager():
print('OK')
@MyContextManager()
def show_hello():
"""show hello message
"""
print('Hello, there!')
show_hello()