169 days ago · 6 min read1142 words · Tech · hide · Fediverse post

With the new event modifier, I think we can eliminate the button_pressed event. But first … I do like the notion of calling the modifier caller_id, so I think I’ll do that renaming. PyCharm helpfully renames throughout all the implementations of the protocol. But next … I don’t like this magic one_shot flag appearing in the public argument list: class PubSub: def subscribe(self, event, caller_id, callback, one_shot=False): key = (event, caller_id) subscription = Subscription(callback, one_shot) try: self.subscriptions[key].append(subscription) except KeyError: self.subscriptions[key] = [subscription] def subscribe_once(self, event, caller_id, callback): self.subscribe(event, '', callback, one_shot=True) Let’s extract a private method: def subscribe(self, event, caller_id, callback, one_shot=False): subscription = Subscription(callback, one_shot) self._store_subscription(subscription, event, caller_id) def _store_subscription(self, subscription, event, caller_id): key = (event,…

Commenting on Bubbles has been removed. Why? If you want to talk about this post, write to the author, use the comments on their blog if it has them, or reply with a "RE:" post on your own blog. Suggestions? feedback@bubbles.town