carfield.com.hk FlowerVisitors.py 2001-12-26T16:00:00Z 2001-12-26T16:00:00Z <br/><TEXTAREA name="code" class="py" rows="16" cols="100">#: c11:FlowerVisitors.py # Demonstration of &quot;visitor&quot; pattern. from __future__ import generators import random # The Flower hierarchy cannot be changed: class Flower(object): def accept(self, visitor): visitor.visit(self) def pollinate(self, pollinator): print self, &quot;pollinated by&quot;, pollinator def eat(self, eater): print self, &quot;eaten by&quot;, eater def __str__(self): return self.__class__.__name__ class Gladiolus(Flower): pass class Runuculus(Flower): pass class Chrysanthemum(Flower): pass class Visitor: def __str__(self): return self.__class__.__name__ class Bug(Visitor): pass class Pollinator(Bug): pass class Predator(Bug): pass # Add the ability to do &quot;Bee&quot; activities: class Bee(Pollinator): def visit(self, flower): flower.pollinate(self) # Add the ability to do &quot;Fly&quot; activities: class Fly(Pollinator): def visit(self, flower): flower.pollinate(self) # Add the ability to do &quot;Worm&quot; activities: class Worm(Predator): def visit(self, flower): flower.eat(self) def flowerGen(n): flwrs = Flower.__subclasses__() for i in range(n): yield random.choice(flwrs)() # It's almost as if I had a method to Perform # various &quot;Bug&quot; operations on all Flowers: bee = Bee() fly = Fly() worm = Worm() for flower in flowerGen(10): flower.accept(bee) flower.accept(fly) flower.accept(worm) #:~</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 PaperScissorsRock.py 2001-12-26T16:00:00Z 2001-12-26T16:00:00Z <br/><TEXTAREA name="code" class="py" rows="16" cols="100">#: c11:PaperScissorsRock.py # Demonstration of multiple dispatching. from __future__ import generators import random # An enumeration type: class Outcome: def __init__(self, value, name): self.value = value self.name = name def __str__(self): return self.name def __eq__(self, other): return self.value == other.value Outcome.WIN = Outcome(0, &quot;win&quot;) Outcome.LOSE = Outcome(1, &quot;lose&quot;) Outcome.DRAW = Outcome(2, &quot;draw&quot;) class Item(object): def __str__(self): return self.__class__.__name__ class Paper(Item): def compete(self, item): # First dispatch: self was Paper return item.evalPaper(self) def evalPaper(self, item): # Item was Paper, we're in Paper return Outcome.DRAW def evalScissors(self, item): # Item was Scissors, we're in Paper return Outcome.WIN def evalRock(self, item): # Item was Rock, we're in Paper return Outcome.LOSE class Scissors(Item): def compete(self, item): # First dispatch: self was Scissors return item.evalScissors(self) def evalPaper(self, item): # Item was Paper, we're in Scissors return Outcome.LOSE def evalScissors(self, item): # Item was Scissors, we're in Scissors return Outcome.DRAW def evalRock(self, item): # Item was Rock, we're in Scissors return Outcome.WIN class Rock(Item): def compete(self, item): # First dispatch: self was Rock return item.evalRock(self) def evalPaper(self, item): # Item was Paper, we're in Rock return Outcome.WIN def evalScissors(self, item): # Item was Scissors, we're in Rock return Outcome.LOSE def evalRock(self, item): # Item was Rock, we're in Rock return Outcome.DRAW def match(item1, item2): print &quot;%s &lt;--&gt; %s : %s&quot; % ( item1, item2, item1.compete(item2)) # Generate the items: def itemPairGen(n): # Create a list of instances of all Items: Items = Item.__subclasses__() for i in range(n): yield (random.choice(Items)(), random.choice(Items)()) for item1, item2 in itemPairGen(20): match(item1, item2) #:~</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 PaperScissorsRock2.py 2001-12-26T16:00:00Z 2001-12-26T16:00:00Z <br/><TEXTAREA name="code" class="py" rows="16" cols="100">#: c11:PaperScissorsRock2.py # Multiple dispatching using a table from __future__ import generators import random class Outcome: def __init__(self, value, name): self.value = value self.name = name def __str__(self): return self.name def __eq__(self, other): return self.value == other.value Outcome.WIN = Outcome(0, &quot;win&quot;) Outcome.LOSE = Outcome(1, &quot;lose&quot;) Outcome.DRAW = Outcome(2, &quot;draw&quot;) class Item(object): def compete(self, item): # Use a tuple for table lookup: return outcome[self.__class__, item.__class__] def __str__(self): return self.__class__.__name__ class Paper(Item): pass class Scissors(Item): pass class Rock(Item): pass outcome = { (Paper, Rock): Outcome.WIN, (Paper, Scissors): Outcome.LOSE, (Paper, Paper): Outcome.DRAW, (Scissors, Paper): Outcome.WIN, (Scissors, Rock): Outcome.LOSE, (Scissors, Scissors): Outcome.DRAW, (Rock, Scissors): Outcome.WIN, (Rock, Paper): Outcome.LOSE, (Rock, Rock): Outcome.DRAW, } def match(item1, item2): print &quot;%s &lt;--&gt; %s : %s&quot; % ( item1, item2, item1.compete(item2)) # Generate the items: def itemPairGen(n): # Create a list of instances of all Items: Items = Item.__subclasses__() for i in range(n): yield (random.choice(Items)(), random.choice(Items)()) for item1, item2 in itemPairGen(20): match(item1, item2) #:~</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