-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcontext_managers.py
More file actions
101 lines (70 loc) · 2.34 KB
/
context_managers.py
File metadata and controls
101 lines (70 loc) · 2.34 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import os
from contextlib import contextmanager
# f = open("sample.txt", 'w')
# f.write("Adding some more text to it")
# f.close()
# use of context manager "with", no need to write close, that get handled itself and hence efficiently managing resources.
# Even if there is an error in writing or opening to it.
with open("sample.txt", "w") as f:
f.write("Adding some text to it")
class Open_file:
def __init__(self, file_name, mode) -> None:
self.file_name = file_name
self.mode = mode
def __enter__(self):
print("Inside enter")
self.file = open(self.file_name, self.mode)
return self.file
def __exit__(self, exec_type, exec_val, traceback):
print("exit called")
self.file.close()
# Testing if exit is called when returning from with in the context decorator
def random_fn():
print("inside fn")
with Open_file("sample_2.txt", "w") as f: # init and enter method called
print("Opening a file")
f.write("I am Ayush working as backend developer (SDE-2) at innovaccer.")
return 6
# # exit method called
random_fn()
print(f.closed)
# Using contextlib
print("---------------- Using contextLib ------------------ ")
@contextmanager
def open_file(file_name, mode):
try:
f = open(file_name, mode) # class equivalent of init and enter method
yield f # yieldling out file object, so that we can work with it later
finally:
f.close() # exec method
with open_file("sample_3.txt", "w") as f:
f.write("Hey! you people are awesome")
print(f.closed)
# open in python is already a context manager.
# example 2
# cwd = os.getcwd()
# os.chdir("oops")
# print(os.listdir())
# os.chdir(cwd)
# cwd = os.getcwd()
# os.chdir("solid_principles")
# print(os.listdir())
# os.chdir(cwd)
@contextmanager
def change_dir(destination):
try:
cwd = os.getcwd()
os.chdir(destination)
yield
finally:
os.chdir(cwd) # we need not to do this again and again
with change_dir("oops"):
print(os.listdir())
with change_dir("solid_principles"):
print(os.listdir())
"""
Context manager is useful in many scenarios:
1. Opening and closing database connection
2. Acquiring and releasing locks on resources
When using decorator contextmanager, after that we can use the function with, "with"
"""