易截截图软件、单文件、免安装、纯绿色、仅160KB

Python Mako Template 学习笔记


Mako是什么?Moko是Python写的一个模板库,Python官网python.org用的就是它哦。其他废话也就不累赘了,直接来点代码,方便阅读与了解把。
(Mako官网地址:http://www.makotemplates.org/ ,可以下载安装包,推荐使用easy_install安装)
from mako.template import Template
mytemplate = Template("hello world!")
print mytemplate.render()
mytemplate = Template("hello, ${name}!")
print mytemplate.render(name="jack")
代码可以参考官方doc部分
mytemplate = Template(filename='/docs/mytmpl.txt')
print mytemplate.render()
还可以从设置模板为文件,设置filename属性
mytemplate = Template(filename='/docs/mytmpl.txt', module_directory='/tmp/mako_modules')
print mytemplate.render()
文件还可以缓存到某个目录下,下面的/docs/mytmpl.txt会产生一个py:/tmp/mako_modules/docs/mytmpl.txt.py
from mako.lookup import TemplateLookup
mylookup = TemplateLookup(directories=['/docs'])
mytemplate = Template("""<%include file="header.txt"/> hello world!""", lookup=mylookup)
查找模板,方便统一模板路径使用。
mylookup = TemplateLookup(directories=['/docs'], module_directory='/tmp/mako_modules')
def serve_template(templatename, **kwargs):
mytemplate = mylookup.get_template(templatename)
print mytemplate.render(**kwargs)
改良了上面的查找方式
mylookup = TemplateLookup(directories=['/docs'], output_encoding='utf-8',
encoding_errors='replace')
mytemplate = mylookup.get_template("foo.txt")
print mytemplate.render()
设置输出编码,以及编码错误时候处理方式
.
来源:"小鱼博客" http://chenxiaoyu.org/blog/


相关文档:

python 之 pil生成验证码图片

#!/usr/bin/python
#coding=utf-8
import Image,ImageDraw,ImageFont,os,string,random,ImageFilter
def initChars():
"""
允许的字符集合,初始集合为数字、大小写字母
usage: initChars()
param: None
return: list
返回允许的字符集和
for: picChecker类初始字符集合
todo: ......

Python正则表达式操作指南[转]

原文出处:http://www.amk.ca/python/howto/regex/
原文作者:A.M. Kuchling (amk@amk.ca)
授权许可:创作共享协议
翻译人员:FireHare
校对人员:Leal
适用版本:Python 1.5 及后续版本
简介
Python 自1.5版本起增加了re 模块,它提供 Perl 风格的正则表达式模式。Python 1.5之前版本则是通过 regex
模块提供 ......

python中os.path.dirname(__file__)的使用

     (1).当"print os.path.dirname(__file__)"所在脚本是以完整路径被运行的, 那么将输出该脚本所在的完整路径,比如:
             python d:\pythonSrc\test\test.py
         &nb ......

python_复杂数据类型

python_复杂数据类型
python中原生的队列有2种,一种是普通的队列(Queue),一种叫做优先队列(PriorityQueue),即小的先出列。
注意:队列是线程安全的,python 3.0中支持多进程,也有类似的Queue,但不是这个。
1栈、队列、堆
  python中原生的队列有2种,一种是普通的队列(Queue),一种叫做优先队列(PriorityQueu ......

【python】如何读取命令行的输出

如果python调用外部程序,需要直接抓去命令行的输出,有什么好的办法呢?
这里我们需要用到 os.popen 这个管道,然后用 read、readline或者readlines来读取命令行输出
#需要执行的命令
strCommand = 'xxxxxxxxxxxxxxxxx'
#用popen来执行命令行
oStdout = os.popen(strCommand)
#假设输出的内容只有一行
strStdout = ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号