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("mouse appears")
MouseAction.runsAway = MouseAction("mouse runs away")
MouseAction.enters = MouseAction("mouse enters trap")
MouseAction.escapes = MouseAction("mouse escapes")
MouseAction.trapped = MouseAction("mouse trapped")
MouseAction.removed = MouseAction("mouse removed")
#:~</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 "Input not supported for current state"
class Waiting(StateT):
def run(self):
print "Waiting: Broadcasting cheese smell"
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 "Luring: Presenting Cheese, door open"
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 "Trapping: Closing door"
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 "Holding: Mouse caught"
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("../mouse/MouseMoves.txt").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 "Waiting: Broadcasting cheese smell"
def next(self, input):
if input == MouseAction.appears:
return MouseTrap.luring
return MouseTrap.waiting
class Luring(State):
def run(self):
print "Luring: Presenting Cheese, door open"
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 "Trapping: Closing door"
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 "Holding: Mouse caught"
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("../mouse/MouseMoves.txt").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 "Implementation.f()"
def g(self):
print "Implementation.g()"
def h(self):
print "Implementation.h()"
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 "Implementation.f()"
def g(self):
print "Implementation.g()"
def h(self):
print "Implementation.h()"
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, "run not implemented"
def next(self, input):
assert 1, "next not implemented"
#:~</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 "Fiddle de dum, Fiddle de dee,"
def g(self):
print "Eric the half a bee."
def h(self):
print "Ho ho ho, tee hee hee,"
class Implementation2:
def f(self):
print "We're Knights of the Round Table."
def g(self):
print "We dance whene'er we're able."
def h(self):
print "We do routines and chorus scenes"
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