| 49 | |
| 50 | print("Example 1") |
| 51 | class Toaster: |
| 52 | def __init__(self, timer): |
| 53 | self.timer = timer |
| 54 | self.doneness = 3 |
| 55 | self.hot = False |
| 56 | |
| 57 | def _get_duration(self): |
| 58 | return max(0.1, min(120, self.doneness * 10)) |
| 59 | |
| 60 | def push_down(self): |
| 61 | if self.hot: |
| 62 | return |
| 63 | |
| 64 | self.hot = True |
| 65 | self.timer.countdown(self._get_duration(), self.pop_up) |
| 66 | |
| 67 | def pop_up(self): |
| 68 | print("Pop!") # Release the spring |
| 69 | self.hot = False |
| 70 | self.timer.end() |
| 71 | |
| 72 | |
| 73 | print("Example 2") |
no outgoing calls