Python论坛  - 讨论区

标题:Re[3]: [python-chinese] 关于模板系统--我的一个简单实现--欢迎讨论

2004年08月25日 星期三 17:09

Zoom.Quiet zoomq at infopro.cn
Wed Aug 25 17:09:56 HKT 2004

Hollo limodou:

  http://wiki.woodpecker.org.cn/moin.cgi/4suite4xslt

整理了一下子!
发现XML 数据源的模板解析还是XSLT最舒服!!


/******** [2004-08-25]17:09:15 ; limodou wrote:

limodou> Zoom.Quiet,您好!

limodou> 	有心得写成文章呀!

limodou> ======= 2004-08-24 17:36:34 您在来信中写道:=======

>>Hollo limodou:
>>
>>  谢谢!!
>>
>>回家好好看一看!
>>今天,研究了一下子 4suite 使用其 4xslt 模块,可以在脚本中进行 XSLT+XML 的解析处理,
>>
>>哈哈哈!直接使用 XSLT 进行嵌套处理非常的自然,也是XML 标准的处理方式!!!
>>
>>
>>/******** [2004-08-24]17:34:58 ; limodou wrote:
>>
>>limodou> python-chinese,您好!
>>
>>limodou>
>>我写了一个小的模板类,现在可以实现嵌套处理,但是不支持根据列表数据而实现模板的循环。
>>
>>limodou> 模板类:
>>
>>limodou> import re
>>limodou> import sets
>>limodou> import copy
>>
>>limodou> re_node = re.compile('%\((\w+)\)s')
>>
>>limodou> class Template:
>>limodou> 	def __init__(self):
>>limodou> 		pass
>>	
>>limodou> 	def load(self, tplname):
>>limodou> 		mod = __import__(tplname)
>>limodou> 		components = tplname.split('.')
>>limodou> 		for comp in components[1:]:
>>limodou> 			mod = getattr(mod, comp)
>>
>>limodou> 		self.vars = {}
>>limodou> 		self.nodes = {}
>>limodou> 		for v in dir(mod):
>>limodou> 			if v.startswith('__') and v.endswith('__'):
>>limodou> 				continue
>>limodou> 			self.vars[v] = getattr(mod, v)
>>limodou> 			self.nodes[v] =
>>limodou> list(sets.Set(self._get_rely_on_node(self.vars[v])))
>>
>>limodou> 	def _get_rely_on_node(self, s):	#search for
>>limodou> %(name)s format, make a dict
>>limodou> 		return re_node.findall(s)
>>
>>limodou> 	def _get_list(self, path, target):
>>limodou> 		if not self.vars.has_key(target):
>>limodou> 			return
>>limodou> 		if target not in path:
>>limodou> 			path.append(target)
>>limodou> 		for i in self.nodes[target]:
>>limodou> 			self._get_list(path, i)
>>limodou> 		return
>>
>>limodou> 	def value(self, target='main', values=None):
>>limodou> 		path = []
>>limodou> 		self._get_list(path, target)
>>limodou> 		path.reverse()
>>limodou> 		if not values:
>>limodou> 			vals = {}
>>limodou> 		else:
>>limodou> 			vals = copy.deepcopy(values)
>>limodou> 		for i in path:
>>limodou> 			vals[i] = self.vars[i] % vals
>>limodou> 		return vals[target]
>>
>>limodou> if __name__ == '__main__':
>>limodou> 	vars = dict(value='aba')
>>limodou> 	template = Template()
>>limodou> 	template.load('tmp2')
>>limodou> 	print template.value('main', vars)
>>
>>limodou> 测试文件:
>>
>>limodou> main="""
>>limodou> v = []
>>limodou> %(statement)s
>>limodou> for i in range(10):
>>limodou> %(printvar)s"""
>>
>>limodou> statement = "v.append('%(value)s')\n"
>>
>>limodou> printvar = "    print '\\n'.join(%(var)s)\n"
>>
>>limodou> var = 'v'
>>
>>limodou> 有兴趣的可以讨论。
>>
>>limodou>
>>现在的问题是对于可循环的模板,比如上面的statement我想根据提供的数据如果为列表或元组时,会自动进行重复。但一个模板可能涉及多个模板变量,如果有多个模板变量为多值该如何处理。按我
>>limodou>
>>的想法,这种多值应该组织为一个元组的列表。不过,不太清楚otter会分析出什么数据来。不知道ZoomQuiet对这个有没有兴趣。
>>
>>limodou> 简单说一下用法:
>>
>>limodou> 我定义的模板其实是一个模块,不过只是用来定义数据的。
>>
>>limodou> 	vars = dict(value='aba')  #生成模板用到的变量
>>limodou> 	template = Template()     #生成一个模板对象
>>limodou> 	template.load('tmp2')     #装入一个模板
>>limodou> 	print template.value('main', vars)  
>>limodou> #得到最终模板的值
>>当然,模板中还有许多的子模板,都可以得到。
>>limodou>         致
>>limodou> 礼!
>> 				
>>
>>limodou>         limodou
>>limodou>         chatme at 263.net
>>limodou>           2004-08-24
>>
>>
>>********************************************/
>>
>>-- 
>>Free as in Freedom
>>
>> Zoom.Quiet                           
>>
>>#=========================================#
>>]Time is unimportant, only life important![
>>#=========================================#
>>
>>sender is the Bat!2.12.00
>>
>>_______________________________________________
>>python-chinese list
>>python-chinese at lists.python.cn
>>http://python.cn/mailman/listinfo/python-chinese
>>

limodou> = = = = = = = = = = = = = = = = = = = =
			

limodou>         致
limodou> 礼!
 
				 
limodou>         limodou
limodou>         chatme at 263.net
limodou>           2004-08-24



********************************************/

-- 
Free as in Freedom

 Zoom.Quiet                           

#=========================================#
]Time is unimportant, only life important![
#=========================================#

sender is the Bat!2.12.00



[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2004年08月25日 星期三 17:13

limodou chatme at 263.net
Wed Aug 25 17:13:34 HKT 2004

hoxide,您好!

	总结不错,希望能看到你在社区里大显身手。

======= 2004-08-25 16:32:40 您在来信中写道:=======

>这也是我的社会实践报告~~~ 				
>
>        hoxide
>        hoxide_dirac at yahoo.com.cn
>          2004-08-25
>_______________________________________________
>python-chinese list
>python-chinese at lists.python.cn
>http://python.cn/mailman/listinfo/python-chinese
>

= = = = = = = = = = = = = = = = = = = =
			

        致
礼!
 
				 
        limodou
        chatme at 263.net
          2004-08-25


[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2004年08月25日 星期三 17:15

limodou chatme at 263.net
Wed Aug 25 17:15:02 HKT 2004

Zoom.Quiet,您好!

	那是自然了。xslt就是专门处理xml的脚本语言。只不过,用的东西太广的话,别人就很难参与进来了。因为学习曲线可能太长了。

======= 2004-08-25 17:09:56 您在来信中写道:=======

>Hollo limodou:
>
>  http://wiki.woodpecker.org.cn/moin.cgi/4suite4xslt
>
>整理了一下子!
>发现XML 数据源的模板解析还是XSLT最舒服!!
>
>
>/******** [2004-08-25]17:09:15 ; limodou wrote:
>
>limodou> Zoom.Quiet,您好!
>
>limodou> 	有心得写成文章呀!
>
>limodou> ======= 2004-08-24 17:36:34 您在来信中写道:=======
>
>>>Hollo limodou:
>>>
>>>  谢谢!!
>>>
>>>回家好好看一看!
>>>今天,研究了一下子 4suite 使用其 4xslt 模块,可以在脚本中进行 XSLT+XML 的解析处理,
>>>
>>>哈哈哈!直接使用 XSLT 进行嵌套处理非常的自然,也是XML 标准的处理方式!!!
>>>
>>>
>>>/******** [2004-08-24]17:34:58 ; limodou wrote:
>>>
>>>limodou> python-chinese,您好!
>>>
>>>limodou>
>>>我写了一个小的模板类,现在可以实现嵌套处理,但是不支持根据列表数据而实现模板的循环。
>>>
>>>limodou> 模板类:
>>>
>>>limodou> import re
>>>limodou> import sets
>>>limodou> import copy
>>>
>>>limodou> re_node = re.compile('%\((\w+)\)s')
>>>
>>>limodou> class Template:
>>>limodou> 	def __init__(self):
>>>limodou> 		pass
>>>	
>>>limodou> 	def load(self, tplname):
>>>limodou> 		mod = __import__(tplname)
>>>limodou> 		components = tplname.split('.')
>>>limodou> 		for comp in components[1:]:
>>>limodou> 			mod = getattr(mod, comp)
>>>
>>>limodou> 		self.vars = {}
>>>limodou> 		self.nodes = {}
>>>limodou> 		for v in dir(mod):
>>>limodou> 			if v.startswith('__') and v.endswith('__'):
>>>limodou> 				continue
>>>limodou> 			self.vars[v] = getattr(mod, v)
>>>limodou> 			self.nodes[v] =
>>>limodou> list(sets.Set(self._get_rely_on_node(self.vars[v])))
>>>
>>>limodou> 	def _get_rely_on_node(self, s):	#search for
>>>limodou> %(name)s format, make a dict
>>>limodou> 		return re_node.findall(s)
>>>
>>>limodou> 	def _get_list(self, path, target):
>>>limodou> 		if not self.vars.has_key(target):
>>>limodou> 			return
>>>limodou> 		if target not in path:
>>>limodou> 			path.append(target)
>>>limodou> 		for i in self.nodes[target]:
>>>limodou> 			self._get_list(path, i)
>>>limodou> 		return
>>>
>>>limodou> 	def value(self, target='main', values=None):
>>>limodou> 		path = []
>>>limodou> 		self._get_list(path, target)
>>>limodou> 		path.reverse()
>>>limodou> 		if not values:
>>>limodou> 			vals = {}
>>>limodou> 		else:
>>>limodou> 			vals = copy.deepcopy(values)
>>>limodou> 		for i in path:
>>>limodou> 			vals[i] = self.vars[i] % vals
>>>limodou> 		return vals[target]
>>>
>>>limodou> if __name__ == '__main__':
>>>limodou> 	vars = dict(value='aba')
>>>limodou> 	template = Template()
>>>limodou> 	template.load('tmp2')
>>>limodou> 	print template.value('main', vars)
>>>
>>>limodou> 测试文件:
>>>
>>>limodou> main="""
>>>limodou> v = []
>>>limodou> %(statement)s
>>>limodou> for i in range(10):
>>>limodou> %(printvar)s"""
>>>
>>>limodou> statement = "v.append('%(value)s')\n"
>>>
>>>limodou> printvar = "    print '\\n'.join(%(var)s)\n"
>>>
>>>limodou> var = 'v'
>>>
>>>limodou> 有兴趣的可以讨论。
>>>
>>>limodou>
>>>现在的问题是对于可循环的模板,比如上面的statement我想根据提供的数据如果为列表或元组时,会自动进行重复。但一个模板可能涉及多个模板变量,如果有多个模板变量为多值该如何处理。按我
>>>limodou>
>>>的想法,这种多值应该组织为一个元组的列表。不过,不太清楚otter会分析出什么数据来。不知道ZoomQuiet对这个有没有兴趣。
>>>
>>>limodou> 简单说一下用法:
>>>
>>>limodou> 我定义的模板其实是一个模块,不过只是用来定义数据的。
>>>
>>>limodou> 	vars = dict(value='aba')  #生成模板用到的变量
>>>limodou> 	template = Template()     #生成一个模板对象
>>>limodou> 	template.load('tmp2')     #装入一个模板
>>>limodou> 	print template.value('main', vars)  
>>>limodou> #得到最终模板的值
>>>当然,模板中还有许多的子模板,都可以得到。
>>>limodou>         致
>>>limodou> 礼!
>>> 				
>>>
>>>limodou>         limodou
>>>limodou>         chatme at 263.net
>>>limodou>           2004-08-24
>>>
>>>
>>>********************************************/
>>>
>>>-- 
>>>Free as in Freedom
>>>
>>> Zoom.Quiet                           
>>>
>>>#=========================================#
>>>]Time is unimportant, only life important![
>>>#=========================================#
>>>
>>>sender is the Bat!2.12.00
>>>
>>>_______________________________________________
>>>python-chinese list
>>>python-chinese at lists.python.cn
>>>http://python.cn/mailman/listinfo/python-chinese
>>>
>
>limodou> = = = = = = = = = = = = = = = = = = = =
>			
>
>limodou>         致
>limodou> 礼!
> 
>				 
>limodou>         limodou
>limodou>         chatme at 263.net
>limodou>           2004-08-24
>
>
>
>********************************************/
>
>-- 
>Free as in Freedom
>
> Zoom.Quiet                           
>
>#=========================================#
>]Time is unimportant, only life important![
>#=========================================#
>
>sender is the Bat!2.12.00
>
>_______________________________________________
>python-chinese list
>python-chinese at lists.python.cn
>http://python.cn/mailman/listinfo/python-chinese
>

= = = = = = = = = = = = = = = = = = = =
			

        致
礼!
 
				 
        limodou
        chatme at 263.net
          2004-08-25


[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

如下红色区域有误,请重新填写。

    你的回复:

    请 登录 后回复。还没有在Zeuux哲思注册吗?现在 注册 !

    Zeuux © 2025

    京ICP备05028076号