Skip to content

Commit

Permalink
Improvements of Modules lesson according to feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
kochaika committed May 10, 2024
1 parent 8e03d82 commit da1aa63
Show file tree
Hide file tree
Showing 18 changed files with 144 additions and 99 deletions.
2 changes: 1 addition & 1 deletion Modules and packages/Built-in modules/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ secondary prompts if the interpreter is in the interactive mode:
The variable `sys.path` is a list of strings that determines the interpreter’s search path
for modules: see what it prints for you when you run the code of the task.

Remember that you can use &shortcut:CodeCompletion; after a dot (.) to explore available
Remember that you can use &shortcut:CodeCompletion; shortcut after a dot (.) to explore available
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>.

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).
Expand Down
8 changes: 4 additions & 4 deletions Modules and packages/From import/task-info.yaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
type: edu
files:
- name: my_module.py
visible: true
- name: calculator.py
visible: true
- name: from_import.py
visible: true
placeholders:
Expand All @@ -13,6 +9,10 @@ files:
- offset: 97
length: 12
placeholder_text: '''Instantiate a calculator'''
- name: my_module.py
visible: true
- name: calculator.py
visible: true
- name: tests/__init__.py
visible: false
- name: tests/test_task.py
Expand Down
Empty file.
11 changes: 11 additions & 0 deletions Modules and packages/Import module 2/imports.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import calculator

calc = calculator.Calculator()
for i in range(100):
calc.add(i)

print(calc.get_current())




23 changes: 23 additions & 0 deletions Modules and packages/Import module 2/task-info.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
type: edu
files:
- name: imports.py
visible: true
placeholders:
- offset: 0
length: 17
placeholder_text: '# Import the module `calculator` here'
- offset: 75
length: 11
placeholder_text: '# Use Calculator method `add` to add `i` to the current value.'
- offset: 26
length: 23
placeholder_text: '''Create a new instance of Calculator class defined in calculator'''
- name: calculator.py
visible: true
- name: tests/test_task.py
visible: false
- name: __init__.py
visible: false
- name: tests/__init__.py
visible: false
feedback_link: https://docs.google.com/forms/d/e/1FAIpQLSfRlDlldKfuq-cHMNFfHMER61P1PRIan7KG6yp1GvaweDI7GA/viewform?usp=pp_url&entry.2103429047=Modules+and+Packages+/+Import+module+2
1 change: 1 addition & 0 deletions Modules and packages/Import module 2/task-remote-info.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
id: 1396640314
15 changes: 15 additions & 0 deletions Modules and packages/Import module 2/task.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
## Import module 2

You can import not only functions: you can import classes or even other modules. It is customary but not required to place all
import statements at the beginning of a module.

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.

### Task
In the code editor, import the module `calculator` and create an instance of the class `Calculator` (`calc`).
Use the `add` method defined in `Calculator` in a loop to add up numbers from 0 to 99.

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

Empty file.
33 changes: 33 additions & 0 deletions Modules and packages/Import module 2/tests/test_task.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import unittest
import contextlib
import io
import re
import calculator

f = io.StringIO()
try:
with contextlib.redirect_stdout(f):
from imports import *
output = f.getvalue().split('\n')

class TestCase(unittest.TestCase):
def test_class(self):
try:
self.assertTrue(isinstance(calc, calculator.Calculator),
msg='`calc` should be an instance of Calculator.')
except NameError:
self.assertTrue(False, msg='Do not change variable names.')

def test_out(self):
expected, actual = str(4950), output[0]
self.assertEqual(expected, actual, msg='Calculation result looks wrong.')

except NameError:
class TestFailCase(unittest.TestCase):
def test_fail(self):
self.assertTrue(False, msg='You need to import the calculator module.')

except ModuleNotFoundError:
class TestFailCase1(unittest.TestCase):
def test_fail(self):
self.assertTrue(False, msg="Don't use file extensions in imports.")
16 changes: 2 additions & 14 deletions Modules and packages/Import module/imports.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,3 @@
import my_module
import calculator

my_module.hello_world("John")


calc = calculator.Calculator()
for i in range(100):
calc.add(i)

print(calc.get_current())



import my_funcs

