- #!/usr/bin/env python
- #-*- coding:utf-8 -*-
- '''
-
- '''
- class Handler():
- def callback(self,prefix,name,*args):
- mothed=getattr(self,prefix+name,None)
- if callable(mothed): #加一层判断,如果对象是可调用的才返回
- return mothed(*args)
- def start(self,name):
- self.callback('start_',name)
- def end(self,name):
- self.callback('end_',name)
-
- class HTMLRenderer(Handler):
- '''
- 用于生成HTML的具体处理程序
- HTMLRender内的方法都可以通过超类处理程序的start(),end(),sub()方法来访问,它们实现了用于HTML文档的基本标签.
-
- '''
-
- def start_document(self):
- print(' html ')
- def end_document(self):
- print('')
-
- def start_paragraph(self):
- print('
')
- def end_paragraph(self):
- print('
') -
- def start_heading(self):
- print('
')
- def end_heading(self):
- print('')
-
- def start_list(self):
- print(' )
- def end_list(self):
- print(' '
) def start_listitem
(self
) : print ( ' '
) def end_listitem
(self
) : print ( ' '
) def start_title
(self
) : print ( ' ')
def end_title
(self
) : print ( '' ) def feed
(self
,data
) : print (data
) '' ' getattr(object, name[, default])
Return the value of the named attribute of object. name must be a string. If the string is the name of one of the object’s attributes, the result is the value of that attribute. For example, getattr(x, 'foobar
') is equivalent to x.foobar. If the named attribute does not exist, default is returned if provided, otherwise AttributeError is raised. callable(object)
Return True if the object argument appears callable, False if not. If this returns true, it is still possible that a call fails, but if it is false, calling object will never succeed. Note that classes are callable (calling a class returns a new instance); class instances are callable if they have a __call__() method.
'
'' #下面是给XML渲染