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 c80fa36

Browse files
committedNov 11, 2021
Updated tests and task in Packages
1 parent d4668ea commit c80fa36

File tree

8 files changed

+54
-41
lines changed

8 files changed

+54
-41
lines changed
 

‎Modules and packages/From import/calculator.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
This module contains calculator classes
2+
This module contains a calculator
33
"""
44

55

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,26 @@
11
"""
2-
This module contains calculator classes
2+
This module contains a calculator
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

‎Modules and packages/Packages/task.py renamed to ‎Modules and packages/Packages/packages.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@
55

66

77
print(functions.greeting.hello.hello('Susan'))
8-
bye.good_bye('Alex')
9-
c = calculator.Multiply()
10-
c.multiply(2)
8+
print(bye.good_bye('Alex'))
9+
10+
c = calculator.Calculator()
11+
c.add(2)
12+
c.multiply(10)
1113
print(c.get_current())
1214

1315
print(official.hello('Sam'))
14-

‎Modules and packages/Packages/task-info.yaml

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
type: edu
22
files:
3-
- name: task.py
3+
- name: packages.py
44
visible: true
55
placeholders:
66
- offset: 95
77
length: 46
8-
placeholder_text: '# Import the official module as official'
9-
- name: tests.py
10-
visible: false
8+
placeholder_text: '# Import the `official` module here'
9+
- offset: 197
10+
length: 20
11+
placeholder_text: '''Say goodbye to Alex'''
1112
- name: functions/greeting/hello.py
1213
visible: true
1314
- name: functions/goodbye.py
@@ -22,3 +23,7 @@ files:
2223
visible: true
2324
- name: functions/greeting/official.py
2425
visible: true
26+
- name: tests/__init__.py
27+
visible: false
28+
- name: tests/test_task.py
29+
visible: false

‎Modules and packages/Packages/task.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,9 @@ You can learn more about packages by reading <a href="https://docs.python.org/3/
4343
In the code editor, import the `official` module properly to make the last `print`
4444
statement work.
4545

46+
In the second print statement, add a function call (find the right function) so that it prints a goodbye to `'Alex'`.
47+
4648
<div class="hint">Access the module using syntax such as <code>package.subpackage.module</code>.</div>
4749
<div class="hint">Use syntax such as <code>import module as something</code>.</div>
50+
<div class="hint">Check out imports: there's one that might have the right function for the second task.
51+
Be careful when using it: the module is already imported with a specific name.</div>

‎Modules and packages/Packages/tests.py

Lines changed: 0 additions & 14 deletions
This file was deleted.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import unittest
2+
import contextlib
3+
import io
4+
5+
f = io.StringIO()
6+
7+
with contextlib.redirect_stdout(f):
8+
import packages
9+
output = f.getvalue().split('\n')
10+
11+
12+
class TestCase(unittest.TestCase):
13+
def test_alex(self):
14+
self.assertEqual('See you later, Alex!', output[1],
15+
msg='The second line of output should contain a goodbye to '
16+
'Alex produced by the function `good_bye` that can be found in one of the modules.')
17+
18+
def test_sam(self):
19+
self.assertEqual('Dear Sam, I am glad to finally meet you in person.', output[3],
20+
msg='The fourth line of output should contain an official greeting for Sam.')
21+
22+
def test_out_len(self):
23+
self.assertEqual(5, len(output), msg='Please do not remove or add any print statements')
24+
25+
26+
27+

0 commit comments

Comments
 (0)
Please sign in to comment.