my_funcs.hello_world("John")
34 changes: 13 additions & 21 deletions Modules and packages/Import module/task-info.yaml
Original file line number Diff line number Diff line change
@@ -1,23 +1,15 @@
type: edu
files:
- name: my_module.py
visible: true
- name: imports.py
visible: true
placeholders:
- offset: 17
length: 17
placeholder_text: '# Import the module `calculator` here'
- offset: 124
length: 11
placeholder_text: '# Use Calculator method `add` to add `i` to the current value.'
- offset: 75
length: 23
placeholder_text: '''Create a new instance of Calculator class defined in calculator'''
- name: calculator.py
visible: true
- name: tests/__init__.py
visible: false
- name: tests/test_task.py
visible: false
feedback_link: https://docs.google.com/forms/d/e/1FAIpQLSfRlDlldKfuq-cHMNFfHMER61P1PRIan7KG6yp1GvaweDI7GA/viewform?usp=pp_url&entry.2103429047=Modules+and+Packages+/+Import+module
- name: imports.py
visible: true
placeholders:
- offset: 26
length: 19
placeholder_text: '# call hello_world function from the my_funcs module'
- name: my_funcs.py
visible: true
- name: tests/__init__.py
visible: false
- name: tests/test_task.py
visible: false
feedback_link: https://docs.google.com/forms/d/e/1FAIpQLSfRlDlldKfuq-cHMNFfHMER61P1PRIan7KG6yp1GvaweDI7GA/viewform?usp=pp_url&entry.2103429047=Modules+and+Packages+/+Import+module
10 changes: 2 additions & 8 deletions Modules and packages/Import module/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,13 @@ directly, but using the module name, you can now access the functions, for examp
```python
my_funcs.func1()
```

Modules can import other modules. It is customary but not required to place all
import statements at the beginning of a module.

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.

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

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

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

21 changes: 3 additions & 18 deletions Modules and packages/Import module/tests/test_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import contextlib
import io
import re
import calculator

f = io.StringIO()
try:
Expand All @@ -11,25 +10,11 @@
output = f.getvalue().split('\n')

class TestCase(unittest.TestCase):
def test_class(self):
try:
self.assertTrue(isinstance(calc, calculator.Calculator),
msg='`calc` should be an instance of Calculator.')
except NameError:
self.assertTrue(False, msg='Do not change variable names.')

def test_out(self):
expected, actual = 'Hello, World! My name is John', output[0]
self.assertEqual(expected, actual, msg='Please do not change the starter code.')
expected, actual = str(4950), output[1]
self.assertEqual(expected, actual, msg='Calculation result looks wrong.')
self.assertEqual(expected, actual, msg='Call hello_world with "John" argument')

except NameError:
except AttributeError:
class TestFailCase(unittest.TestCase):
def test_fail(self):
self.assertTrue(False, msg='You need to import the calculator module.')

except ModuleNotFoundError:
class TestFailCase1(unittest.TestCase):
def test_fail(self):
self.assertTrue(False, msg="Don't use file extensions in imports.")
self.assertTrue(False, msg='You need to use hello_world function from my_funcs module.')
56 changes: 28 additions & 28 deletions Modules and packages/Packages/task-info.yaml
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
type: edu
files:
- name: packages.py
visible: true
placeholders:
- offset: 95
length: 46
placeholder_text: '# Import the `official` module here'
- offset: 197
length: 20
placeholder_text: '''Say goodbye to Alex'''
- name: functions/greeting/hello.py
visible: true
- name: functions/goodbye.py
visible: true
- name: classes/calculator.py
visible: true
- name: classes/__init__.py
visible: true
- name: functions/__init__.py
visible: true
- name: functions/greeting/__init__.py
visible: true
- name: functions/greeting/official.py
visible: true
- name: tests/__init__.py
visible: false
- name: tests/test_task.py
visible: false
feedback_link: https://docs.google.com/forms/d/e/1FAIpQLSfRlDlldKfuq-cHMNFfHMER61P1PRIan7KG6yp1GvaweDI7GA/viewform?usp=pp_url&entry.2103429047=Modules+and+Packages+/+Packages
- name: packages.py
visible: true
placeholders:
- offset: 95
length: 46
placeholder_text: '# Import the `official` module here'
- offset: 197
length: 20
placeholder_text: '''Say goodbye to Alex'''
- name: functions/greeting/hello.py
visible: true
- name: functions/goodbye.py
visible: true
- name: classes/calculator.py
visible: true
- name: classes/__init__.py
visible: true
- name: functions/__init__.py
visible: true
- name: functions/greeting/__init__.py
visible: true
- name: functions/greeting/official.py
visible: true
- name: tests/__init__.py
visible: false
- name: tests/test_task.py
visible: false
feedback_link: https://docs.google.com/forms/d/e/1FAIpQLSfRlDlldKfuq-cHMNFfHMER61P1PRIan7KG6yp1GvaweDI7GA/viewform?usp=pp_url&entry.2103429047=Modules+and+Packages+/+Packages
2 changes: 2 additions & 0 deletions Modules and packages/Packages/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ You can learn more about packages by reading <a href="https://docs.python.org/3/
For more structured and detailed information, you can also refer to [this Hyperskill knowledge base page](https://hyperskill.org/learn/step/6384).

### Task
Look at the file structure in the `classes` and `functions` directories and in the subdirectories.

In the code editor, import the `official` module properly to make the last `print`
statement work.

Expand Down
11 changes: 6 additions & 5 deletions Modules and packages/lesson-info.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
content:
- Import module
- Built-in modules
- From import
- Packages
- Executing modules as scripts
- Import module
- Import module 2
- Built-in modules
- From import
- Packages
- Executing modules as scripts

0 comments on commit da1aa63

Please sign in to comment.