Skip to content

Commit

Permalink
Merge pull request #233 from jetbrains-academy/konstantin/modules_fee…
Browse files Browse the repository at this point in the history
…dback_improvements

Improvements of Modules lesson according to feedback
  • Loading branch information
kochaika authored Jul 31, 2024
2 parents 49cc31c + d6b1bad commit 1396208
Show file tree
Hide file tree
Showing 21 changed files with 123 additions and 76 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 the &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?utm_source=jba&utm_medium=jba_courses_links).
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
23 changes: 0 additions & 23 deletions Modules and packages/Import module/task-info.yaml

This file was deleted.

3 changes: 3 additions & 0 deletions Modules and packages/Import_module/imports.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import my_funcs

my_funcs.hello_world("John")
16 changes: 16 additions & 0 deletions Modules and packages/Import_module/task-info.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
type: edu
custom_name: Import module
files:
- name: imports.py
visible: true
placeholders:
- offset: 26
length: 19
placeholder_text: '# call the 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
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?utm_source=jba&utm_medium=jba_courses_links).

### 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 the module `my_funcs`.
Call the function `hello_world` from this module with the 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>

20 changes: 20 additions & 0 deletions Modules and packages/Import_module/tests/test_task.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import unittest
import contextlib
import io
import re

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

class TestCase(unittest.TestCase):
def test_out(self):
expected, actual = 'Hello, World! My name is John', output[0]
self.assertEqual(expected, actual, msg='Call hello_world with the "John" argument.')

except AttributeError:
class TestFailCase(unittest.TestCase):
def test_fail(self):
self.assertTrue(False, msg='You need to use the hello_world function from the my_funcs module.')
Empty file.
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import my_module
import calculator

my_module.hello_world("John")


calc = calculator.Calculator()
for i in range(100):
calc.add(i)
Expand Down
24 changes: 24 additions & 0 deletions Modules and packages/Import_module_part_two/task-info.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
type: edu
custom_name: Import module part 2
files:
- name: imports.py
visible: true
placeholders:
- offset: 0
length: 17
placeholder_text: '# Import the `calculator` module here'
- offset: 26
length: 23
placeholder_text: '#Create a new instance of the `Calculator` class defined in the `calculator` module'
- offset: 75
length: 11
placeholder_text: '# Use the Calculator method `add` to add `i` to the current value.'
- 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
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_part_two/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 but also classes and even other modules. It is customary, though not required, to place all
import statements at the beginning of a module.

You can learn 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 `calculator` module and create an instance of the `Calculator` class (`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.
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ def test_class(self):
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]
expected, actual = str(4950), output[0]
self.assertEqual(expected, actual, msg='Calculation result looks wrong.')

except NameError:
Expand Down
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?utm_source=jba&utm_medium=jba_courses_links).

### Task
Look at the file structure in the `classes` and `functions` directories and their 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_part_two
- Built-in modules
- From import
- Packages
- Executing modules as scripts

0 comments on commit 1396208

Please sign in to comment.