Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 4f1f48b

Browse files
committedJun 17, 2021
Added Packages task
1 parent e08b4f9 commit 4f1f48b

File tree

15 files changed

+158
-1
lines changed

15 files changed

+158
-1
lines changed
 

‎Modules and packages/Builtin modules/task.md

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

2323
Remember that you can use &shortcut:CodeCompletion; after a dot (.) to explore available
24-
methods of a module.
24+
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
Print the current date using an imported built-in module `datetime`.
2727

‎Modules and packages/Packages/__init__.py

Whitespace-only changes.

‎Modules and packages/Packages/classes/__init__.py

Whitespace-only changes.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
"""
2+
This module contains calculator classes
3+
"""
4+
5+
6+
class Add:
7+
def __init__(self):
8+
self.current = 0
9+
10+
def add(self, amount):
11+
self.current += amount
12+
13+
def get_current(self):
14+
return self.current
15+
16+
17+
class Subtract:
18+
def __init__(self):
19+
self.current = 100
20+
21+
def subtract(self, amount):
22+
self.current -= amount
23+
24+
def get_current(self):
25+
return self.current
26+
27+
28+
class Multiply:
29+
def __init__(self):
30+
self.current = 10
31+
32+
def multiply(self, amount):
33+
self.current *= amount
34+
35+
def get_current(self):
36+
return self.current

‎Modules and packages/Packages/functions/__init__.py

Whitespace-only changes.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
""" documentation string for module my_module
2+
This module contains good_bye function
3+
"""
4+
5+
6+
def good_bye(name):
7+
return f"See you later, {name}!"

‎Modules and packages/Packages/functions/greeting/__init__.py

Whitespace-only changes.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
""" documentation string for module my_module
2+
This module contains hello function
3+
"""
4+
5+
6+
def hello(name):
7+
return f"Hey there, {name}!"
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
""" documentation string for module my_module
2+
This module contains official hello function
3+
"""
4+
5+
6+
def hello(name):
7+
return f"Dear {name}, I am glad to finally meet you in person."
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
type: edu
2+
files:
3+
- name: task.py
4+
visible: true
5+
placeholders:
6+
- offset: 95
7+
length: 46
8+
placeholder_text: '# import the official module as official'
9+
- name: tests.py
10+
visible: false
11+
- name: functions/greeting/hello.py
12+
visible: true
13+
- name: functions/goodbye.py
14+
visible: true
15+
- name: classes/calculator.py
16+
visible: true
17+
- name: classes/__init__.py
18+
visible: true
19+
- name: functions/__init__.py
20+
visible: true
21+
- name: functions/greeting/__init__.py
22+
visible: true
23+
- name: functions/greeting/official.py
24+
visible: true

‎Modules and packages/Packages/task.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
## Packages
2+
3+
Packages are a way of structuring Python’s module namespace by using “dotted module
4+
names”. For example, the module name `A.B` designates a submodule named `B` in a package
5+
named `A`. Just like the use of modules saves the authors of different modules from
6+
having to worry about each other’s global variable names, the use of dotted module
7+
names saves the authors of multi-module packages like [NumPy](https://numpy.org/)
8+
or [Pillow](https://pypi.org/project/Pillow/) from having to worry about each other’s
9+
module names.
10+
11+
<div class="hint">The `__init__.py` files are required to make Python treat directories
12+
containing the file as packages. This prevents directories with a common name, such
13+
as string, unintentionally hiding valid modules that occur later on the module search
14+
path. In the simplest case, `__init__.py` can just be an empty file, but it can also
15+
execute initialization code for the package or set the `__all__` variable, described
16+
later.</div>
17+
18+
Check out the packages `functions` and `classes` we created. Users of the packages
19+
can import individual modules from the package, for example:
20+
21+
```python
22+
import functions.greeting.hello
23+
```
24+
25+
This loads the submodule `functions.greeting.hello`. It must be referenced with its full name:
26+
27+
```python
28+
functions.greeting.hello.hello('Susan')
29+
```
30+
An alternative way of importing the submodule is:
31+
32+
```python
33+
from functions.greeting import hello
34+
```
35+
36+
This also loads the submodule echo, and makes it available without its package prefix,
37+
so it can be used as follows:
38+
39+
```python
40+
hello.hello('Susan')
41+
```
42+
43+
In the code editor, import the `official` module properly to make the last `print`
44+
statement work.
45+
46+
<div class="hint">Access the module using syntax such as <code>package.subpackage.module</code>.</div>
47+
<div class="hint">Use syntax such as <code>import module as something</code>.</div>

‎Modules and packages/Packages/task.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import functions.goodbye as bye
2+
import functions.greeting.hello
3+
from classes import calculator
4+
import functions.greeting.official as official
5+
6+
7+
print(functions.greeting.hello.hello('Susan'))
8+
bye.good_bye('Alex')
9+
c = calculator.Multiply()
10+
c.multiply(2)
11+
print(c.get_current())
12+
13+
print(official.hello('Sam'))
14+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from test_helper import run_common_tests, failed, passed, get_answer_placeholders
2+
3+
4+
def test_window():
5+
window = get_answer_placeholders()[0]
6+
if "import functions.greeting.official as official" in window:
7+
passed()
8+
else:
9+
failed("Wrong import, check out the hints!")
10+
11+
12+
if __name__ == '__main__':
13+
run_common_tests()
14+
test_window()

‎Modules and packages/__init__.py

Whitespace-only changes.

‎Modules and packages/lesson-info.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ content:
22
- Import module
33
- Builtin modules
44
- From import
5+
- Packages

0 commit comments

Comments
 (0)
Please sign in to comment.