python自动寻路模板_Python实现的简单模板引擎功能示例
本文实例讲述了Python实现的简单模板引擎功能。分享给大家供大家参考,具体如下:
#coding:utf- 8
__author__="sdm"
__author_email='sdmzhu3@gmail.com'
__date__ ="$2009-8-25 21:04:13$"
'' '
pytpl 类似 php的模板类
'' '
import sys
import StringIO
import os.path
import os
#模 板的缓存
_tpl_cache={}
class Pytpl:
def __init__(self,tpl_path='./' ):
self.tpl_path=tpl_path
self.data={}
self.output = StringIO.StringIO()
pass
def set(self,name,value):
'' '
设 置模板变量
'' '
self.data[name]=value;
pass
def get(self,name):
'' '
得 到模板变量
'' '
t={}
return t.get(name, '' )
pass
def tpl(self,tplname):
'' '
渲 染模板
'' '
f=self.tpl_path+tplname
if not os.path.exists(f):
raise Exception('tpl:[%s] is not exists' % f)
mtime=os.stat(f).st_mtime
if not _tpl_cache.has_key(f) or _tpl_cache[f][ 'time' ]
src_code=self.__compile__(open(f).read())
try :
t=open(f+'.py' , 'w' )
t.write(src_code)
t.close()
except:
pass
py_code=compile(src_code, f+'.py' , 'exec' )
_tpl_cache[f]={'code' :py_code, 'time' :mtime}
else :
py_code= _tpl_cache[f]['code' ]
exec(py_code, {'self' :self}, self.data)
return self.output.getvalue()
def execute(self,code,data,tplname):
'' '
执 行这个模板
'' '
py_file_name=tplname+'.py'
f=open(py_file_name,'w' )
f.write(code)
f.close()
execfile(py_file_name, {'self' :self}, data)
def __compile__ (self,code):
'' '
编 译模板
查找 <?标记
'' '
tlen=len(code);
flag_start=''
flag_end='?>'
# 默认普通标记
status=0
i=0
#分块 标记
pos_end=0
pos_start=0
#缩 进
global indent
indent=0
py_code=[]
def place_t_code(c,t_indent):
'' '
基 本的代码处理
'' '
global indent
if (c[ 0 ]== '=' ):
return ( ' ' * 4 *indent) + 'echo ( /'%s/' % (' +c[ 1 :]+ '))'
lines=c.split("/n" )
t=[]
for i in lines:
indent2=indent
tmp=i.strip(" /n/r" )
c=tmp[len(tmp)-1 :len(tmp)]
# 判定最后一个字符
if (c== '{' ):
indent+=1
tmp=tmp[0 :len(tmp)- 1 ]+ ":"
elif(c=='}' ):
indent-=1
tmp=tmp[0 :len(tmp)- 1 ]
t.append((' ' * 4 *indent2) +tmp )
return "/n" .join(t)
while 1 :
if i>=tlen: break
c=code[i];
if status== 0 :
# 编译加速
pos_start=code.find(flag_start,pos_end);
if (pos_start>- 1 ):
s=code[pos_end:pos_start]
t_code= 'echo ( ' +repr(s)+ ')'
t_code=' ' *indent* 4 +t_code
if s:
py_code.append(t_code)
i=pos_start
last_pos=i
# 进入代码状态
status=1
continue
else :
# 没有没有找到
pos_start=tlen
t_code='echo ( ' +repr(code[pos_end:pos_start])+ ' ) '
t_code=' ' *indent* 4 +t_code
py_code.append(t_code)
break
if status== 1 :
# 查找结束标记
pos_end=code.find(flag_end,i)
if (pos_end>- 1 ):
# 需要跳过 这个标记
t_code=place_t_code(code[pos_start+2 :pos_end],indent)
# 跳过?>结束标记
pos_end+=2
py_code.append(t_code)
else :
# 没查找到直接结束
pos_end=tlen
# 需要跳过 这个标记
t_code=place_t_code(code[pos_start+2 :pos_end],indent)
py_code.append(t_code)
break
status=0
i=pos_end
pass
i+=1
py_code_str="#coding:utf-8/nimport sys;global echo;echo=self.output.write/n"
py_code_str+="/n" .join(py_code)
py_code_str=py_code_str.replace("/t" , " " )
return py_code_str
def test():
tpl=Pytpl('./' );
tpl.set('title' , '标题3' )
print tpl.tpl('test.html' )
pass
if __name__ == "__main__" :
test()
希望本文所述对大家Python程序设计有所帮助。
python自动寻路模板_Python实现的简单模板引擎功能示例相关推荐
- python微信红包_Python实现的微信红包提醒功能示例
本文实例讲述了Python实现的微信红包提醒功能.分享给大家供大家参考,具体如下: #coding=utf-8 import itchat from itchat.content import TEX ...
- python 微信数据_Python实现的微信好友数据分析功能示例
本文实例讲述了python实现的微信好友数据分析功能.分享给大家供大家参考,具体如下: 这里主要利用python对个人微信好友进行分析并把结果输出到一个html文档当中,主要用到的python包为it ...
- php计算器按钮功能,PHP简单在线计算器功能示例
PHP简单在线计算器功能示例 PHP可以被嵌入于HTML语言,它相对于其他语言.编辑简单,实用性强,更适合初学者.下面是小编分享的PHP简单在线计算器功能示例,一起来看一下吧. 简单的计算器(www. ...
- python3 模板引擎_Python实现的简单模板引擎功能示例
本文实例讲述了Python实现的简单模板引擎功能.分享给大家供大家参考,具体如下: #coding:utf- 8 __author__="sdm" __author_email=' ...
- php模板引擎 例子,PHP实现简单的模板引擎功能示例
本文实例讲述了PHP实现简单的模板引擎功能.分享给大家供大家参考,具体如下: php web开发中广泛采取mvc的设计模式,controller传递给view层的数据,必须通过模板引擎才能解析出来.实 ...
- qtp xml联合xsl输出html报表,通过xml和xsl实现数据和页面展示模板的解耦(简单完整网站代码示例)...
一.示例简介 该示例通过xml配置数据源,在其xsl样式模板中配置数据源的展示模板,从而达到数据和页面模板解耦,降低前端和后端开发的依赖,相比于传统的HTML+CSS的实现页面模板展示,数据和模板解耦 ...
- python写排列组合_Python实现的简单排列组合算法示例
本文实例讲述了Python实现的简单排列组合算法.分享给大家供大家参考,具体如下: 1.python语言简单.方便,其内部可以快速实现排列组合算法,下面做简单介绍 2.一个列表数据任意组合 主要是利用 ...
- python给excel排序_Python实现EXCEL表格的排序功能示例
Python实现EXCEL表格的排序功能示例 EXCEL的数值排序功能还是挺强大的,升序.降序,尤其自定义排序,能够对多个字段进行排序工作. 那么,在Python大法中,有没有这样强大的排序功能呢?答 ...
- python微信红包代码_Python实现的微信红包提醒功能示例
本文实例讲述了Python实现的微信红包提醒功能.分享给大家供大家参考,具体如下: #coding=utf-8 import itchat from itchat.content import TEX ...
最新文章
- json省市区城市级联
- 《Adobe Illustrator CC经典教程》—第0课0.1节简介
- java基础语法以及进制的转换
- Vue过滤器的简单使用--实时显示格式化的时间
- SSH无密码登录:只需两个简单步骤 (Linux)
- python输入一个数组输出24进制式的时间_【翻译】《利用Python进行数据分析·第2版》第4章(下)NumPy基础:数组和矢量计算...
- 掌控谈话~让对方说“你说得对
- 引入静态变量_Common Lisp变量的一些事情
- 2021年中国电影营销数字化发展分析
- RequireJS模块的建立:插件化体验 - demo演示篇
- 为GridView-Delete列添加确认对话框(2种方法)
- 从Java程序员到架构师,从工程师到技术专家,迷茫之路
- 云台山风景区,感受人生最美的风景
- Windows如何设置右键快捷键
- [附源码]java毕业设计学校缴费系统
- 利用Python实现PDF转文本,就是如此简单!
- oracle10g lsnrctl,linux下oracle10g lsnrctl没反应 sqlplus正常 bug4518443
- 第二篇 算法概述及复杂度
- 计算机应用用蒙语怎么写,100句常用蒙古语 - 百度文库
- centos7下vim的开箱使用与简单配置分享
热门文章
- 管道过滤器(Pipe-And-Filter)模式
- 李天平×××作诞生记——《亮剑.NET:.NET深入体验与实战精要》
- 1582年日历怎么了_【知乎周边】知乎2020年日历开箱+测评
- rsem比对_RSEM方法比对和表达量计算
- 新款苹果电脑_苹果真牛!iPhone的软件,苹果电脑上也照样可用了
- Spring AOP两种使用方式以及如何使用解析
- mysql 跳表 b 树_简单谈谈Mysql索引与redis跳表
- python树莓派 是什么_用树莓派和Python给你的植物浇水
- c bitset get_Java BitSet get()方法与示例
- 退火算法 贪婪算法_算法贪婪策略简介