From 891a6239f0ad9d443d190db66910b142cd7ee714 Mon Sep 17 00:00:00 2001 From: KerbezOrazgaliyeva <160107140@stu.sdu.edu.kz> Date: Sat, 10 Nov 2018 21:40:17 +0600 Subject: [PATCH] level2 --- context_manager/answers/change_directory.py | 30 +++++++++++++ context_manager/change_directory.py | 33 ++++++++++++++ context_manager/instruction.md | 44 ++++++++++++++++++- .../tests/test_change_directory.py | 27 ++++++++++++ 4 files changed, 132 insertions(+), 2 deletions(-) create mode 100644 context_manager/answers/change_directory.py create mode 100644 context_manager/change_directory.py create mode 100644 context_manager/tests/test_change_directory.py diff --git a/context_manager/answers/change_directory.py b/context_manager/answers/change_directory.py new file mode 100644 index 0000000..8c8ac62 --- /dev/null +++ b/context_manager/answers/change_directory.py @@ -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() diff --git a/context_manager/change_directory.py b/context_manager/change_directory.py new file mode 100644 index 0000000..293fd34 --- /dev/null +++ b/context_manager/change_directory.py @@ -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 + """ + diff --git a/context_manager/instruction.md b/context_manager/instruction.md index 234b424..c8be33f 100644 --- a/context_manager/instruction.md +++ b/context_manager/instruction.md @@ -2,7 +2,7 @@ 1. Look at the file write_into_file.py -2. Look at the tests in tests folter +2. Look at the tests in tests folder 3. Run tests with command: @@ -18,4 +18,44 @@ 7. Congratulations, you have passed first level. Commit and push your changes. -### LEVEL-2 \ No newline at end of file +### LEVEL-2 + +1. Look at the file change_directory.py + +2. Look at the test_change_directory.py in tests folder + +3. Run tests with command: + + ```python3 -m unittest tests/test_change_directory.py``` + + As you see all your tests are failed + +4. Now, complete the functions in change_directory.py + +5. Run tests and all of them should be passed. + +6. Compare your answer with mine located in answers/change_directory.py + +7. Congratulations, you have passed second level. Commit and push your changes. + +### LEVEL-3 + +1. Look at the file change_directory.py + +2. Look at the test_change_directory.py in tests folder + +3. Run tests with command: + + ```python3 -m unittest tests/test_change_directory.py``` + + As you see all your tests are failed + +4. Now, complete the functions in change_directory.py + +5. Run tests and all of them should be passed. + +6. Compare your answer with mine located in answers/change_directory.py + +7. Congratulations, you have passed second level. Commit and push your changes. + + diff --git a/context_manager/tests/test_change_directory.py b/context_manager/tests/test_change_directory.py new file mode 100644 index 0000000..10f96d2 --- /dev/null +++ b/context_manager/tests/test_change_directory.py @@ -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']) \ No newline at end of file