Created
May 16, 2014 13:17
-
-
Save mottosso/d742e0e8d446b580c36b to your computer and use it in GitHub Desktop.
Request Pattern - Strong
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Request(object): | |
def __call__(self, *args, **kwargs): | |
return self.request_first(*args, **kwargs) | |
def __init__(self, **kwargs): | |
self.__slots = [] | |
def request(self, *args, **kwargs): | |
"""Return generator, yielding one return value per slot""" | |
for subs in self.__slots: | |
yield subs(*args, **kwargs) | |
def request_first(self, *args, **kwargs): | |
"""Return first available result""" | |
try: | |
return next(self.request(*args, **kwargs)) | |
except StopIteration: | |
return None | |
return None | |
def connect(self, func): | |
self.__slots.append(func) | |
def disconnect(self, func): | |
try: | |
self.__slots.remove(func) | |
except ValueError: |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment