折腾:
期间,希望实现,把代码中一些配置项,比如
输出文件夹叫做output等
作为可配置的项,放到配置文件中,然后用python去加载配置文件,读取配置
python config file
python config file best practices
果然很我猜的类似:
就是用json文件,然后python可以用json库加载和解析出配置
python中有专门的解析.cfg的ConfigParser
不过明显用起来很麻烦。
支持ini的配置,但是也不够灵活。
【总结】
目前用:# init and parse config
with open("config.json") as configJsonFile:
# <open file "config.json", mode "r" at 0x10e868390>
gCfg = json.load(configJsonFile)
#{u"befrugal": {u"username": u"xxx", u"password": u"yyy"}, u"outputFolder": u"output"}
对应配置文件config.json内容:{
"outputFolder" : "output",
"befrugal" : {
"username" : "xxx",
"password" : "yyy"
}
}
实现了基本的读取和解析配置文件,后续就可以得到dict的变量的配置变量去使用了:# init output folder
if not os.path.exists(gCfg["outputFolder"]):
os.makedirs(gCfg["outputFolder"])
# init logging
filenameNoSufx = crifanLib.getInputFileBasenameNoSuffix()
logFilename = os.path.join(gCfg["outputFolder"], filenameNoSufx + ".log")
crifanLib.loggingInit(logFilename)
# logging.info("filenameNoSufx=%s", filenameNoSufx)
logging.info("Current log filename: %s", logFilename)
logging.info("Current config: %s", gCfg)