Python论坛  - 讨论区

标题:Re: [python-chinese] 新的模板类

2004年08月26日 星期四 12:33

HD hdcola at gmail.com
Thu Aug 26 12:33:50 HKT 2004

好东东呀,小新和zoomq看看呀,木头的好东东 :)

On Tue, 24 Aug 2004 23:53:54 +0800, limodou <chatme at 263.net> wrote:
> python-chinese,您好!
> 
> 模板类:模板元素定义改为类对象,支持循环
> 
> #coding=utf-8
> 
> import re
> import sets
> import copy
> import types
> 
> class T:
>         def __init__(self, string):
>                 self.text = string
> 
>         def getText(self):
>                 return self.text
> 
> class Template:
>         def __init__(self, beginchars='<#', endchars='#>'):
>                 self.beginchars = beginchars            #define template var's left delimeter chars
>                 self.endchars = endchars                        #define template var's right delimeter chars
> 
>         def load(self, tplname):
>                 mod = __import__(tplname)
>                 components = tplname.split('.')
>                 for comp in components[1:]:
>                         mod = getattr(mod, comp)
> 
>                 self.vars = {}
>                 self.nodes = {}
>                 for vn in dir(mod):
>                         v = getattr(mod, vn)
>                         if hasattr(v, '__class__') and v.__class__.__name__ == 'T':
>                                 self.vars[vn] = v
>                                 self.nodes[vn] = self._get_rely_on_node(v.getText())
> 
>         #取模板元素的相关集
>         def _get_rely_on_node(self, s): #search for %(name)s format, make a dict
>                 re_node = re.compile(r'%s(\w+)%s' % (self.beginchars, self.endchars))
> 
>                 return list(sets.Set(re_node.findall(s)))
> 
>         #取模板元素的生成顺序
>         def _get_list(self, path, target):
>                 if not self.vars.has_key(target):
>                         return
>                 if target not in path:
>                         path.append(target)
>                 for i in self.nodes[target]:
>                         self._get_list(path, i)
>                 return
> 
>         #生成模板值
>         #values应为字典的字典。即每一个模板元素如果引用有外部的变量,那么在values中应有此模板元素的一个键。
>         #同时它的值应为所有外部变量的一个字典
>         def value(self, target='main', values=None):
>                 path = []
>                 self._get_list(path, target)
>                 path.reverse()
>                 vals = {}
>                 for i in path:
>                         value = self._getElementValue(i, vals, values)
>                         vals[i] = value
>                 return vals[target]
> 
>         def _replace(self, text, values):
>                 def dosup(matchobj, values=values):
>                         if values:
>                                 return values.get(matchobj.groups()[0], matchobj.group())
>                         else:
>                                 return matchobj.group()
>                 #先将所有已经存在的模板元素进行替换
>                 return re.sub(r'%s(\w+)%s' % (self.beginchars, self.endchars), dosup, text)
> 
>         def _getElementValue(self, name, elements, values=None):
>                 text = self.vars[name].getText()
>                 text = self._replace(text, elements)
>                 #再根据外部引用变量的类型决定是否进行循环
>                 if values and values.has_key(name):
>                         if len(values[name]) == 1:      #是字典则只进行一次替换
>                                 text = self._replace(text, values[name])
>                         else:
>                                 s = []
>                                 for v in values[name]:
>                                         s.append(self._replace(text, v))
>                                 text = ''.join(s)
> 
>                 return text
> 
> if __name__ == '__main__':
>         vars = dict(hello=[{'var':'var1'},{'var':'var2'},{'var':'var3'}])
>         template = Template()
>         template.load('tmp2')
>         print template.value('program', vars)
> 
> 测试模板:
> 
> from Template import T
> 
> hello = T("Hello, <#var#> \n")
> message = T("Please input yourname:")
> program = T("""name = input_raw("<#message#>")
> print '''<#hello#>'''
>         """)
> 
>> 礼!
> 
> 
> limodou
> chatme at 263.net
> 2004-08-24
> 
> 
> _______________________________________________
> python-chinese list
> python-chinese at lists.python.cn
> http://python.cn/mailman/listinfo/python-chinese
> 
> 


-- 
HD(燃烧中的火)
我工作我快乐,我勤奋我收获。请与我一起快乐,与我一起收获。


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

2004年08月26日 星期四 12:36

Zoom.Quiet zoomq at infopro.cn
Thu Aug 26 12:36:30 HKT 2004

Hollo HD:

  咳咳!一直打扫卫生!没有时间看自个儿的田哪!
http://wiki.woodpecker.org.cn/moin.cgi/meteor_28_e6_b5_81_e6_98_9f_29_e6_a8_a1_e6_9d_bf_e7_b3_bb_e7_bb_9f_e8_af_b4_e6_98_8e
早就发布了!

进阶想法也讨论了一天了…………


