Ray's playground

 

Python Design Patterns I(Chapter 8 of Python 3 Object Oriented Programming)

decorator
 1 >>> import time
 2 >>> def log_calls(func):
 3     def wrapper(*args, **kwargs):
 4         now = time.time()
 5         print("Calling {0} with {1} and {2}".format(
 6             func.__name__, args, kwargs))
 7         return_value = func(*args, **kwargs)
 8         print("Executed {0} in {1}ms".format(
 9             func.__name__, time.time() - now))
10         return return_value
11     return wrapper
12 
13 >>> @log_calls
14 def test1(a, b, c):
15     print("\t test1 called")
16 
17     
18 >>> test1(123)
19 Calling test1 with (123and {}
20      test1 called
21 Executed test1 in 0.0629999637604ms


 

observer
 1 >>> class Inventory:
 2     def __init__(self):
 3         self.observers = []
 4         self._product = None
 5         self._quantity = 0
 6     def attach(self, observer):
 7         self.observers.append(observer)
 8     @property
 9     def product(self):
10         return self._product
11     @product.setter
12     def product(self, value):
13         self._product = value
14         self._update_observers()
15     @property
16     def quantity(self):
17         return self._quantity
18     @quantity.setter
19     def quantity(self, value):
20         self._quantity = value
21         self._update_observers()
22     def _update_observers(self):
23         for observer in self.observers:
24             observer()
25 
26             
27 >>> class ConsoleObserver:
28     def __init__(self, inventory):
29         self.inventory = inventory
30     def __call__(self):
31         print(self.inventory.product)
32         print(self.inventory.quantity)
33 
34 >>> i = Inventory()
35 >>> c = ConsoleObserver(i)
36 >>> i.attach(c)
37 >>> i.product = "widget"
38 widget
39 0
40 >>> i.quantity = 3
41 widget
42 3
43 

 

 

 

state
  1 >>> class Node:
  2     def __init__(self, tag_name, parent=None):
  3         self.parent = parent
  4         self.tag_name = tag_name
  5         self.children = []
  6         self.text=""
  7     def __str__(self):
  8         if self.text:
  9             return self.tag_name + "" + self.text
 10         else:
 11             return self.tag_name
 12 
 13         
 14 >>> class Parser:
 15     def __init__(self, parse_string):
 16         self.parse_string = parse_string
 17         self.root = None
 18         self.current_node = None
 19         self.state = FirstTag()
 20     def process(self, remaining_string):
 21         remaining = self.state.process(remaining_string, self)
 22         if remaining:
 23             self.process(remaining)
 24     def start(self):
 25         self.process(self.parse_string)
 26 
 27         
 28 >>> class FirstTag:
 29     def process(self, remaining_string, parser):
 30         i_start_tag = remaining_string.find('<')
 31         i_end_tag = remaining_string.find('>')
 32         tag_name = remaining_string[i_start_tag+1:i_end_tag]
 33         root = Node(tag_name)
 34         parser.root = parser.current_node = root
 35         parser.state = ChildNode()
 36         return remaining_string[i_end_tag+1:]
 37 
 38     
 39 >>> class ChildNode:
 40     def process(self, remaining_string, parser):
 41         stripped = remaining_string.strip()
 42         if stripped.startswith("</"):
 43             parser.state = CloseTag()
 44         elif stripped.startswith("<"):
 45             parser.state = OpenTag()
 46         else:
 47             parser.state = TextNode()
 48         return stripped
 49 
 50     
 51 >>> class OpenTag:
 52     def process(self, remaining_string, parser):
 53         i_start_tag = remaining_string.find('<')
 54         i_end_tag = remaining_string.find('>')
 55         tag_name = remaining_string[i_start_tag+1:i_end_tag]
 56         node = Node(tag_name, parser.current_node)
 57         parser.current_node.children.append(node)
 58         parser.current_node = node
 59         parser.state = ChildNode()
 60         return remaining_string[i_end_tag+1:]
 61 
 62     
 63 >>> class CloseTag:
 64     def process(self, remaining_string, parser):
 65         i_start_tag = remaining_string.find('<')
 66         i_end_tag = remaining_string.find('>')
 67         assert remaining_string[i_start_tag+1== "/"
 68         tag_name = remaining_string[i_start_tag+2: i_end_tag]
 69         assert tag_name == parser.current_node.tag_name
 70         parser.current_node = parser.current_node.parent
 71         parser.state = ChildNode()
 72         return remaining_string[i_end_tag+1:].strip()
 73 >>> class TextNode:
 74     def process(self, remaining_string, parser):
 75         i_start_tag = remaining_string.find('<')
 76         text = remaining_string[:i_start_tag]
 77         parser.current_node.text = text
 78         parser.state = ChildNode()
 79         return remaining_string[i_start_tag:]
 80 
 81     
 82 >>> with open("c:\\1.txt") as file:
 83     contents = file.read()
 84     p = Parser(contents)
 85     p.start()
 86     nodes = [p.root]
 87     while nodes:
 88         node = nodes.pop(0)
 89         print(node)
 90         nodes = node.children + nodes
 91 
 92         
 93 book
 94 author: Dusty Phillips
 95 publisher: Packt Publishing
 96 title: Python 3 Object Oriented Programming
 97 content
 98 chapter
 99 number: 1
100 title: Object Oriented Design
101 chapter
102 number: 2
103 title: Objects In Python

 

 

singleton
 1 >>> class OneOnly:
 2     _singleton = None
 3     def __new__(cls, *args, **kwargs):
 4         if not cls._singleton:
 5             cls._singleton = super(OneOnly, cls).__new__(cls, *args, **kwargs)
 6         return cls._singleton
 7 
 8     
 9 >>> o1 = OneOnly()
10 >>> o2 = OneOnly()
11 >>> o1
12 <__main__.OneOnly object at 0x013AB9F0>
13 >>> o2
14 <__main__.OneOnly object at 0x013AB9F0>
15 
16 
17 >>> def singleton(cls):
18     instance_container = []
19     def getinstance():
20         if not len(instance_container):
21             instance_container.append(cls())
22         return instance_container[0]
23     return getinstance
24 
25 >>> @singleton
26 class MyClass:
27     pass
28 
29 >>> c1 = MyClass()
30 >>> c2 = MyClass()
31 >>> c1
32 <__main__.MyClass object at 0x013ABA50>
33 >>> c2
34 <__main__.MyClass object at 0x013ABA50>

 

 

posted on 2010-09-04 11:14  Ray Z  阅读(356)  评论(0编辑  收藏  举报

导航