8000 `Object-oriented`: Add simpler examples, update quiz and exercises (#… · efmrpsat/python-tutorial@9240a6d · GitHub
[go: up one dir, main page]

Skip to content 8000

Commit 9240a6d

Browse files
despadamedoardob90Snowwpandapre-commit-ci[bot]
authored
Object-oriented: Add simpler examples, update quiz and exercises (empa-scientific-it#249)
* add 2 new examples * finish new examples * finish new examples pt2 * update new examples with docstrings and improved tests * add docstrings * finish docstrings * make sure that class methods are only using the class attributes to produce their results * test that the return value is an instance of a custom class * use closure to check for usage of class attributes * update property section and quiz * Expand docstring description of Exercise 4 * finish updating the exercises * Minor changes * Stupid metadata fix of the ipython notebook * Fixing * Replacing variable l: E741 Ambiguous variable name: `l` * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: edoardob90 <edoardob90@gmail.com> Co-authored-by: Snowwpanda <pascal.su@empa.ch> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 1ce10a4 commit 9240a6d

8 files changed

+978
-283
lines changed

object_oriented_programming.ipynb

Lines changed: 558 additions & 244 deletions
Large diffs are not rendered by default.

tutorial/object_oriented_programming.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,37 +4,37 @@
44
class OopQuiz(Quiz):
55
def __init__(self, title=""):
66
q1 = Question(
7-
question="Based on what you learned about Python's special methods, which of the following statements is <strong>true</strong>?",
7+
question="Based on what you learned about Python's special methods, which statement best describes the relationship between <i>__str__</i> and <i>__repr__</i>?",
88
options={
9-
"__repr__ is also used for __str__, but not vice versa.": "Correct! This statement is true.",
10-
"__str__ is also used for __repr__, but not vice versa.": "The opposite is true.",
11-
"__repr__ and __str__ are completely independent.": "__repr__ is also used for __str__, but not vice versa.",
9+
"__repr__ is used as a fallback when __str__ is missing.": "Correct! When __str__ is not defined, Python will use __repr__ instead.",
10+
"__str__ is used as a fallback when __repr__ is missing.": "Think again based on the example we saw earlier.",
11+
"__repr__ and __str__ are independent methods with no relationship to each other.": "There is a relationship between the two methods. Which one could it be?",
1212
},
13-
correct_answer="__repr__ is also used for __str__, but not vice versa.",
13+
correct_answer="__repr__ is used as a fallback when __str__ is missing.",
1414
hint="",
1515
shuffle=True,
1616
)
1717

1818
q2 = Question(
1919
question="Based on what you learned about Python's comparison methods, which of the following statements is <strong>false</strong>?",
2020
options={
21-
"If we implement __gt__, Python will also use it for __lt__": "This statement is true.",
22-
"If we implement __lt__, Python will also use it for __le__": "Correct! This statement is false.",
23-
"If we implement __eq__, Python will also use it for __ne__": "This statement is true.",
21+
"If we implement __gt__, Python will also use it for __lt__": "Wrong! This statement is true because Python is able to cleverly swap the comparison terms.",
22+
"If we implement __lt__, Python will also use it for __le__": "Correct! This statement is false because Python has no knowledge of what equality could mean based just on a comparison.",
23+
"If we implement __eq__, Python will also use it for __ne__": "Wrong! This statement is true because Python is able to cleverly negate the equality comparison.",
2424
},
2525
correct_answer="If we implement __lt__, Python will also use it for __le__",
2626
hint="",
2727
shuffle=True,
2828
)
2929

3030
q3 = Question(
31-
question="Based on what you learned about the @property keyword, which of the following statements is <strong>false</strong>?",
31+
question="Based on what you learned about the <i>@property</i> keyword, which of the following statements is <strong>true</strong>?",
3232
options={
33-
"@property creates attributes that act like methods but can be accessed and assigned as regular attributes.": "This statement is true.",
34-
"@property helps implement attributes that require additional logic or validation when getting or setting their values.": "This statement is true.",
35-
"@property makes code more readable but restricts dynamic attibute behaviour.": "Correct! This statement is false.",
33+
"@property creates attributes that act like methods, which means that they need to be called as regular methods.": "Wrong! This statement is false beacuse we access these attributes as regular ones.",
34+
"@property helps implement attributes that require additional logic or validation when calculating their values.": "Correct! This is how you can make your classes more readable and user-friendly.",
35+
"@property allows to get and set the values of attributes, while applying additional logic in the background.": "Wrong! This statement is false beacuse we are not allowed to directly set the values of these attributes.",
3636
},
37-
correct_answer="@property makes code more readable but restricts dynamic attibute behaviour.",
37+
correct_answer="@property helps implement attributes that require additional logic or validation when calculating their values.",
3838
hint="",
3939
shuffle=True,
4040
)

tutorial/tests/test_functions.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,9 @@ def test_password_validator2(start: int, end: int, function_to_test) -> None:
297297
# Exercise: Buckets reorganization
298298
#
299299

300-
prio = {l: i for i, l in enumerate(ascii_lowercase + ascii_uppercase, start=1)}
300+
prio = {
301+
letter: i for i, letter in enumerate(ascii_lowercase + ascii_uppercase, start=1)
302+
}
301303
buckets_1, buckets_2 = (read_data(f"buckets_{num}.txt") for num in (1, 2))
302304

303305

tutorial/tests/test_functions_advanced.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,10 @@ def hello(name):
144144
def reference_once(allowed_time: int = 15) -> t.Callable:
145145
"""Decorator to run a function at most once"""
146146

147+
class TooSoonError(RuntimeError):
148+
def __init__(self, wait: float):
149+
super().__init__(f"Wait another {wait:.2f} seconds")
150+
147151
def decorator(func: t.Callable) -> t.Callable:
148152
timer = 0.0
149153

@@ -156,9 +160,7 @@ def wrapper(*args, **kwargs) -> t.Any:
156160
return func(*args, **kwargs)
157161

158162
if (stop := time.perf_counter()) - timer < allowed_time:
159-
raise RuntimeError(
160-
f"Wait another {allowed_time - (stop - timer):.2f} seconds"
161-
)
163+
raise TooSoonError(allowed_time - (stop - timer))
162164

163165
timer = time.perf_counter()
164166

tutorial/tests/test_library_scipy.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ def reference_lu():
3939
# 1. TODO: define the matrix a_ref here:
4040
a_ref = np.array([[9, 3, 3], [3, 2, 2], [3, 4, 2]])
4141
# 2. TODO: call the lu function here:
42-
p, l, u = lu(a_ref)
42+
p_matrix, l_matrix, u_matrix = lu(a_ref)
4343

4444
# 3. TODO: return p, l, u matrices in this order here:
45-
return p, l, u
45+
return p_matrix, l_matrix, u_matrix
4646

4747

4848
def test_lu(function_to_test):

0 commit comments

Comments
 (0)
0