Skip to content

Commit da1aa63

Browse files
committedMay 10, 2024
Improvements of Modules lesson according to feedback
1 parent 8e03d82 commit da1aa63

File tree

18 files changed

+144
-99
lines changed

18 files changed

+144
-99
lines changed
 

‎Modules and packages/Built-in modules/task.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ secondary prompts if the interpreter is in the interactive mode:
2020
The variable `sys.path` is a list of strings that determines the interpreter’s search path
2121
for modules: see what it prints for you when you run the code of the task.
2222

23-
Remember that you can use &shortcut:CodeCompletion; after a dot (.) to explore available
23+
Remember that you can use &shortcut:CodeCompletion; shortcut after a dot (.) to explore available
2424
methods of a module. You can read more about standard modules <a href="https://docs.python.org/3/tutorial/modules.html#standard-modules">here</a>.
2525

2626
For more structured and detailed information, you can also refer to [this Hyperskill knowledge base page](https://hyperskill.org/learn/step/6019#built-in-modules).

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
type: edu
22
files:
3-
- name: my_module.py
4-
visible: true
5-
- name: calculator.py
6-
visible: true
73
- name: from_import.py
84
visible: true
95
placeholders:
@@ -13,6 +9,10 @@ files:
139
- offset: 97
1410
length: 12
1511
placeholder_text: '''Instantiate a calculator'''
12+
- name: my_module.py
13+
visible: true
14+
- name: calculator.py
15+
visible: true
1616
- name: tests/__init__.py
1717
visible: false
1818
- name: tests/test_task.py

‎Modules and packages/Import module 2/__init__.py

Whitespace-only changes.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import calculator
2+
3+
calc = calculator.Calculator()
4+
for i in range(100):
5+
calc.add(i)
6+
7+
print(calc.get_current())
8+
9+
10+
11+
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
type: edu
2+
files:
3+
- name: imports.py
4+
visible: true
5+
placeholders:
6+
- offset: 0
7+
length: 17
8+
placeholder_text: '# Import the module `calculator` here'
9+
- offset: 75
10+
length: 11
11+
placeholder_text: '# Use Calculator method `add` to add `i` to the current value.'
12+
- offset: 26
13+
length: 23
14+
placeholder_text: '''Create a new instance of Calculator class defined in calculator'''
15+
- name: calculator.py
16+
visible: true
17+
- name: tests/test_task.py
18+
visible: false
19+
- name: __init__.py
20+
visible: false
21+
- name: tests/__init__.py
22+
visible: false
23+
feedback_link: https://docs.google.com/forms/d/e/1FAIpQLSfRlDlldKfuq-cHMNFfHMER61P1PRIan7KG6yp1GvaweDI7GA/viewform?usp=pp_url&entry.2103429047=Modules+and+Packages+/+Import+module+2
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
id: 1396640314
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
## Import module 2
2+
3+
You can import not only functions: you can import classes or even other modules. It is customary but not required to place all
4+
import statements at the beginning of a module.
5+
6+
You can find out more about modules in Python by reading [this section](https://docs.python.org/3/tutorial/modules.html) of The Python Tutorial.
7+
8+
### Task
9+
In the code editor, import the module `calculator` and create an instance of the class `Calculator` (`calc`).
10+
Use the `add` method defined in `Calculator` in a loop to add up numbers from 0 to 99.
11+
12+
<div class='hint'>Use the <code>import</code> keyword and the <code>calculator</code> reference.</div>
13+
<div class='hint'>Access the function from the module using syntax such as <code>module.function()</code>.</div>
14+
<div class="hint">Don't forget to provide the function with an argument.</div>
15+

‎Modules and packages/Import module 2/tests/__init__.py

Whitespace-only changes.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import unittest
2+
import contextlib
3+
import io
4+
import re
5+
import calculator
6+
7+
f = io.StringIO()
8+
try:
9+
with contextlib.redirect_stdout(f):
10+
from imports import *
11+
output = f.getvalue().split('\n')
12+
13+
class TestCase(unittest.TestCase):
14+
def test_class(self):
15+
try:
16+
self.assertTrue(isinstance(calc, calculator.Calculator),
17+
msg='`calc` should be an instance of Calculator.')
18+
except NameError:
19+
self.assertTrue(False, msg='Do not change variable names.')
20+
21+
def test_out(self):
22+
expected, actual = str(4950), output[0]
23+
self.assertEqual(expected, actual, msg='Calculation result looks wrong.')
24+
25+
except NameError:
26+
class TestFailCase(unittest.TestCase):
27+
def test_fail(self):
28+
self.assertTrue(False, msg='You need to import the calculator module.')
29+
30+
except ModuleNotFoundError:
31+
class TestFailCase1(unittest.TestCase):
32+
def test_fail(self):
33+
self.assertTrue(False, msg="Don't use file extensions in imports.")
Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,3 @@
1-
import my_module
2-
import calculator
3-
4-
my_module.hello_world("John")
5-
6-
7-
calc = calculator.Calculator()
8-
for i in range(100):
9-
calc.add(i)
10-
11-
print(calc.get_current())
12-
13-
14-
1+
import my_funcs
152

3+
my_funcs.hello_world("John")
Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,15 @@
11
type: edu
22
files:
3-
- name: my_module.py
4-
visible: true
5-
- name: imports.py
6-
visible: true
7-
placeholders:
8-
- offset: 17
9-
length: 17
10-
placeholder_text: '# Import the module `calculator` here'
11-
- offset: 124
12-
length: 11
13-
placeholder_text: '# Use Calculator method `add` to add `i` to the current value.'
14-
- offset: 75
15-
length: 23
16-
placeholder_text: '''Create a new instance of Calculator class defined in calculator'''
17-
- name: calculator.py
18-
visible: true
19-
- name: tests/__init__.py
20-
visible: false
21-
- name: tests/test_task.py
22-
visible: false
23-
feedback_link: https://docs.google.com/forms/d/e/1FAIpQLSfRlDlldKfuq-cHMNFfHMER61P1PRIan7KG6yp1GvaweDI7GA/viewform?usp=pp_url&entry.2103429047=Modules+and+Packages+/+Import+module
3+
- name: imports.py
4+
visible: true
5+
placeholders:
6+
- offset: 26
7+
length: 19
8+
placeholder_text: '# call hello_world function from the my_funcs module'
9+
- name: my_funcs.py
10+
visible: true
11+
- name: tests/__init__.py
12+
visible: false
13+
- name: tests/test_task.py
14+
visible: false
15+
feedback_link: https://docs.google.com/forms/d/e/1FAIpQLSfRlDlldKfuq-cHMNFfHMER61P1PRIan7KG6yp1GvaweDI7GA/viewform?usp=pp_url&entry.2103429047=Modules+and+Packages+/+Import+module

‎Modules and packages/Import module/task.md

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,13 @@ directly, but using the module name, you can now access the functions, for examp
1616
```python
1717
my_funcs.func1()
1818
```
19-
20-
Modules can import other modules. It is customary but not required to place all
21-
import statements at the beginning of a module.
22-
23-
You can find out more about modules in Python by reading [this section](https://docs.python.org/3/tutorial/modules.html) of The Python Tutorial.
2419

2520
For more structured and detailed information, you can also refer to [this Hyperskill knowledge base page](https://hyperskill.org/learn/step/6019#module-loading).
2621

2722
### Task
28-
In the code editor, import the module `calculator` and create an instance of the class `Calculator` (`calc`).
29-
Use the `add` method defined in `Calculator` in a loop to add up numbers from 0 to 99.
23+
In the code editor, you have already imported module `my_funcs`.
24+
Call the function `hello_world` from this module with argument `"John"`
3025

31-
<div class='hint'>Use the <code>import</code> keyword and the <code>calculator</code> reference.</div>
3226
<div class='hint'>Access the function from the module using syntax such as <code>module.function()</code>.</div>
3327
<div class="hint">Don't forget to provide the function with an argument.</div>
3428

‎Modules and packages/Import module/tests/test_task.py

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import contextlib
33
import io
44
import re
5-
import calculator
65

76
f = io.StringIO()
87
try:
@@ -11,25 +10,11 @@
1110
output = f.getvalue().split('\n')
1211

1312
class TestCase(unittest.TestCase):
14-
def test_class(self):
15-
try:
16-
self.assertTrue(isinstance(calc, calculator.Calculator),
17-
msg='`calc` should be an instance of Calculator.')
18-
except NameError:
19-
self.assertTrue(False, msg='Do not change variable names.')
20-
2113
def test_out(self):
2214
expected, actual = 'Hello, World! My name is John', output[0]
23-
self.assertEqual(expected, actual, msg='Please do not change the starter code.')
24-
expected, actual = str(4950), output[1]
25-
self.assertEqual(expected, actual, msg='Calculation result looks wrong.')
15+
self.assertEqual(expected, actual, msg='Call hello_world with "John" argument')
2616

27-
except NameError:
17+
except AttributeError:
2818
class TestFailCase(unittest.TestCase):
2919
def test_fail(self):
30-
self.assertTrue(False, msg='You need to import the calculator module.')
31-
32-
except ModuleNotFoundError:
33-
class TestFailCase1(unittest.TestCase):
34-
def test_fail(self):
35-
self.assertTrue(False, msg="Don't use file extensions in imports.")
20+
self.assertTrue(False, msg='You need to use hello_world function from my_funcs module.')
Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,30 @@
11
type: edu
22
files:
3-
- name: packages.py
4-
visible: true
5-
placeholders:
6-
- offset: 95
7-
length: 46
8-
placeholder_text: '# Import the `official` module here'
9-
- offset: 197
10-
length: 20
11-
placeholder_text: '''Say goodbye to Alex'''
12-
- name: functions/greeting/hello.py
13-
visible: true
14-
- name: functions/goodbye.py
15-
visible: true
16-
- name: classes/calculator.py
17-
visible: true
18-
- name: classes/__init__.py
19-
visible: true
20-
- name: functions/__init__.py
21-
visible: true
22-
- name: functions/greeting/__init__.py
23-
visible: true
24-
- name: functions/greeting/official.py
25-
visible: true
26-
- name: tests/__init__.py
27-
visible: false
28-
- name: tests/test_task.py
29-
visible: false
30-
feedback_link: https://docs.google.com/forms/d/e/1FAIpQLSfRlDlldKfuq-cHMNFfHMER61P1PRIan7KG6yp1GvaweDI7GA/viewform?usp=pp_url&entry.2103429047=Modules+and+Packages+/+Packages
3+
- name: packages.py
4+
visible: true
5+
placeholders:
6+
- offset: 95
7+
length: 46
8+
placeholder_text: '# Import the `official` module here'
9+
- offset: 197
10+
length: 20
11+
placeholder_text: '''Say goodbye to Alex'''
12+
- name: functions/greeting/hello.py
13+
visible: true
14+
- name: functions/goodbye.py
15+
visible: true
16+
- name: classes/calculator.py
17+
visible: true
18+
- name: classes/__init__.py
19+
visible: true
20+
- name: functions/__init__.py
21+
visible: true
22+
- name: functions/greeting/__init__.py
23+
visible: true
24+
- name: functions/greeting/official.py
25+
visible: true
26+
- name: tests/__init__.py
27+
visible: false
28+
- name: tests/test_task.py
29+
visible: false
30+
feedback_link: https://docs.google.com/forms/d/e/1FAIpQLSfRlDlldKfuq-cHMNFfHMER61P1PRIan7KG6yp1GvaweDI7GA/viewform?usp=pp_url&entry.2103429047=Modules+and+Packages+/+Packages

‎Modules and packages/Packages/task.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ You can learn more about packages by reading <a href="https://docs.python.org/3/
4343
For more structured and detailed information, you can also refer to [this Hyperskill knowledge base page](https://hyperskill.org/learn/step/6384).
4444

4545
### Task
46+
Look at the file structure in the `classes` and `functions` directories and in the subdirectories.
47+
4648
In the code editor, import the `official` module properly to make the last `print`
4749
statement work.
4850

‎Modules and packages/lesson-info.yaml

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
content:
2-
- Import module
3-
- Built-in modules
4-
- From import
5-
- Packages
6-
- Executing modules as scripts
2+
- Import module
3+
- Import module 2
4+
- Built-in modules
5+
- From import
6+
- Packages
7+
- Executing modules as scripts

0 commit comments

Comments
 (0)