class DeamonObj: "Deamon class" # def dmnSet(self, timer, interval, method): # include info as tuple into timer list self.timerList.append((timer,interval,method)) # call kernel function that sets 'timer' to # execute 'method(self)' at specific 'interval' time print 'setting timer ' + timer # def dmnStart(self, timer): # call kernel function that starts 'timer' print 'starting timer ' + timer # def dmnStop(self, timer): # call kernel function that stops 'timer' print 'stopping timer ' + timer # def dmnReset(self, timer): # call kernel function that resets 'timer' print 'reseting timer ' + timer # def dmnLeft(self, timer): # call kernel function that tells how much time # has 'timer' to next activation print 'checking remaining time of ' + timer return 0 # # Applies timer's method to all linked objects # def dmnTrigger(self, timer): found = 0 for tmr in self.timerList: # find timer if timer == tmr[0]: found = 1 break if found == 1: for obj in self.linkedObjs: tmr[2](obj) # execute method # # Links an object to Deamon # def dmnLink(self, obj): self.linkedObjs.append(obj) # # Removes an object from Deamon # def dmnSplit(self, obj): self.linkedObjs.remove(obj) # # Shows what objects are linked to Deamon # def dmnList(self, obj): return self.linkedObjs # def __init__(self): self.timerList = [] self.linkedObjs = [] ############################################# def applyWound( obj ): if obj.__class__ == SomeObj: x = obj.energy = obj.energy - 20 print 'take energy of ' + obj.name + ' which current energy is %(x)d' % vars() def applyHeal( obj ): if obj.__class__ == SomeObj: x = obj.energy = obj.energy + 100 print 'give energy to ' + obj.name + ' which current energy is %(x)d' % vars() class SomeObj: def __init__(self, name, energy): self.name = name self.energy = energy a = SomeObj('obj a', 300) b = SomeObj('obj b', 1000) c = SomeObj('obj c', 3200) x = DeamonObj() print '='*40 x.dmnSet('wound', 200, applyWound) x.dmnSet('heal', 200, applyHeal) x.dmnLink(a) x.dmnLink(b) x.dmnLink(c) x.dmnTrigger('wound') x.dmnSplit(c) x.dmnTrigger('wound') x.dmnTrigger('heal')