#!/usr/bin/python
# -*- coding: utf-8 -*-
import wx
class Observable(object):
"""
this class allows to inform every class attached to this of all the changes
occured during the program running
"""
def __init__(self):
"""
init: creates the observer list(empty)
"""
self.__observerList = []
self.__obj = (None, None)
def attach(self, observer):
"""
allows to attach to the instance of observable
"""
if observer in self.__observerList: return
self.__observerList.append(observer)
def detach(self, observer):
"""
allows to detach from the instance of observable
"""
if not observer in self.__observerList: return
self.__observerList.remove(observer)
def notify(self):
"""
called everytime a modify is occured and the other classes attached
have to be informed
"""
for observer in self.__observerList:
try:
observer.update()
except wx._core.PyDeadObjectError:
self.__observerList.remove(observer)
def set(self, obType, *obj):
"""
sets the values passed by the calling fuction in the "__obj" tuple
"""
self.__obj = (obType, obj)
self.notify()
self.__obj = (None, None)
def get(self):
"""
returns the values saved in the "__obj" tuple
"""
obj = self.__obj
return self.__obj
def getAttached(self):
"""
returns the observers list
"""
return self.__observerList
|