欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python實(shí)現(xiàn)的簡(jiǎn)單模板引擎功能示例

 更新時(shí)間:2017年09月02日 10:04:13   作者:m190481164  
這篇文章主要介紹了Python實(shí)現(xiàn)的簡(jiǎn)單模板引擎功能,結(jié)合具體實(shí)例形式分析了Python模版引擎的定義與使用方法,需要的朋友可以參考下

本文實(shí)例講述了Python實(shí)現(xiàn)的簡(jiǎn)單模板引擎功能。分享給大家供大家參考,具體如下:

#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):
    '' '
    設(shè) 置模板變量
    '' '
    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' ]<mtime:
      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):
    '' '
    執(zhí) 行這個(gè)模板
    '' '
    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):
    '' '
    編 譯模板
    查找 <?標(biāo)記
    '' '
    tlen=len(code);
    flag_start='<?'
    flag_end='?>'
    # 默認(rèn)普通標(biāo)記
    status=0
    i=0
    #分塊 標(biāo)記
    pos_end=0
    pos_start=0
    #縮 進(jìn)
    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)]
        # 判定最后一個(gè)字符
        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
          # 進(jìn)入代碼狀態(tà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 :
        # 查找結(jié)束標(biāo)記
        pos_end=code.find(flag_end,i)
        if (pos_end>- 1 ):
          # 需要跳過<? 這個(gè)標(biāo)記
          t_code=place_t_code(code[pos_start+2 :pos_end],indent)
          # 跳過?>結(jié)束標(biāo)記
          pos_end+=2
          py_code.append(t_code)
        else :
          # 沒查找到直接結(jié)束
          pos_end=tlen
          # 需要跳過<? 這個(gè)標(biāo)記
          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' , '標(biāo)題3' )
  print tpl.tpl('test.html' )
  pass
if __name__ == "__main__" :
  test()

更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》及《Python入門與進(jìn)階經(jīng)典教程

希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • Python機(jī)器學(xué)習(xí)入門(六)之Python優(yōu)化模型

    Python機(jī)器學(xué)習(xí)入門(六)之Python優(yōu)化模型

    這篇文章主要介紹了Python機(jī)器學(xué)習(xí)入門知識(shí),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-08-08
  • 詳解Python腳本如何設(shè)置試用期

    詳解Python腳本如何設(shè)置試用期

    程序員可能會(huì)私下給別人開發(fā)一些工具,但是通常要給別人試用一下,但是萬一別人試用后,把你拉黑,那就白忙活了。今天就分享如何在 Python 里設(shè)置有效期
    2022-07-07
  • 詳解python中文編碼問題

    詳解python中文編碼問題

    一直以來python中文編碼是個(gè)及其頭大的問題,需要好好學(xué)習(xí)下,我用python為例,簡(jiǎn)單介紹下python編程時(shí)如何處理好中文編碼的問題,感興趣的朋友們可以參考下
    2021-06-06
  • 科學(xué)Python開發(fā)環(huán)境Spyder必知必會(huì)點(diǎn)

    科學(xué)Python開發(fā)環(huán)境Spyder必知必會(huì)點(diǎn)

    這篇文章主要為大家介紹了科學(xué)Python開發(fā)環(huán)境Spyder必知必會(huì)點(diǎn)及使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2024-01-01
  • Python爬蟲:Request Payload和Form Data的簡(jiǎn)單區(qū)別說明

    Python爬蟲:Request Payload和Form Data的簡(jiǎn)單區(qū)別說明

    這篇文章主要介紹了Python爬蟲:Request Payload和Form Data的簡(jiǎn)單區(qū)別說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-04-04
  • Python中的變量與常量

    Python中的變量與常量

    本文基于Python基礎(chǔ),主要介紹了Python基礎(chǔ)中變量和常量的區(qū)別,對(duì)于變量的用法做了詳細(xì)的講解,用豐富的案例 ,代碼效果圖的展示幫助大家更好理解,需要的朋友可以參考下
    2021-11-11
  • Python?對(duì)象拷貝及深淺拷貝區(qū)別的詳細(xì)教程示例

    Python?對(duì)象拷貝及深淺拷貝區(qū)別的詳細(xì)教程示例

    這篇文章主要介紹了Python?對(duì)象拷貝及深淺拷貝區(qū)別的詳細(xì)教程示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-03-03
  • Python利用正則表達(dá)式從字符串提取數(shù)字

    Python利用正則表達(dá)式從字符串提取數(shù)字

    正則表達(dá)式是一個(gè)特殊的字符序列,它能幫助你方便的檢查一個(gè)字符串是否與某種模式匹配,下面這篇文章主要給大家介紹了關(guān)于Python利用正則表達(dá)式從字符串提取數(shù)字的相關(guān)資料,需要的朋友可以參考下
    2022-02-02
  • Python+OpenCV圖像處理——實(shí)現(xiàn)輪廓發(fā)現(xiàn)

    Python+OpenCV圖像處理——實(shí)現(xiàn)輪廓發(fā)現(xiàn)

    這篇文章主要介紹了Python+OpenCV實(shí)現(xiàn)輪廓發(fā)現(xiàn),幫助大家更好的利用python處理圖片,感興趣的朋友可以了解下
    2020-10-10
  • 用python給csv里的數(shù)據(jù)排序的具體代碼

    用python給csv里的數(shù)據(jù)排序的具體代碼

    在本文里小編給大家分享的是關(guān)于用python給csv里的數(shù)據(jù)排序的具體代碼內(nèi)容,需要的朋友們可以學(xué)習(xí)下。
    2020-07-07

最新評(píng)論