是否可以添加一个检查,当生成的配置与移动的行相同时丢弃挂起的配置更改?你知道吗
我之所以这么问是因为在一些脚本中,每次关闭某个应用程序时,我都会保存配置,当我提交文件夹时,我总是会得到this之类的内容,而且我认为这是不必要的I/O加载。你知道吗
以下是我的loadCfg和saveCfg函数:
# I/O #
def loadCfg(path, cfg):
"""
:param path:
:param cfg:
"""
if not os.path.isfile(path) or os.path.getsize(path) < 1:
saveCfg(path, cfg)
cfg = cfg.read(path, encoding='utf-8')
def saveCfg(path, cfg):
"""
:param path:
:param cfg:
"""
with open(path, mode='w', encoding="utf-8") as cfgfile:
cfg.write(cfgfile)
首先让我说我怀疑不必要的I/O负载是否重要,您想做的很可能是过早优化的情况。你知道吗
也就是说,这里有一种方法似乎是可行的——尽管我还没有对它进行彻底的测试,也没有尝试将它合并到
loadCfg()
和saveCfg()
函数中。(其名称不遵循PEP 8 - Style Guide for Python Code推荐的函数命名约定,顺便说一句)。你知道吗基本思想是将初始的
ConfigParser
实例转换为字典并保存它。然后,在关闭应用程序之前,再次执行此操作,并比较before和after字典以确定它们是否相同。你知道吗相关问题 更多 >
编程相关推荐