-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improvements of Modules lesson according to feedback
- Loading branch information
Showing
18 changed files
with
144 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
id: 1396640314 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |