pylint如何记住以前运行的评分?
在一次普通的pylint运行中,我们会得到以下输出:
Global evaluation
-----------------
Your code has been rated at 9.50/10 (previous run: 8.50/10)
Duplication
-----------
+-------------------------+------+---------+-----------+
| |now |previous |difference |
+=========================+======+=========+===========+
|nb duplicated lines |0 |0 |= |
+-------------------------+------+---------+-----------+
|percent duplicated lines |0.000 |0.000 |= |
+-------------------------+------+---------+-----------+
我想知道pylint是怎么记住之前运行的分数的——在上面的例子中,之前的分数是8.5。
我想在自己的模块中实现这样的功能,所以我想先了解一下pylint是怎么做到的。
我搜索了一下可能存放这些数据的隐藏文件夹,但没有找到。
2 个回答
1
根据这里的内容,你可以了解到:
分析数据会被存储为一个叫做“pickle”的文件,这个文件会放在一个特定的文件夹里,具体位置是根据以下规则来决定的:
如果设置了PYLINTHOME这个环境变量,就会使用它的值。
如果没有设置这个环境变量,就会在用户的XDG_CACHE_HOME目录下找一个叫“pylint”的子文件夹,具体路径如下:
在Linux系统上,路径是“~/.cache/pylint”。
在macOS系统上,路径是“~/Library/Caches/pylint”。
在Windows系统上,路径是“C:\Users\AppData\Local\pylint”。
如果以上都没有,就会在当前目录下找一个叫“.pylint.d”的文件夹。
15
我在我的主目录里有一个 .pylintrc
文件,里面包含了以下内容:
#pickle collected data for later comparisons.
persistent=yes
所以看起来 pylint
确实使用了 pickle 来进行比较。
在源代码中的 lint.py
文件里:
def make_options():
return (('ignore',
{'type' : 'csv', 'metavar' : '<file>[,<file>...]',
'dest' : 'black_list', 'default' : ('CVS',),
'help' : 'Add files or directories to the blacklist. '
'They should be base names, not paths.'}),
('persistent',
{'default': True, 'type' : 'yn', 'metavar' : '<y_or_n>',
'level': 1,
'help' : 'Pickle collected data for later comparisons.'})
完整的 lint.py
源代码可以在 这里 找到。
最有趣的部分可能是这个方法:
def close(self):
"""close the whole package /module, it's time to make reports !
if persistent run, pickle results for later comparison
"""
if self.file_state.base_name is not None:
# load previous results if any
previous_stats = config.load_results(self.file_state.base_name)
# XXX code below needs refactoring to be more reporter agnostic
self.reporter.on_close(self.stats, previous_stats)
if self.config.reports:
sect = self.make_reports(self.stats, previous_stats)
if self.config.files_output:
filename = 'pylint_global.' + self.reporter.extension
self.reporter.set_output(open(filename, 'w'))
else:
sect = Section()
if self.config.reports or self.config.output_format == 'html':
self.reporter.display_results(sect)
# save results if persistent run
if self.config.persistent:
config.save_results(self.stats, self.file_state.base_name)
else:
self.reporter.on_close(self.stats, {})
你还想看看 config.py
的 源代码。
def load_results(base):
"""try to unpickle and return data from file if it exists and is not
corrupted
return an empty dictionary if it doesn't exists
"""
data_file = get_pdata_path(base, 1)
try:
with open(data_file, _PICK_LOAD) as stream:
return pickle.load(stream)
except:
return {}