Skip to content

Commit

Permalink
create first context manager
Browse files Browse the repository at this point in the history
  • Loading branch information
KerbezOrazgaliyeva committed Oct 25, 2018
1 parent 13cded1 commit 85d893a
Show file tree
Hide file tree
Showing 15 changed files with 92 additions and 0 deletions.
5 changes: 5 additions & 0 deletions context_manager/a.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
a
da
adawd
cea
wda adwa
78 changes: 78 additions & 0 deletions context_manager/cm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#Here write your imports
import os
from contextlib import contextmanager

#
def simple_open_and_write_without_context_manager(filename):
'''
Write simple code without context manager.
Dont forget to close.
'''
f = open(filename ,'w')
f.write('simple open and write done!')
f.close()

def simple_open_and_write_with_context_manager(filename):
'''
Write your first simple context manager.
Use statmant with
'''
with open(filename, 'w') as f:
f.write('write with context manager done!')







#### Using contextlib ####


@contextmanager
def open_file(file, mode):
f = open(file, mode)
yield f
f.close()

def check_open_file(filename):
with open_file(filename, 'w') as f:
f.write('Lorem ipsum dolor sit amet, consectetur adipiscing elit.')
print(f.closed)


#### CD Example ####
def change_dir_without_context_manager(filename1, filename2):
cwd = os.getcwd()
os.chdir(filename1)
print(os.listdir())
os.chdir(cwd)

cwd = os.getcwd()
os.chdir(filename2)
print(os.listdir())
os.chdir(cwd)




@contextmanager
def change_dir(filename):
try:
cwd = os.getcwd()
os.chdir(filename)
yield
finally:
os.chdir(cwd)

def try_change_dir(filename):
with change_dir(filename):
print(os.listdir())


simple_open_and_write_without_context_manager('first.txt')
simple_open_and_write_with_context_manager('second.txt')
check_open_file('third')
change_dir_without_context_manager('file_one','file_two')
try_change_dir('file_one')
try_change_dir('file_two')
Empty file added context_manager/file_one/I.txt
Empty file.
Empty file.
Empty file.
Empty file.
Empty file added context_manager/file_two/I.txt
Empty file.
Empty file.
Empty file.
Empty file.
1 change: 1 addition & 0 deletions context_manager/first.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
simple open and write done!
3 changes: 3 additions & 0 deletions context_manager/readme.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Context managers allow us to properly manage resources so that we can specify exactly what we want to set up and tear down when working with certain objects.
we can tell that we are using context manager becouse of the with statemant.

1 change: 1 addition & 0 deletions context_manager/second.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
write with context manager done!
1 change: 1 addition & 0 deletions context_manager/third
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
3 changes: 3 additions & 0 deletions context_manager/try.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
with open('a.txt', 'r') as infile:
for line in infile:
print('> {}'.format(line))

0 comments on commit 85d893a

Please sign in to comment.