Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit d4668ea

Browse files
committedNov 11, 2021
Updated tests and task in From Import
1 parent 290de74 commit d4668ea

File tree

7 files changed

+54
-52
lines changed

7 files changed

+54
-52
lines changed
 

‎Modules and packages/From import/calculator.py

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,24 @@
33
"""
44

55

6-
class Add:
6+
class Calculator:
77
def __init__(self):
88
self.current = 0
99

1010
def add(self, amount):
1111
self.current += amount
1212

13-
def get_current(self):
14-
return self.current
15-
16-
17-
class Subtract:
18-
def __init__(self):
19-
self.current = 100
20-
2113
def subtract(self, amount):
2214
self.current -= amount
2315

24-
def get_current(self):
25-
return self.current
26-
27-
28-
class Multiply:
29-
def __init__(self):
30-
self.current = 10
31-
3216
def multiply(self, amount):
3317
self.current *= amount
3418

19+
def divide(self, amount):
20+
try:
21+
self.current /= amount
22+
except ZeroDivisionError:
23+
print('Can not divide by zero.')
24+
3525
def get_current(self):
3626
return self.current
Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1-
from calculator import Add
1+
from calculator import Calculator
22
from my_module import hello as hey
33

4+
print(hey("User"))
45

5-
calc = Add() # Name `Calculator` used directly without prefix `calculator`
6+
calc = Calculator() # Name `Calculator` used directly without prefix `calculator`
67
calc.add(2)
8+
calc.multiply(100)
9+
calc.divide(3)
10+
711
print(calc.get_current())
812

913

10-
print(hey("User"))
14+

‎Modules and packages/From import/task-info.yaml

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,13 @@ files:
77
- name: from_import.py
88
visible: true
99
placeholders:
10-
- offset: 27
11-
length: 34
12-
placeholder_text: '# Import hello_world from my_module'
13-
- name: tests.py
10+
- offset: 0
11+
length: 33
12+
placeholder_text: '# Import class Calculator from another module'
13+
- offset: 97
14+
length: 12
15+
placeholder_text: '''Instantiate a calculator'''
16+
- name: tests/__init__.py
17+
visible: false
18+
- name: tests/test_task.py
1419
visible: false

‎Modules and packages/From import/task.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ can use the imported name without the `module_name` prefix. For example:
66
```python
77
from calculator import Add
88

9-
calc = Add() # name `Calculator` used directly without prefix `calculator`
9+
calc = Add() # name `Add` used directly without prefix `calculator`
1010
```
1111

1212
This does not introduce the name of the module from which the imports are taken in the
@@ -37,8 +37,9 @@ when utilising `from` with similar effects:
3737
from calculator import Subtract as Minus
3838
```
3939

40-
Import the `hello` function from `my_module` as `hey` and call it to print your name.
40+
Import the `Calculator` class from `calculator` and create an instance of this class. Remember how to access it correctly in
41+
this case.
4142

42-
<div class='hint'>Use syntax such as <code>from some_module import some_func as func</code>.</div>
43-
<div class="hint">Note: The <code>hey</code> function should be called without a prefix. The function expects
44-
one string argument.</div>
43+
44+
<div class="hint">Note: The <code>Calculator</code> class should be called without a prefix because you
45+
imported it directly.</div>

‎Modules and packages/From import/tests.py

Lines changed: 0 additions & 23 deletions
This file was deleted.

‎Modules and packages/From import/tests/__init__.py

Whitespace-only changes.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import unittest
2+
import contextlib
3+
import io
4+
5+
f = io.StringIO()
6+
try:
7+
with contextlib.redirect_stdout(f):
8+
import from_import
9+
output = f.getvalue().split('\n')
10+
11+
12+
class TestCase(unittest.TestCase):
13+
def test_output(self):
14+
self.assertEqual('Hey there, User!', output[0],
15+
msg='Please do not change the starter code.')
16+
self.assertEqual('66.66666666666667', output[1],
17+
msg='Calculation result is wrong. Please do not change the starter code.')
18+
19+
except NameError:
20+
class TestFailCase(unittest.TestCase):
21+
def test_fail(self):
22+
self.assertTrue(False, msg='Use `Calculator` directly, without a prefix.')
23+
24+
25+

0 commit comments

Comments
 (0)
Please sign in to comment.