-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
13cded1
commit 85d893a
Showing
15 changed files
with
92 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
a | ||
da | ||
adawd | ||
cea | ||
wda adwa |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
simple open and write done! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
write with context manager done! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Lorem ipsum dolor sit amet, consectetur adipiscing elit. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |