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/
相关文档:
1,不用修改/etc/vim下的vimrc及gvimrc文件 。。。
2,在~目录下,新建一个.vimrc的配置文件。内容如下:(高亮,自动对齐,自动缩进,显示行号)
" An example for a vimrc file.
"
" Maintainer: Bram Moolenaar <Bram@vim.org>
" Last change: 2006 Nov ......
Python快速入门
目录
1. 第一章 Python快速入门
&nbs ......
用gcc编译了一个C++调用python的程序,这个程序在VS下是好用的,而且没有使用vs的任何库
可是到了gcc下就是无法使用
后来上网查了一下资料才知道,是因为cl与gcc的运行时库不同。
打开cmd窗口,输入python就可以看到
Python 3.0 (r30:67507, Dec 3 2008, 20:14:27) [MSC v.1500 32 bit (Intel)] on win32
Type " ......
Python支持ascii字符串,unicode字符串,以及各种字符集,那么它们到底各是什么概念,相互之间存在何种关系呢?
在Python中,ascii字符串,即str类型的值,可能用来表示任意的一块存储空间,那么也就是说,这个字符串内部可以是任何值,例如:可见字符组成的字符串,或者一段二进制数据等。unicode字符串,即unicode类型的 ......
如果python调用外部程序,需要直接抓去命令行的输出,有什么好的办法呢?
这里我们需要用到 os.popen 这个管道,然后用 read、readline或者readlines来读取命令行输出
#需要执行的命令
strCommand = 'xxxxxxxxxxxxxxxxx'
#用popen来执行命令行
oStdout = os.popen(strCommand)
#假设输出的内容只有一行
strStdout = ......