From da1aa636f755c72e469dec65953aa77f9b6c230e Mon Sep 17 00:00:00 2001 From: Konstantin Chaika Date: Fri, 10 May 2024 11:06:28 +0400 Subject: [PATCH 1/5] Improvements of Modules lesson according to feedback --- Modules and packages/Built-in modules/task.md | 2 +- .../From import/task-info.yaml | 8 +-- .../Import module 2/__init__.py | 0 .../calculator.py | 0 .../Import module 2/imports.py | 11 ++++ .../Import module 2/task-info.yaml | 23 ++++++++ .../Import module 2/task-remote-info.yaml | 1 + Modules and packages/Import module 2/task.md | 15 +++++ .../Import module 2/tests/__init__.py | 0 .../Import module 2/tests/test_task.py | 33 +++++++++++ Modules and packages/Import module/imports.py | 16 +----- .../{my_module.py => my_funcs.py} | 0 .../Import module/task-info.yaml | 34 +++++------ Modules and packages/Import module/task.md | 10 +--- .../Import module/tests/test_task.py | 21 +------ Modules and packages/Packages/task-info.yaml | 56 +++++++++---------- Modules and packages/Packages/task.md | 2 + Modules and packages/lesson-info.yaml | 11 ++-- 18 files changed, 144 insertions(+), 99 deletions(-) create mode 100644 Modules and packages/Import module 2/__init__.py rename Modules and packages/{Import module => Import module 2}/calculator.py (100%) create mode 100644 Modules and packages/Import module 2/imports.py create mode 100644 Modules and packages/Import module 2/task-info.yaml create mode 100644 Modules and packages/Import module 2/task-remote-info.yaml create mode 100644 Modules and packages/Import module 2/task.md create mode 100644 Modules and packages/Import module 2/tests/__init__.py create mode 100644 Modules and packages/Import module 2/tests/test_task.py rename Modules and packages/Import module/{my_module.py => my_funcs.py} (100%) diff --git a/Modules and packages/Built-in modules/task.md b/Modules and packages/Built-in modules/task.md index d00df945..04d9b50d 100644 --- a/Modules and packages/Built-in modules/task.md +++ b/Modules and packages/Built-in modules/task.md @@ -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 here. 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). diff --git a/Modules and packages/From import/task-info.yaml b/Modules and packages/From import/task-info.yaml index 6f0ed1d0..21f14c53 100644 --- a/Modules and packages/From import/task-info.yaml +++ b/Modules and packages/From import/task-info.yaml @@ -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: @@ -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 diff --git a/Modules and packages/Import module 2/__init__.py b/Modules and packages/Import module 2/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/Modules and packages/Import module/calculator.py b/Modules and packages/Import module 2/calculator.py similarity index 100% rename from Modules and packages/Import module/calculator.py rename to Modules and packages/Import module 2/calculator.py diff --git a/Modules and packages/Import module 2/imports.py b/Modules and packages/Import module 2/imports.py new file mode 100644 index 00000000..bbb371a8 --- /dev/null +++ b/Modules and packages/Import module 2/imports.py @@ -0,0 +1,11 @@ +import calculator + +calc = calculator.Calculator() +for i in range(100): + calc.add(i) + +print(calc.get_current()) + + + + diff --git a/Modules and packages/Import module 2/task-info.yaml b/Modules and packages/Import module 2/task-info.yaml new file mode 100644 index 00000000..35c7f2b8 --- /dev/null +++ b/Modules and packages/Import module 2/task-info.yaml @@ -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 diff --git a/Modules and packages/Import module 2/task-remote-info.yaml b/Modules and packages/Import module 2/task-remote-info.yaml new file mode 100644 index 00000000..de8a0b93 --- /dev/null +++ b/Modules and packages/Import module 2/task-remote-info.yaml @@ -0,0 +1 @@ +id: 1396640314 diff --git a/Modules and packages/Import module 2/task.md b/Modules and packages/Import module 2/task.md new file mode 100644 index 00000000..5d8726c5 --- /dev/null +++ b/Modules and packages/Import module 2/task.md @@ -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. + +
Use the import keyword and the calculator reference.
+
Access the function from the module using syntax such as module.function().
+
Don't forget to provide the function with an argument.
+ diff --git a/Modules and packages/Import module 2/tests/__init__.py b/Modules and packages/Import module 2/tests/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/Modules and packages/Import module 2/tests/test_task.py b/Modules and packages/Import module 2/tests/test_task.py new file mode 100644 index 00000000..ea719359 --- /dev/null +++ b/Modules and packages/Import module 2/tests/test_task.py @@ -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.") diff --git a/Modules and packages/Import module/imports.py b/Modules and packages/Import module/imports.py index 75864551..d1d632bd 100644 --- a/Modules and packages/Import module/imports.py +++ b/Modules and packages/Import module/imports.py @@ -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") diff --git a/Modules and packages/Import module/my_module.py b/Modules and packages/Import module/my_funcs.py similarity index 100% rename from Modules and packages/Import module/my_module.py rename to Modules and packages/Import module/my_funcs.py diff --git a/Modules and packages/Import module/task-info.yaml b/Modules and packages/Import module/task-info.yaml index 9c4f57ff..4f5c8f76 100644 --- a/Modules and packages/Import module/task-info.yaml +++ b/Modules and packages/Import module/task-info.yaml @@ -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 \ No newline at end of file + - 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 diff --git a/Modules and packages/Import module/task.md b/Modules and packages/Import module/task.md index caae4de7..8361d5d8 100644 --- a/Modules and packages/Import module/task.md +++ b/Modules and packages/Import module/task.md @@ -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"` -
Use the import keyword and the calculator reference.
Access the function from the module using syntax such as module.function().
Don't forget to provide the function with an argument.
diff --git a/Modules and packages/Import module/tests/test_task.py b/Modules and packages/Import module/tests/test_task.py index 739b8bfa..00a00829 100644 --- a/Modules and packages/Import module/tests/test_task.py +++ b/Modules and packages/Import module/tests/test_task.py @@ -2,7 +2,6 @@ import contextlib import io import re -import calculator f = io.StringIO() try: @@ -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.') diff --git a/Modules and packages/Packages/task-info.yaml b/Modules and packages/Packages/task-info.yaml index dd16498d..d7787888 100644 --- a/Modules and packages/Packages/task-info.yaml +++ b/Modules and packages/Packages/task-info.yaml @@ -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 \ No newline at end of file + - 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 diff --git a/Modules and packages/Packages/task.md b/Modules and packages/Packages/task.md index c2dbf4ee..f33825b3 100644 --- a/Modules and packages/Packages/task.md +++ b/Modules and packages/Packages/task.md @@ -43,6 +43,8 @@ You can learn more about packages by reading here. 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). diff --git a/Modules and packages/Import_module/task-info.yaml b/Modules and packages/Import_module/task-info.yaml index a33d3d7f..49a4c360 100644 --- a/Modules and packages/Import_module/task-info.yaml +++ b/Modules and packages/Import_module/task-info.yaml @@ -6,7 +6,7 @@ files: placeholders: - offset: 26 length: 19 - placeholder_text: '# call hello_world function from the my_funcs module' + placeholder_text: '# call the hello_world function from the my_funcs module' - name: my_funcs.py visible: true - name: tests/__init__.py diff --git a/Modules and packages/Import_module/task.md b/Modules and packages/Import_module/task.md index c3745d46..3f3de76f 100644 --- a/Modules and packages/Import_module/task.md +++ b/Modules and packages/Import_module/task.md @@ -20,8 +20,8 @@ my_funcs.func1() 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, you have already imported module `my_funcs`. -Call the function `hello_world` from this module with argument `"John"` +In the code editor, you have already imported the module `my_funcs`. +Call the function `hello_world` from this module with the argument `"John"`.
Access the function from the module using syntax such as module.function().
Don't forget to provide the function with an argument.
diff --git a/Modules and packages/Import_module/tests/test_task.py b/Modules and packages/Import_module/tests/test_task.py index 00a00829..0afa0e9a 100644 --- a/Modules and packages/Import_module/tests/test_task.py +++ b/Modules and packages/Import_module/tests/test_task.py @@ -12,9 +12,9 @@ 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 "John" argument') + 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 hello_world function from my_funcs module.') + self.assertTrue(False, msg='You need to use the hello_world function from the my_funcs module.') diff --git a/Modules and packages/Import_module_part_two/task-info.yaml b/Modules and packages/Import_module_part_two/task-info.yaml index 5f565b3d..35f61f15 100644 --- a/Modules and packages/Import_module_part_two/task-info.yaml +++ b/Modules and packages/Import_module_part_two/task-info.yaml @@ -6,13 +6,13 @@ files: placeholders: - offset: 0 length: 17 - placeholder_text: '# Import the module `calculator` here' + placeholder_text: '# Import the `calculator` module here' - offset: 26 length: 23 placeholder_text: '''Create a new instance of Calculator class defined in calculator''' - offset: 75 length: 11 - placeholder_text: '# Use Calculator method `add` to add `i` to the current value.' + placeholder_text: '# Use the Calculator method `add` to add `i` to the current value.' - name: calculator.py visible: true - name: tests/test_task.py diff --git a/Modules and packages/Import_module_part_two/task.md b/Modules and packages/Import_module_part_two/task.md index 5d8726c5..19e9d121 100644 --- a/Modules and packages/Import_module_part_two/task.md +++ b/Modules and packages/Import_module_part_two/task.md @@ -1,12 +1,12 @@ ## 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 +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 find out more about modules in Python by reading [this section](https://docs.python.org/3/tutorial/modules.html) of The Python Tutorial. +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 module `calculator` and create an instance of the class `Calculator` (`calc`). +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.
Use the import keyword and the calculator reference.
diff --git a/Modules and packages/Packages/task.md b/Modules and packages/Packages/task.md index 5fa42481..c4bcf4c5 100644 --- a/Modules and packages/Packages/task.md +++ b/Modules and packages/Packages/task.md @@ -43,7 +43,7 @@ You can learn more about packages by reading