|
20 | 20 | https://docs.djangoproject.com/en/2.1/ref/request-response/#httprequest-objects
|
21 | 21 | """
|
22 | 22 |
|
| 23 | +from typing import Union |
| 24 | + |
23 | 25 |
|
24 | 26 | class HideFileCommand:
|
25 | 27 | """
|
26 | 28 | A command to hide a file given its name
|
27 | 29 | """
|
28 | 30 |
|
29 |
| - def __init__(self): |
| 31 | + def __init__(self) -> None: |
30 | 32 | # an array of files hidden, to undo them as needed
|
31 | 33 | self._hidden_files = []
|
32 | 34 |
|
33 |
| - def execute(self, filename): |
34 |
| - print(f'hiding {filename}') |
| 35 | + def execute(self, filename: str) -> None: |
| 36 | + print(f"hiding {filename}") |
35 | 37 | self._hidden_files.append(filename)
|
36 | 38 |
|
37 |
| - def undo(self): |
| 39 | + def undo(self) -> None: |
38 | 40 | filename = self._hidden_files.pop()
|
39 |
| - print(f'un-hiding {filename}') |
| 41 | + print(f"un-hiding {filename}") |
40 | 42 |
|
41 | 43 |
|
42 | 44 | class DeleteFileCommand:
|
43 | 45 | """
|
44 | 46 | A command to delete a file given its name
|
45 | 47 | """
|
46 | 48 |
|
47 |
| - def __init__(self): |
| 49 | + def __init__(self) -> None: |
48 | 50 | # an array of deleted files, to undo them as needed
|
49 | 51 | self._deleted_files = []
|
50 | 52 |
|
51 |
| - def execute(self, filename): |
52 |
| - print(f'deleting {filename}') |
| 53 | + def execute(self, filename: str) -> None: |
| 54 | + print(f"deleting {filename}") |
53 | 55 | self._deleted_files.append(filename)
|
54 | 56 |
|
55 |
| - def undo(self): |
| 57 | + def undo(self) -> None: |
56 | 58 | filename = self._deleted_files.pop()
|
57 |
| - print(f'restoring {filename}') |
| 59 | + print(f"restoring {filename}") |
58 | 60 |
|
59 | 61 |
|
60 | 62 | class MenuItem:
|
61 | 63 | """
|
62 | 64 | The invoker class. Here it is items in a menu.
|
63 | 65 | """
|
64 | 66 |
|
65 |
| - def __init__(self, command): |
| 67 | + def __init__(self, command: Union[HideFileCommand, DeleteFileCommand]) -> None: |
66 | 68 | self._command = command
|
67 | 69 |
|
68 |
| - def on_do_press(self, filename): |
| 70 | + def on_do_press(self, filename: str) -> None: |
69 | 71 | self._command.execute(filename)
|
70 | 72 |
|
71 |
| - def on_undo_press(self): |
| 73 | + def on_undo_press(self) -> None: |
72 | 74 | self._command.undo()
|
73 | 75 |
|
74 | 76 |
|
@@ -101,4 +103,5 @@ def main():
|
101 | 103 |
|
102 | 104 | if __name__ == "__main__":
|
103 | 105 | import doctest
|
| 106 | + |
104 | 107 | doctest.testmod()
|
0 commit comments