carfield.com.hk MouseAction.py 2001-12-26T16:00:00Z 2001-12-26T16:00:00Z <br/><TEXTAREA name="code" class="py" rows="16" cols="100">#: c04:mouse:MouseAction.py class MouseAction: def __init__(self, action): self.action = action def __str__(self): return self.action def __cmp__(self, other): return cmp(self.action, other.action) # Necessary when __cmp__ or __eq__ is defined # in order to make this class usable as a # dictionary key: def __hash__(self): return hash(self.action) # Static fields; an enumeration of instances: MouseAction.appears = MouseAction(&quot;mouse appears&quot;) MouseAction.runsAway = MouseAction(&quot;mouse runs away&quot;) MouseAction.enters = MouseAction(&quot;mouse enters trap&quot;) MouseAction.escapes = MouseAction(&quot;mouse escapes&quot;) MouseAction.trapped = MouseAction(&quot;mouse trapped&quot;) MouseAction.removed = MouseAction(&quot;mouse removed&quot;) #:~</TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2001-12-26T16:00:00Z MouseMoves.txt 2001-12-26T16:00:00Z 2001-12-26T16:00:00Z <br/>mouse appears<br/>mouse runs away<br/>mouse appears<br/>mouse enters trap<br/>mouse escapes<br/>mouse appears<br/>mouse enters trap<br/>mouse trapped<br/>mouse removed<br/>mouse appears<br/>mouse runs away<br/>mouse appears<br/>mouse enters trap<br/>mouse trapped<br/>mouse removed<br/><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2001-12-26T16:00:00Z MouseTrap2Test.py 2001-12-26T16:00:00Z 2001-12-26T16:00:00Z <br/><TEXTAREA name="code" class="py" rows="16" cols="100">#: c04:mousetrap2:MouseTrap2Test.py # A better mousetrap using tables import string, sys sys.path += ['../statemachine', '../mouse'] from State import State from StateMachine import StateMachine from MouseAction import MouseAction class StateT(State): def __init__(self): self.transitions = None def next(self, input): if self.transitions.has_key(input): return self.transitions[input] else: raise &quot;Input not supported for current state&quot; class Waiting(StateT): def run(self): print &quot;Waiting: Broadcasting cheese smell&quot; def next(self, input): # Lazy initialization: if not self.transitions: self.transitions = { MouseAction.appears : MouseTrap.luring } return StateT.next(self, input) class Luring(StateT): def run(self): print &quot;Luring: Presenting Cheese, door open&quot; def next(self, input): # Lazy initialization: if not self.transitions: self.transitions = { MouseAction.enters : MouseTrap.trapping, MouseAction.runsAway : MouseTrap.waiting } return StateT.next(self, input) class Trapping(StateT): def run(self): print &quot;Trapping: Closing door&quot; def next(self, input): # Lazy initialization: if not self.transitions: self.transitions = { MouseAction.escapes : MouseTrap.waiting, MouseAction.trapped : MouseTrap.holding } return StateT.next(self, input) class Holding(StateT): def run(self): print &quot;Holding: Mouse caught&quot; def next(self, input): # Lazy initialization: if not self.transitions: self.transitions = { MouseAction.removed : MouseTrap.waiting } return StateT.next(self, input) class MouseTrap(StateMachine): def __init__(self): # Initial state StateMachine.__init__(self, MouseTrap.waiting) # Static variable initialization: MouseTrap.waiting = Waiting() MouseTrap.luring = Luring() MouseTrap.trapping = Trapping() MouseTrap.holding = Holding() moves = map(string.strip, open(&quot;../mouse/MouseMoves.txt&quot;).readlines()) mouseMoves = map(MouseAction, moves) MouseTrap().runAll(mouseMoves) #:~</TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2001-12-26T16:00:00Z MouseTrapTest.py 2001-12-26T16:00:00Z 2001-12-26T16:00:00Z <br/><TEXTAREA name="code" class="py" rows="16" cols="100">#: c04:mousetrap1:MouseTrapTest.py # State Machine pattern using 'if' statements # to determine the next state. import string, sys sys.path += ['../statemachine', '../mouse'] from State import State from StateMachine import StateMachine from MouseAction import MouseAction # A different subclass for each state: class Waiting(State): def run(self): print &quot;Waiting: Broadcasting cheese smell&quot; def next(self, input): if input == MouseAction.appears: return MouseTrap.luring return MouseTrap.waiting class Luring(State): def run(self): print &quot;Luring: Presenting Cheese, door open&quot; def next(self, input): if input == MouseAction.runsAway: return MouseTrap.waiting if input == MouseAction.enters: return MouseTrap.trapping return MouseTrap.luring class Trapping(State): def run(self): print &quot;Trapping: Closing door&quot; def next(self, input): if input == MouseAction.escapes: return MouseTrap.waiting if input == MouseAction.trapped: return MouseTrap.holding return MouseTrap.trapping class Holding(State): def run(self): print &quot;Holding: Mouse caught&quot; def next(self, input): if input == MouseAction.removed: return MouseTrap.waiting return MouseTrap.holding class MouseTrap(StateMachine): def __init__(self): # Initial state StateMachine.__init__(self, MouseTrap.waiting) # Static variable initialization: MouseTrap.waiting = Waiting() MouseTrap.luring = Luring() MouseTrap.trapping = Trapping() MouseTrap.holding = Holding() moves = map(string.strip, open(&quot;../mouse/MouseMoves.txt&quot;).readlines()) MouseTrap().runAll(map(MouseAction, moves)) #:~</TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2001-12-26T16:00:00Z ProxyDemo.py 2001-12-26T16:00:00Z 2001-12-26T16:00:00Z <br/><TEXTAREA name="code" class="py" rows="16" cols="100">#: c04:ProxyDemo.py # Simple demonstration of the Proxy pattern. class Implementation: def f(self): print &quot;Implementation.f()&quot; def g(self): print &quot;Implementation.g()&quot; def h(self): print &quot;Implementation.h()&quot; class Proxy: def __init__(self): self.__implementation = Implementation() # Pass method calls to the implementation: def f(self): self.__implementation.f() def g(self): self.__implementation.g() def h(self): self.__implementation.h() p = Proxy() p.f(); p.g(); p.h() #:~</TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2001-12-26T16:00:00Z ProxyDemo2.py 2001-12-26T16:00:00Z 2001-12-26T16:00:00Z <br/><TEXTAREA name="code" class="py" rows="16" cols="100">#: c04:ProxyDemo2.py # Simple demonstration of the Proxy pattern. class Implementation2: def f(self): print &quot;Implementation.f()&quot; def g(self): print &quot;Implementation.g()&quot; def h(self): print &quot;Implementation.h()&quot; class Proxy2: def __init__(self): self.__implementation = Implementation2() def __getattr__(self, name): return getattr(self.__implementation, name) p = Proxy2() p.f(); p.g(); p.h(); #:~</TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2001-12-26T16:00:00Z State.py 2001-12-26T16:00:00Z 2001-12-26T16:00:00Z <br/><TEXTAREA name="code" class="py" rows="16" cols="100">#: c04:statemachine:State.py # A State has an operation, and can be moved # into the next State given an Input: class State: def run(self): assert 1, &quot;run not implemented&quot; def next(self, input): assert 1, &quot;next not implemented&quot; #:~</TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2001-12-26T16:00:00Z StateDemo.py 2001-12-26T16:00:00Z 2001-12-26T16:00:00Z <br/><TEXTAREA name="code" class="py" rows="16" cols="100">#: c04:StateDemo.py # Simple demonstration of the State pattern. class State_d: def __init__(self, imp): self.__implementation = imp def changeImp(self, newImp): self.__implementation = newImp # Delegate calls to the implementation: def __getattr__(self, name): return getattr(self.__implementation, name) class Implementation1: def f(self): print &quot;Fiddle de dum, Fiddle de dee,&quot; def g(self): print &quot;Eric the half a bee.&quot; def h(self): print &quot;Ho ho ho, tee hee hee,&quot; class Implementation2: def f(self): print &quot;We're Knights of the Round Table.&quot; def g(self): print &quot;We dance whene'er we're able.&quot; def h(self): print &quot;We do routines and chorus scenes&quot; def run(b): b.f() b.g() b.h() b.g() b = State_d(Implementation1()) run(b) b.changeImp(Implementation2()) run(b) #:~</TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2001-12-26T16:00:00Z StateMachine.py 2001-12-26T16:00:00Z 2001-12-26T16:00:00Z <br/><TEXTAREA name="code" class="py" rows="16" cols="100">#: c04:statemachine:StateMachine.py # Takes a list of Inputs to move from State to # State using a template method. class StateMachine: def __init__(self, initialState): self.currentState = initialState self.currentState.run() # Template method: def runAll(self, inputs): for i in inputs: print i self.currentState = self.currentState.next(i) self.currentState.run() #:~</TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2001-12-26T16:00:00Z