2004年08月24日 星期二 13:48
多谢多谢 --- hoxide <hoxide_dirac at yahoo.com.cn> 的正文: > CHEN Guang \(Oliver\),您好! > > 没办法附件太大了,书名叫,<> Tkinter>>, 是chm, 你自己搜一下吧,也许对你有用 > > > ======= 2004-08-22 19:49:07 您在来信中写道:======= > > > > >各位大侠: > > 小弟急需 Tkinter.Canvas 的参考资料,从 Google > >查到《 Python and Tkinter Programming > >》一书的价绍,觉得甚为合适,不知哪位大侠如有它的电子版,能否发给小弟一份?当然,其他有关 > >Tkinter.Canvas 的参考资料小弟也很需要啊。 > > > >_________________________________________________________ > >Do You Yahoo!? > >150万曲MP3疯狂搜,带您闯入音乐殿堂 > >http://cn.rd.yahoo.com/mail_cn/tag/yisou/music/*http://music.yisou.com/ > >美女明星应有尽有,搜遍美图、艳图和酷图 > >http://cn.rd.yahoo.com/mail_cn/tag/yisou/image/*http://image.yisou.com > >1G就是1000兆,雅虎电邮自助扩容! > >http://cn.rd.yahoo.com/mail_cn/tag/1g/*http://cn.mail.yahoo.com/event/mail_1g/ > >_______________________________________________ > >python-chinese list > >python-chinese at lists.python.cn > >http://python.cn/mailman/listinfo/python-chinese > > = = = = = = = = = = = = = = = = = = = = > > > 致 > 礼! > > > hoxide > hoxide_dirac at yahoo.com.cn > 2004-08-22 > > > _______________________________________________ > python-chinese list > python-chinese at lists.python.cn > http://python.cn/mailman/listinfo/python-chinese > _________________________________________________________ Do You Yahoo!? 150万曲MP3疯狂搜,带您闯入音乐殿堂 http://cn.rd.yahoo.com/mail_cn/tag/yisou/music/*http://music.yisou.com/ 美女明星应有尽有,搜遍美图、艳图和酷图 http://cn.rd.yahoo.com/mail_cn/tag/yisou/image/*http://image.yisou.com 1G就是1000兆,雅虎电邮自助扩容! http://cn.rd.yahoo.com/mail_cn/tag/1g/*http://cn.mail.yahoo.com/event/mail_1g/
2004年08月24日 星期二 17:29
python-chinese,您好!
我写了一个小的模板类,现在可以实现嵌套处理,但是不支持根据列表数据而实现模板的循环。
模板类:
import re
import sets
import copy
re_node = re.compile('%\((\w+)\)s')
class Template:
def __init__(self):
pass
def load(self, tplname):
mod = __import__(tplname)
components = tplname.split('.')
for comp in components[1:]:
mod = getattr(mod, comp)
self.vars = {}
self.nodes = {}
for v in dir(mod):
if v.startswith('__') and v.endswith('__'):
continue
self.vars[v] = getattr(mod, v)
self.nodes[v] = list(sets.Set(self._get_rely_on_node(self.vars[v])))
def _get_rely_on_node(self, s): #search for %(name)s format, make a dict
return 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
def value(self, target='main', values=None):
path = []
self._get_list(path, target)
path.reverse()
if not values:
vals = {}
else:
vals = copy.deepcopy(values)
for i in path:
vals[i] = self.vars[i] % vals
return vals[target]
if __name__ == '__main__':
vars = dict(value='aba')
template = Template()
template.load('tmp2')
print template.value('main', vars)
测试文件:
main="""
v = []
%(statement)s
for i in range(10):
%(printvar)s"""
statement = "v.append('%(value)s')\n"
printvar = " print '\\n'.join(%(var)s)\n"
var = 'v'
有兴趣的可以讨论。
现在的问题是对于可循环的模板,比如上面的statement我想根据提供的数据如果为列表或元组时,会自动进行重复。但一个模板可能涉及多个模板变量,如果有多个模板变量为多值该如何处理。按我
的想法,这种多值应该组织为一个元组的列表。不过,不太清楚otter会分析出什么数据来。不知道ZoomQuiet对这个有没有兴趣。
简单说一下用法:
我定义的模板其实是一个模块,不过只是用来定义数据的。
vars = dict(value='aba') #生成模板用到的变量
template = Template() #生成一个模板对象
template.load('tmp2') #装入一个模板
print template.value('main', vars) #得到最终模板的值 当然,模板中还有许多的子模板,都可以得到。
致
礼!
limodou
chatme at 263.net
2004-08-24
Zeuux © 2025
京ICP备05028076号