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

對(duì)python實(shí)現(xiàn)模板生成腳本的方法詳解

 更新時(shí)間:2019年01月30日 10:24:06   作者:像風(fēng)一樣的自由  
今天小編就為大家分享一篇對(duì)python實(shí)現(xiàn)模板生成腳本的方法詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧

最近項(xiàng)目需要,針對(duì)主項(xiàng)目提取一個(gè)小的基礎(chǔ)版本,供于在新建項(xiàng)目時(shí)使用,所以就有這個(gè)python模板生成腳本,其作用如下:

1、通過(guò)配置文件來(lái)控制模板中的數(shù)據(jù)、格式化的過(guò)濾條件

2、執(zhí)行后會(huì)把目錄下所有的文件都會(huì)執(zhí)行一篇

#!/usr/bin/python
#encoding: utf-8
 
import json
import codecs
import os
 
def get_files(root_path):
  for dir in os.walk(root_path):
    if dir[2]:
      for nf in dir[2]:
        yield os.path.join(dir[0], nf)
 
def exclude_filter(exclude, nfile):
  files_path = exclude.get('file_path')
  files_name = exclude.get('file_name')
  base_name = os.path.basename(nfile)
  exts_name = exclude.get('ext_name')
  base_ext_name = base_name.rsplit(".", 1)[1]
  if files_path:
    for npath in files_path:
      if npath==nfile:
        return True
  elif files_name:
    for name in files_name:
      print name, base_name
      if name==base_name:
        return True
  elif exts_name:
    for name in exts_name:
      print name, base_ext_name
      if name==base_ext_name:
        return True
 
def include_filter(include, nfile):
  files_path = include.get('file_path')
  files_name = include.get('file_name')
  base_name = os.path.basename(nfile)
  if files_path:
    for npath in files_path:
      if npath==nfile:
        return True
  elif files_name:
    for name in files_name:
      if name==base_name:
        return True
 
def main():
  # read config
  config = {}
  with codecs.open("config.json","rb","UTF-8") as f:
    config = json.loads(f.read())
  if not config:
    return
 
  template = config.get("template")
  if template and template.get('path'):
    root_path = template.get('path')
    if not os.path.exists(root_path):
      print "source path not exist"
      return
    root_path = os.path.abspath(root_path)
    old_path = os.path.dirname(root_path)
  else:
    return
  exclude = template.get('exclude')
  include = template.get('include')
 
  store = config.get("store")
  if not store or not os.path.exists(store.get('dir_path', '')):
    return
 
  data = config.get("data")
  if not data:
    return
 
  if not os.path.exists(root_path):
    print 'root path not exists'
    return
 
  if os.path.isfile(root_path):
    files = [root_path]
  else:
    base_name = os.path.basename(root_path)
    store_root_path = os.path.join(store.get('dir_path'), base_name)
    if not os.path.exists(store_root_path):
      os.mkdir(store_root_path)
    files = get_files(root_path)
 
  for nfile in files:
    print nfile
    try:
      with codecs.open(nfile, "rb", "UTF-8") as f:
        s = f.read()
 
      if not exclude_filter(exclude, nfile) or include_filter(include, nfile):
        s = s % data
    except:
      with codecs.open(nfile, "rb") as f:
        s = f.read()
 
    # save to file
    fn = nfile.replace(old_path, store.get('dir_path'))
    fn_dir = os.path.dirname(fn)
    if not os.path.exists(fn_dir):
      os.makedirs(fn_dir)
    try:
      with codecs.open(fn, "wb", "UTF-8") as f:
        f.write(s)
        f.flush()
    except:
      with codecs.open(fn, "wb") as f:
        f.write(s)
        f.flush()
 
if __name__ == '__main__':
  main()

配置文件:

