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/
Ïà¹ØÎĵµ£º
#!/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: ......
1¡¢ÔÚPythonÖÐ×î»ù±¾µÄÊý¾Ý½á¹¹ÊÇÐòÁУ¨sequence£©£¬ÐòÁÐÖеÄÿ¸öÔªËØ±»·ÖÅäÒ»¸öÊý×Ö——ÔªËØµÄλÖã¬Ò²½ÐË÷Òý¡£µÚÒ»¸öË÷ÒýÊÇ 0£¬µÚ¶þ¸öË÷ÒýÊÇ1£¬Èç´ËµÝÍÆ¡£ÐòÁÐÖеÄ×îºóÒ»¸öÌõÄ¿±»±ê¼ÇΪ-1£¬µ¹ÊýµÚ¶þÊÇ-2£¬Èç´ËµÝÍÆ¡£
2¡¢ÐòÁУºeg:edward = ['Edward Gumby', 42]£¬ÐòÁÐÒ²Äܰüº¬ÆäËûµÄÐòÁС£Ð ......
(1).µ±"print os.path.dirname(__file__)"ËùÔڽű¾ÊÇÒÔÍêÕû·¾¶±»ÔËÐеģ¬ ÄÇô½«Êä³ö¸Ã½Å±¾ËùÔÚµÄÍêÕû·¾¶£¬±ÈÈ磺
python d:\pythonSrc\test\test.py
&nb ......