Last active
December 10, 2015 01:38
-
-
Save jreisstudio/4359884 to your computer and use it in GitHub Desktop.
Python Observer
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
from Subscriber import * | |
class Observer: | |
subscribersList = [] | |
def subscribe(self,subscriber): | |
try: | |
if self.isAllowedSubscriber(subscriber): | |
if self.uniqueIdValidation(subscriber.id): | |
self.subscribersList.append(subscriber) | |
return True | |
else: | |
return False; | |
else: | |
return False | |
except Exception as ex: | |
print ex; | |
return False | |
def unsubscribe(self,id): | |
try: | |
for subscriber in self.subscribersList: | |
if subscriber.id ==id: | |
self.subscribersList.remove(subscriber) | |
return True | |
except Exception as ex: | |
print ex; | |
return False | |
def publish(self, publishNotification): | |
for subscriber in self.subscribersList: | |
subscriber.receiveNotification(publishNotification) | |
def isAllowedSubscriber(self,subscriber): | |
return isinstance(subscriber,Subscriber) | |
def uniqueIdValidation(self,id): | |
if len(self.subscribersList) !=0: | |
for subscriber in self.subscribersList: | |
if subscriber.id == id: | |
return False | |
else: | |
return True | |
else: | |
return True |
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
from Subscriber import * | |
from Observer import * | |
class ObserverTests: | |
SUCCESS_MESSAGE="OK" | |
ERROR_MESSAGE = "ERROR" | |
observer = Observer() | |
def test_addAllowedSubscriber(self): | |
try: | |
subscriber = Subscriber(); | |
subscriber.id = "sub_01" | |
isSubscriberAdded = self.observer.subscribe(subscriber) | |
if isSubscriberAdded & len(self.observer.subscribersList)>0: | |
return self.SUCCESS_MESSAGE | |
else: | |
return self.ERROR_MESSAGE | |
except Exception as ex: | |
print ex | |
return self.ERROR_MESSAGE | |
def test_addAllowedSubscriberTwice(self): | |
try: | |
subscriber = Subscriber(); | |
subscriber.id = "sub_01" | |
subscriber2 = Subscriber(); | |
subscriber2.id = "sub_01" | |
isSubscriberAdded_1 = self.observer.subscribe(subscriber) | |
isSubscriberAdded_2 = self.observer.subscribe(subscriber2) | |
if (not isSubscriberAdded_2) & len(self.observer.subscribersList)==1: | |
return self.SUCCESS_MESSAGE | |
else: | |
return self.ERROR_MESSAGE | |
except Exception as ex: | |
print ex | |
return self.ERROR_MESSAGE | |
def test_addNotAllowedSubscriber(self): | |
try: | |
subscriberFalse = "sub_02" | |
isSubscriberAdded = self.observer.subscribe(subscriberFalse) | |
if (not isSubscriberAdded) & len(self.observer.subscribersList)==1: | |
return self.SUCCESS_MESSAGE | |
else: | |
return self.ERROR_MESSAGE | |
except Exception as ex: | |
print ex | |
return self.ERROR_MESSAGE | |
def test_RemoveSubscriber(self): | |
try: | |
subscriber = Subscriber(); | |
subscriber.id = "sub_01" | |
self.observer.subscribe(subscriber) | |
isSubscriberRemoved =self.observer.unsubscribe("sub_01") | |
if isSubscriberRemoved & len(self.observer.subscribersList)==0: | |
return self.SUCCESS_MESSAGE | |
else: | |
return self.ERROR_MESSAGE | |
except Exception as ex: | |
print ex | |
return self.ERROR_MESSAGE | |
def test_publish(self): | |
try: | |
subscriber = Subscriber(); | |
subscriber.id = "sub_01" | |
subscriber2 = Subscriber(); | |
subscriber2.id = "sub_02" | |
self.observer.subscribe(subscriber) | |
self.observer.subscribe(subscriber2) | |
notification= "notification!" | |
self.observer.publish(notification) | |
return self.SUCCESS_MESSAGE | |
except Exception as ex: | |
print ex | |
return self.ERROR_MESSAGE | |
observerTests = ObserverTests() | |
result={} | |
result["add allowed Subscriber"]= observerTests.test_addAllowedSubscriber() | |
result["add not allowed Subscriber"]= observerTests.test_addNotAllowedSubscriber() | |
result["Adding a subscriber with an existing id "] = observerTests.test_addAllowedSubscriberTwice() | |
result["remove Subscriber"]= observerTests.test_RemoveSubscriber() | |
result["publish"]= observerTests.test_publish() | |
print "Test results:------------------------" | |
for key,value in result.iteritems(): | |
print key,"---" ,value; | |
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 Subscriber: | |
id="" | |
def receiveNotification(self,publishNotification): | |
#Do something with the publish notification... | |
#here you can put some default business rule, that aplies for all Subscribers | |
print self.id,"-",publishNotification |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment