-
Notifications
You must be signed in to change notification settings - Fork 0
/
exercise.py
43 lines (35 loc) · 1.64 KB
/
exercise.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
class Exercise:
def chapter_identifier(self):
raise NotImplementedError(
f"You must define a subchapter_identifier(self) for the class {self.__class__.__name__} to be able to use it in the app."
)
def __str__(self) -> str:
raise NotImplementedError(
f"You must define a __str__(self) for the class {self.__class__.__name__} to be able to use it in the app."
)
def render_body(self):
raise NotImplementedError(
f"You must define a render_body(self) for the class {self.__class__.__name__} to be able to use it in the app."
)
def enumerate(self):
return enumerate(self.subchapters)
def __getitem__(self, k):
assert type(self.subchapters) is dict
assert type(k) is str, f"cannot set at index {k} because a string is expected"
return self.subchapters[k]
def keys(self):
assert type(self.subchapters) is dict
return self.subchapters.keys()
def get(self, k):
assert type(self.subchapters) is dict
assert type(k) is str, f"cannot set at index {k} because a string is expected"
return self.subchapters.get(k, None)
def __setitem__(self, k, new_value):
assert type(self.subchapters) is dict
assert type(k) is str, f"cannot set at index {k} because a string is expected"
assert type(new_value) is not str
# if not issubclass(new_value.__class__, Exercise):
# raise Exception(f"cannot set value {new_value} because an Exercise subclass is expected")
self.subchapters[k] = new_value
def __init__(self) -> None:
self.subchapters = {}