{
 "template": {
  "path" : "D:/tunicorn-web/framework-template",  ##模板文件主目錄
  "exclude" : {                  ##不進(jìn)行模板格式化的文件
   "file_path" : [],  
   "file_name" : ["config.json", "make_project.py"], 
   "ext_name" : ["css", "woff2"],
   "file_type" : [],
   "regex" : []
  },
  "include" : {                  ##進(jìn)行模板格式化的文件
   "file_path" : [],
   "file_name" : []
  }
 },
 "store":{
  "dir_path" : "e:/test"             ##輸出路徑主目錄     
  "data" : {
  "project_name":"NewJAVA",            ##模板數(shù)據(jù)
  "project_prefix":"newjava"           ##模板數(shù)據(jù)
 }
}

執(zhí)行操作:

1、安裝了python環(huán)境

2、雙擊python腳本

3、然后在執(zhí)行下README中的步驟

readme:

README
=============

腳本使用
-------------
1. 打開(kāi)config.json文件
2. 配置相關(guān)信息[輸出目錄、項(xiàng)目名稱、項(xiàng)目前綴]
3. 執(zhí)行make_project.py腳本
4. 查看輸出目錄

以上這篇對(duì)python實(shí)現(xiàn)模板生成腳本的方法詳解就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Python中實(shí)現(xiàn)參數(shù)類型檢查的簡(jiǎn)單方法

    Python中實(shí)現(xiàn)參數(shù)類型檢查的簡(jiǎn)單方法

    這篇文章主要介紹了Python中實(shí)現(xiàn)參數(shù)類型檢查的簡(jiǎn)單方法,本文講解使用裝飾器實(shí)現(xiàn)參數(shù)類型檢查并給出代碼實(shí)例,需要的朋友可以參考下
    2015-04-04
  • python基于property()函數(shù)定義屬性

    python基于property()函數(shù)定義屬性

    這篇文章主要介紹了python基于property()函數(shù)定義屬性,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-01-01
  • Python實(shí)現(xiàn)圖片裁剪的兩種方式(Pillow和OpenCV)

    Python實(shí)現(xiàn)圖片裁剪的兩種方式(Pillow和OpenCV)

    這篇文章主要介紹了Python實(shí)現(xiàn)圖片裁剪的兩種方式(Pillow和OpenCV),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-10-10
  • TensorFlow實(shí)現(xiàn)非線性支持向量機(jī)的實(shí)現(xiàn)方法

    TensorFlow實(shí)現(xiàn)非線性支持向量機(jī)的實(shí)現(xiàn)方法

    本篇文章主要介紹了TensorFlow實(shí)現(xiàn)非線性支持向量機(jī)的實(shí)現(xiàn)方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-04-04
  • Django User 模塊之 AbstractUser 擴(kuò)展詳解

    Django User 模塊之 AbstractUser 擴(kuò)展詳解

    這篇文章主要介紹了Django User 模塊之 AbstractUser 擴(kuò)展詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-03-03
  • matplotlib中plt.hist()參數(shù)解釋及應(yīng)用實(shí)例

    matplotlib中plt.hist()參數(shù)解釋及應(yīng)用實(shí)例

    本文主要介紹了matplotlib中plt.hist()參數(shù)解釋及應(yīng)用實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-08-08
  • tensorflow如何將one_hot標(biāo)簽和數(shù)字(整數(shù))標(biāo)簽進(jìn)行相互轉(zhuǎn)化

    tensorflow如何將one_hot標(biāo)簽和數(shù)字(整數(shù))標(biāo)簽進(jìn)行相互轉(zhuǎn)化

    這篇文章主要介紹了tensorflow如何將one_hot標(biāo)簽和數(shù)字(整數(shù))標(biāo)簽進(jìn)行相互轉(zhuǎn)化問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-06-06
  • Python調(diào)用VBA實(shí)現(xiàn)保留原始樣式的表格合并方法

    Python調(diào)用VBA實(shí)現(xiàn)保留原始樣式的表格合并方法

    本文主要介紹了Python調(diào)用VBA實(shí)現(xiàn)保留原始樣式的表格合并方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-01-01
  • Python數(shù)據(jù)持久化存儲(chǔ)實(shí)現(xiàn)方法分析

    Python數(shù)據(jù)持久化存儲(chǔ)實(shí)現(xiàn)方法分析

    這篇文章主要介紹了Python數(shù)據(jù)持久化存儲(chǔ)實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了Python基于pymongo及mysql模塊的數(shù)據(jù)持久化存儲(chǔ)操作相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下
    2019-12-12
  • python使用RNN實(shí)現(xiàn)文本分類

    python使用RNN實(shí)現(xiàn)文本分類

    這篇文章主要為大家詳細(xì)介紹了python使用RNN進(jìn)行文本分類,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-05-05

最新評(píng)論

宜黄县| 历史| 通江县| 贞丰县| 新和县| 南城县| 含山县| 环江| 中超| 突泉县| 固安县| 威海市| 芜湖市| 托里县| 嵩明县| 库尔勒市| 柳林县| 观塘区| 星子县| 武冈市| 吉木萨尔县| 渭源县| 綦江县| 葫芦岛市| 元阳县| 徐汇区| 江津市| 鹤山市| 西昌市| 班玛县| 拜城县| 巴中市| 霍林郭勒市| 基隆市| 金秀| 安化县| 彰武县| 东光县| 法库县| 台中市| 大庆市|