Event Singleton Class How to use example: from .event import Event Event().profile_data_updated(400, 312)
| 1 | class Event: |
| 2 | """Event Singleton Class |
| 3 | |
| 4 | How to use example: |
| 5 | from .event import Event |
| 6 | Event().profile_data_updated(400, 312) |
| 7 | """ |
| 8 | |
| 9 | singleton = None |
| 10 | callbacks = dict() |
| 11 | |
| 12 | def __new__(cls, *args, **kwargs): |
| 13 | if not cls.singleton: |
| 14 | cls.singleton = object.__new__(Event) |
| 15 | return cls.singleton |
| 16 | |
| 17 | def __init__(self): |
| 18 | pass |
| 19 | |
| 20 | def fire_callbacks(self, function_name, *args, **kwargs): |
| 21 | if function_name not in self.callbacks: |
| 22 | return |
| 23 | for callback in self.callbacks[function_name]: |
| 24 | callback(*args, **kwargs) |
| 25 | |
| 26 | def add_callback(self, function_name, callback): |
| 27 | if function_name not in self.callbacks: |
| 28 | self.callbacks[function_name] = [] |
| 29 | |
| 30 | self.callbacks[function_name].append(callback) |
| 31 | |
| 32 | # place custom events below |
| 33 | def profile_data_updated(self, username, followers_count, following_count): |
| 34 | self.fire_callbacks( |
| 35 | self.profile_data_updated.__name__, |
| 36 | username, |
| 37 | followers_count, |
| 38 | following_count, |
| 39 | ) |
| 40 | |
| 41 | def commented(self, username): |
| 42 | self.fire_callbacks(self.commented.__name__, username) |
| 43 | |
| 44 | def liked(self, username): |
| 45 | self.fire_callbacks(self.liked.__name__, username) |
| 46 | |
| 47 | def followed(self, username): |
| 48 | self.fire_callbacks(self.followed.__name__, username) |
| 49 | |
| 50 | def unfollowed(self, username): |
| 51 | self.fire_callbacks(self.unfollowed.__name__, username) |
no outgoing calls
no test coverage detected