An object were callbacks can be registered and called
| 33 | |
| 34 | |
| 35 | class Caller(): |
| 36 | """ An object were callbacks can be registered and called """ |
| 37 | |
| 38 | def __init__(self): |
| 39 | """ Create the object """ |
| 40 | self.callbacks = [] |
| 41 | |
| 42 | def add_callback(self, cb): |
| 43 | """ Register cb as a new callback. Will not register duplicates. """ |
| 44 | if ((cb in self.callbacks) is False): |
| 45 | self.callbacks.append(cb) |
| 46 | |
| 47 | def remove_callback(self, cb): |
| 48 | """ Un-register cb from the callbacks """ |
| 49 | self.callbacks.remove(cb) |
| 50 | |
| 51 | def call(self, *args): |
| 52 | """ Call the callbacks registered with the arguments args """ |
| 53 | copy_of_callbacks = list(self.callbacks) |
| 54 | for cb in copy_of_callbacks: |
| 55 | cb(*args) |