Skip to content

Commit

Permalink
fixed conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
An4ik committed Dec 22, 2018
2 parents 5b6cca4 + cdb42cc commit fadf9c5
Show file tree
Hide file tree
Showing 21 changed files with 618 additions and 90 deletions.
30 changes: 30 additions & 0 deletions context_manager/answers/change_directory.py
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()
77 changes: 0 additions & 77 deletions context_manager/answers/context_manager.py

This file was deleted.

17 changes: 17 additions & 0 deletions context_manager/answers/context_manager_as_class.py
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)
33 changes: 33 additions & 0 deletions context_manager/change_directory.py
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
"""

16 changes: 16 additions & 0 deletions context_manager/context_manager_as_class.py
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 removed context_manager/first.txt
Empty file.
22 changes: 20 additions & 2 deletions context_manager/instruction.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand All @@ -18,4 +18,22 @@

7. Congratulations, you have passed first level. Commit and push your changes.

### LEVEL-2
### 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.
Empty file removed context_manager/second.txt
Empty file.
27 changes: 27 additions & 0 deletions context_manager/tests/test_change_directory.py
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'])
11 changes: 11 additions & 0 deletions context_manager/tests/test_context_manager_as_class.py
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)')
4 changes: 4 additions & 0 deletions context_manager/tests/test_write_into_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@
read_file
)

<<<<<<< HEAD
class LevelOneTest(TestCase):
=======
class LevelTwoTest(TestCase):
>>>>>>> cdb42cc1efb71900fdcfa9b0e9831c25313b30ee
def test_write_without(self):
result = write_without_context_manager('first.txt')
self.assertEqual(result, True)
Expand Down
Empty file removed context_manager/third.txt
Empty file.
2 changes: 1 addition & 1 deletion context_manager/write_into_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,4 @@ def check_open_file(filename):
def read_file(filename):
with open(filename, 'r') as f:
for line in f:
return format(line)
return format(line)
Binary file modified docs/build/doctrees/context_manager.doctree
Binary file not shown.
Binary file modified docs/build/doctrees/environment.pickle
Binary file not shown.
52 changes: 52 additions & 0 deletions docs/build/html/_sources/context_manager.rst.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,48 @@ or as class by using magic functions::

We want you to pass three levels for a better understanding of **context manager**

<<<<<<< HEAD


LEVEL-1
-------
=======
Context manager as class
------------------------

1. Look at the file context_manager_as_class.py

2. Look at the test_context_manager_as_class.py in tests folder

3. Run tests with command::

python3 -m unittest tests/test_context_manager_as_class.py

As you see all your tests are failed

4. Now, complete the functions in context_manager_as_class.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 first level. Commit and push your changes.

Write into file
---------------
>>>>>>> cdb42cc1efb71900fdcfa9b0e9831c25313b30ee

1. Look at the file write_into_file.py

2. Look at the tests in tests folter

3. Run tests with command::

<<<<<<< HEAD
python3 -m unittest tests/tests_for_level_one.py
=======
python3 -m unittest tests/tests_write_into_file.py
>>>>>>> cdb42cc1efb71900fdcfa9b0e9831c25313b30ee

As you see all your tests are failed

Expand All @@ -47,6 +77,28 @@ LEVEL-1

7. Congratulations, you have passed first level. Commit and push your changes.

<<<<<<< HEAD
LEVEL-2
-------

=======
Change directory
----------------
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.
>>>>>>> cdb42cc1efb71900fdcfa9b0e9831c25313b30ee
Loading

0 comments on commit fadf9c5

Please sign in to comment.