-
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
Showing
21 changed files
with
618 additions
and
90 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,30 @@ | ||
import os | ||
from contextlib import contextmanager | ||
|
||
|
||
def change_dir_without_context_manager(filename1, filename2): | ||
cwd = os.getcwd() | ||
os.chdir(filename1) | ||
a = os.listdir() | ||
os.chdir(cwd) | ||
|
||
cwd = os.getcwd() | ||
os.chdir(filename2) | ||
b = os.listdir() | ||
os.chdir(cwd) | ||
return a + b | ||
|
||
|
||
@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): | ||
return os.listdir() |
This file was deleted.
Oops, something went wrong.
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,17 @@ | ||
class File(object): | ||
def __init__(self, file_name, method): | ||
self.file_obj = open(file_name, method) | ||
def __enter__(self): | ||
return self.file_obj | ||
def __exit__(self, type, value, traceback): | ||
self.file_obj.close() | ||
|
||
def check(filename): | ||
with File(filename, 'w') as opened_file: | ||
opened_file.write('Yeah! You can write context manager as class)') | ||
|
||
|
||
def read_file(filename): | ||
with open(filename, 'r') as f: | ||
for line in f: | ||
return format(line) |
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,33 @@ | ||
import os | ||
from contextlib import contextmanager | ||
|
||
|
||
def change_dir_without_context_manager(filename1, filename2): | ||
""" | ||
Changes directory from pwd to pwd/filename. | ||
:param filename1: name of first folder | ||
:param filename2: name of second folder | ||
:return: list if files in filename1 and filename2 | ||
""" | ||
|
||
|
||
""" | ||
With context manager. | ||
""" | ||
|
||
|
||
@contextmanager | ||
def change_dir(filename): | ||
""" | ||
Changes directory from pwd to pwd/filename. | ||
:param filename: name of folder | ||
""" | ||
|
||
|
||
def try_change_dir(filename): | ||
""" | ||
Uses change_dir(). | ||
:param filename: name of folder | ||
:return: list of files in folder | ||
""" | ||
|
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,16 @@ | ||
class File(object): | ||
""" | ||
Context manager class with '__init__', '__enter__', '__exit__' methods that opens and closes file. | ||
""" | ||
|
||
def check(filename): | ||
""" | ||
Writes 'Yeah! You can write context manager as class)') into filename with File class. | ||
:param filename: txt file | ||
""" | ||
|
||
|
||
def read_file(filename): | ||
with open(filename, 'r') as f: | ||
for line in f: | ||
return format(line) |
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
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,27 @@ | ||
from unittest import TestCase | ||
from change_directory import ( | ||
change_dir_without_context_manager, | ||
try_change_dir | ||
) | ||
|
||
class ContextManagerTest(TestCase): | ||
def test_change_dir_without(self): | ||
result = change_dir_without_context_manager('file_one','file_two') | ||
self.assertEqual(result, ['love.txt', 'context.txt', 'I.txt', 'manager.txt', 'love.txt', 'and.txt', 'I.txt', 'beshbarmak.txt']) | ||
|
||
def test_change_dir_without1(self): | ||
result = change_dir_without_context_manager('file_one', 'file_one') | ||
self.assertEqual(result, ['love.txt', 'context.txt', 'I.txt', 'manager.txt', 'love.txt', 'context.txt', 'I.txt', 'manager.txt']) | ||
|
||
def test_change_dir_without2(self): | ||
result = change_dir_without_context_manager('file_two', 'file_two') | ||
self.assertEqual(result, ['love.txt', 'and.txt', 'I.txt', 'beshbarmak.txt', 'love.txt', 'and.txt', 'I.txt', 'beshbarmak.txt']) | ||
|
||
|
||
def test_change_dir_with(self): | ||
result = try_change_dir('file_one') | ||
self.assertEqual(result, ['love.txt', 'context.txt', 'I.txt', 'manager.txt']) | ||
|
||
def test_change_dir_with1(self): | ||
result = try_change_dir('file_two') | ||
self.assertEqual(result,['love.txt', 'and.txt', 'I.txt', 'beshbarmak.txt']) |
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,11 @@ | ||
from unittest import TestCase | ||
from context_manager_as_class import ( | ||
check, | ||
read_file | ||
) | ||
|
||
class LevelOneTest(TestCase): | ||
def test_write_with1(self): | ||
check('first.txt') | ||
result = read_file('first.txt') | ||
self.assertEqual(result, 'Yeah! You can write context manager as class)') |
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
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
Binary file not shown.
Binary file not shown.
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
Oops, something went wrong.