在典型的 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 是如何实现该功能的。
我搜索了可以存储这些数据的隐藏文件夹,但没有找到。
我的主目录中有一个
.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 {}
可以在这里阅读:
分析数据作为 pickle 文件存储在以下目录中 使用以下规则进行本地化:
- PYLINTHOME 环境变量的值(如果设置)
- 如果设置了环境变量,则为用户 XDG_CACHE_HOME 的“pylint”子目录,否则
- Linux:“~/.cache/pylint”
- macOS:“~/Library/Caches/pylint”
- Windows:“C:UsersAppDataLocalpylint”
- 当前目录下的“.pylint.d”目录