/******** [2004-08-26]12:35:47 ; HD wrote:

HD> 好东东呀,小新和zoomq看看呀,木头的好东东 :)

HD> On Tue, 24 Aug 2004 23:53:54 +0800, limodou <chatme at 263.net> wrote:
>> python-chinese,您好!
>> 
>> 模板类:模板元素定义改为类对象,支持循环
>> 
>> #coding=utf-8
>> 
>> import re
>> import sets
>> import copy
>> import types
>> 
>> class T:
>>         def __init__(self, string):
>>                 self.text = string
>> 
>>         def getText(self):
>>                 return self.text
>> 
>> class Template:
>>         def __init__(self, beginchars='<#', endchars='#>'):
>>                 self.beginchars = beginchars            #define
>> template var's left delimeter chars
>>                 self.endchars = endchars                       
>> #define template var's right delimeter chars
>> 
>>         def load(self, tplname):
>>                 mod = __import__(tplname)
>>                 components = tplname.split('.')
>>                 for comp in components[1:]:
>>                         mod = getattr(mod, comp)
>> 
>>                 self.vars = {}
>>                 self.nodes = {}
>>                 for vn in dir(mod):
>>                         v = getattr(mod, vn)
>>                         if hasattr(v, '__class__') and v.__class__.__name__ == 'T':
>>                                 self.vars[vn] = v
>>                                 self.nodes[vn] =
>> self._get_rely_on_node(v.getText())
>> 
>>         #取模板元素的相关集
>>         def _get_rely_on_node(self, s): #search for %(name)s format, make a dict
>>                 re_node = re.compile(r'%s(\w+)%s' %
>> (self.beginchars, self.endchars))
>> 
>>                 return list(sets.Set(re_node.findall(s)))
>> 
>>         #取模板元素的生成顺序
>>         def _get_list(self, path, target):
>>                 if not self.vars.has_key(target):
>>                         return
>>                 if target not in path:
>>                         path.append(target)
>>                 for i in self.nodes[target]:
>>                         self._get_list(path, i)
>>                 return
>> 
>>         #生成模板值
>>        
>> #values应为字典的字典。即每一个模板元素如果引用有外部的变量,那么在values中应有此模板元素的一个键。
>>         #同时它的值应为所有外部变量的一个字典
>>         def value(self, target='main', values=None):
>>                 path = []
>>                 self._get_list(path, target)
>>                 path.reverse()
>>                 vals = {}
>>                 for i in path:
>>                         value = self._getElementValue(i, vals, values)
>>                         vals[i] = value
>>                 return vals[target]
>> 
>>         def _replace(self, text, values):
>>                 def dosup(matchobj, values=values):
>>                         if values:
>>                                 return
>> values.get(matchobj.groups()[0], matchobj.group())
>>                         else:
>>                                 return matchobj.group()
>>                 #先将所有已经存在的模板元素进行替换
>>                 return re.sub(r'%s(\w+)%s' % (self.beginchars, self.endchars), dosup, text)
>> 
>>         def _getElementValue(self, name, elements, values=None):
>>                 text = self.vars[name].getText()
>>                 text = self._replace(text, elements)
>>                 #再根据外部引用变量的类型决定是否进行循环
>>                 if values and values.has_key(name):
>>                         if len(values[name]) == 1:      #是字典则只进行一次替换
>>                                 text = self._replace(text, values[name])
>>                         else:
>>                                 s = []
>>                                 for v in values[name]:
>>                                        
>> s.append(self._replace(text, v))
>>                                 text = ''.join(s)
>> 
>>                 return text
>> 
>> if __name__ == '__main__':
>>         vars =
>> dict(hello=[{'var':'var1'},{'var':'var2'},{'var':'var3'}])
>>         template = Template()
>>         template.load('tmp2')
>>         print template.value('program', vars)
>> 
>> 测试模板:
>> 
>> from Template import T
>> 
>> hello = T("Hello, <#var#> \n")
>> message = T("Please input yourname:")
>> program = T("""name = input_raw("<#message#>")
>> print '''<#hello#>'''
>>         """)
>> 
>>>> 礼!
>> 
>> 
>> limodou
>> chatme at 263.net
>> 2004-08-24
>> 
>> 
>> _______________________________________________
>> python-chinese list
>> python-chinese at lists.python.cn
>> http://python.cn/mailman/listinfo/python-chinese
>> 
>> 




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

-- 
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月26日 星期四 12:54

limodou chatme at 263.net
Thu Aug 26 12:54:34 HKT 2004

mdkr,您好!

	正打算学一学。

======= 2004-08-26 12:31:20 您在来信中写道:=======

>用pythoncard开发 wx的界面,有人做过吗_______________________________________________
>python-chinese list
>python-chinese at lists.python.cn
>http://python.cn/mailman/listinfo/python-chinese
>

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

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


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

2004年08月26日 星期四 12:56

limodou chatme at 263.net
Thu Aug 26 12:56:33 HKT 2004

HD,您好!

	回头模板类再增加一个可以重载的方法,用于将模板分类成内部表示。这样,就可以支持各种各样的模板样式了。

======= 2004-08-26 12:33:50 您在来信中写道:=======

>好东东呀,小新和zoomq看看呀,木头的好东东 :)
>
>On Tue, 24 Aug 2004 23:53:54 +0800, limodou <chatme at 263.net> wrote:
>> python-chinese,您好!
>> 
>> 模板类:模板元素定义改为类对象,支持循环
>> 
>> #coding=utf-8
>> 
>> import re
>> import sets
>> import copy
>> import types
>> 
>> class T:
>>         def __init__(self, string):
>>                 self.text = string
>> 
>>         def getText(self):
>>                 return self.text
>> 
>> class Template:
>>         def __init__(self, beginchars='<#', endchars='#>'):
>>                 self.beginchars = beginchars            #define template var's left delimeter chars
>>                 self.endchars = endchars                        #define template var's right delimeter chars
>> 
>>         def load(self, tplname):
>>                 mod = __import__(tplname)
>>                 components = tplname.split('.')
>>                 for comp in components[1:]:
>>                         mod = getattr(mod, comp)
>> 
>>                 self.vars = {}
>>                 self.nodes = {}
>>                 for vn in dir(mod):
>>                         v = getattr(mod, vn)
>>                         if hasattr(v, '__class__') and v.__class__.__name__ == 'T':
>>                                 self.vars[vn] = v
>>                                 self.nodes[vn] = self._get_rely_on_node(v.getText())
>> 
>>         #取模板元素的相关集
>>         def _get_rely_on_node(self, s): #search for %(name)s format, make a dict
>>                 re_node = re.compile(r'%s(\w+)%s' % (self.beginchars, self.endchars))
>> 
>>                 return list(sets.Set(re_node.findall(s)))
>> 
>>         #取模板元素的生成顺序
>>         def _get_list(self, path, target):
>>                 if not self.vars.has_key(target):
>>                         return
>>                 if target not in path:
>>                         path.append(target)
>>                 for i in self.nodes[target]:
>>                         self._get_list(path, i)
>>                 return
>> 
>>         #生成模板值
>>         #values应为字典的字典。即每一个模板元素如果引用有外部的变量,那么在values中应有此模板元素的一个键。
>>         #同时它的值应为所有外部变量的一个字典
>>         def value(self, target='main', values=None):
>>                 path = []
>>                 self._get_list(path, target)
>>                 path.reverse()
>>                 vals = {}
>>                 for i in path:
>>                         value = self._getElementValue(i, vals, values)
>>                         vals[i] = value
>>                 return vals[target]
>> 
>>         def _replace(self, text, values):
>>                 def dosup(matchobj, values=values):
>>                         if values:
>>                                 return values.get(matchobj.groups()[0], matchobj.group())
>>                         else:
>>                                 return matchobj.group()
>>                 #先将所有已经存在的模板元素进行替换
>>                 return re.sub(r'%s(\w+)%s' % (self.beginchars, self.endchars), dosup, text)
>> 
>>         def _getElementValue(self, name, elements, values=None):
>>                 text = self.vars[name].getText()
>>                 text = self._replace(text, elements)
>>                 #再根据外部引用变量的类型决定是否进行循环
>>                 if values and values.has_key(name):
>>                         if len(values[name]) == 1:      #是字典则只进行一次替换
>>                                 text = self._replace(text, values[name])
>>                         else:
>>                                 s = []
>>                                 for v in values[name]:
>>                                         s.append(self._replace(text, v))
>>                                 text = ''.join(s)
>> 
>>                 return text
>> 
>> if __name__ == '__main__':
>>         vars = dict(hello=[{'var':'var1'},{'var':'var2'},{'var':'var3'}])
>>         template = Template()
>>         template.load('tmp2')
>>         print template.value('program', vars)
>> 
>> 测试模板:
>> 
>> from Template import T
>> 
>> hello = T("Hello, <#var#> \n")
>> message = T("Please input yourname:")
>> program = T("""name = input_raw("<#message#>")
>> print '''<#hello#>'''
>>         """)
>> 
>>>> 礼!
>> 
>> 
>> limodou
>> chatme at 263.net
>> 2004-08-24
>> 
>> 
>> _______________________________________________
>> python-chinese list
>> python-chinese at lists.python.cn
>> http://python.cn/mailman/listinfo/python-chinese
>> 
>> 
>
>
>-- 
>HD(燃烧中的火)
>我工作我快乐,我勤奋我收获。请与我一起快乐,与我一起收获。
>_______________________________________________
>python-chinese list
>python-chinese at lists.python.cn
>http://python.cn/mailman/listinfo/python-chinese

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

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


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

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

    你的回复:

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

    Zeuux © 2025

    京ICP备05028076号