最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

本文實例講述了Python實現(xià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í) 行這個模板
    '' '
    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
          # 進入代碼狀態(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é)束標記
        pos_end=code.find(flag_end,i)
        if (pos_end>- 1 ):
          # 需要跳過<? 這個標記
          t_code=place_t_code(code[pos_start+2 :pos_end],indent)
          # 跳過?>結(jié)束標記
          pos_end+=2
          py_code.append(t_code)
        else :
          # 沒查找到直接結(jié)束
          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()

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

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

相關(guān)文章

  • Python用csv寫入文件_消除空余行的方法

    Python用csv寫入文件_消除空余行的方法

    今天小編就為大家分享一篇Python用csv寫入文件_消除空余行的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-07-07
  • 詳解tensorflow之過擬合問題實戰(zhàn)

    詳解tensorflow之過擬合問題實戰(zhàn)

    這篇文章主要介紹了詳解tensorflow之過擬合問題實戰(zhàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-11-11
  • python逆向之pyc反編譯的使用教程

    python逆向之pyc反編譯的使用教程

    python代碼的運行是靠python解析器將源代碼轉(zhuǎn)換為字節(jié)碼,本文主要介紹了python逆向之pyc反編譯的使用教程,具有一定的參考價值,感興趣的可以了解一下
    2024-03-03
  • pandas實現(xiàn)處理TB級別的數(shù)據(jù)

    pandas實現(xiàn)處理TB級別的數(shù)據(jù)

    這篇文章主要介紹了pandas實現(xiàn)處理TB級別的數(shù)據(jù),具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2025-04-04
  • Pandas時間序列重采樣(resample)方法中closed、label的作用詳解

    Pandas時間序列重采樣(resample)方法中closed、label的作用詳解

    這篇文章主要介紹了Pandas時間序列重采樣(resample)方法中closed、label的作用詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-12-12
  • Pandas如何將Timestamp轉(zhuǎn)為datetime類型

    Pandas如何將Timestamp轉(zhuǎn)為datetime類型

    這篇文章主要介紹了Pandas如何將Timestamp轉(zhuǎn)為datetime類型,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-07-07
  • 利用python來跟蹤ip地址的方法

    利用python來跟蹤ip地址的方法

    今天來介紹一個流行的 python庫 ip2geotools,使用它可以確定 IP地址 對應(yīng)的 國家、地區(qū)、城市、緯度和經(jīng)度等,文中通過代碼示例介紹了如何使用python來跟蹤ip地址,需要的朋友可以參考下
    2023-06-06
  • 將tensorflow模型打包成PB文件及PB文件讀取方式

    將tensorflow模型打包成PB文件及PB文件讀取方式

    今天小編就為大家分享一篇將tensorflow模型打包成PB文件及PB文件讀取方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-01-01
  • pytorch 圖像中的數(shù)據(jù)預處理和批標準化實例

    pytorch 圖像中的數(shù)據(jù)預處理和批標準化實例

    今天小編就為大家分享一篇pytorch 圖像中的數(shù)據(jù)預處理和批標準化實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-01-01
  • 利用pip安裝python第三方庫的4種方法

    利用pip安裝python第三方庫的4種方法

    Python最強大的功能之一是能夠十分方便地使用性能優(yōu)異的第三方庫,這些庫涵蓋了大量的不同領(lǐng)域,下面這篇文章主要給大家介紹了關(guān)于利用pip安裝python第三方庫的4種方法,需要的朋友可以參考下
    2023-02-02

最新評論

涟水县| 塘沽区| 晋城| 永仁县| 高安市| 隆德县| 长垣县| 报价| 张家口市| 高密市| 舒城县| 景德镇市| 巢湖市| 祁门县| 甘南县| 天镇县| 和田市| 遵义市| 怀化市| 白山市| 大城县| 镇安县| 固始县| 隆回县| 嫩江县| 哈密市| 南康市| 定兴县| 宜良县| 如东县| 陆丰市| 巨野县| 葵青区| 广饶县| 宁陕县| 灵寿县| 南通市| 钟祥市| 阿克苏市| 红安县| 旺苍县|