日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python自动寻路模板_Python实现的简单模板引擎功能示例

發布時間:2025/3/11 python 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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实现的简单模板引擎功能示例的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。