【问题标题】:pycodestyle retrieve results without printing thempycodestyle 检索结果而不打印它们
【发布时间】:2018-06-28 12:36:35
【问题描述】:

我正在尝试在 python 程序中使用pycodestyle

from pycodestyle import StyleGuide, StandardReport

CONFIG_FILE = 'setup.cfg'


def check_pycodestyle():
    style_guide = StyleGuide(config_file=CONFIG_FILE, quiet=True)
    # style_guide = StyleGuide(config_file=CONFIG_FILE, reporter=StandardReport, quiet=True)
    report = style_guide.check_files(paths='./')

不幸的是,我还没有找到将结果作为简单返回值的方法。使用quiet=True 标志时,不会自动打印任何内容,但返回的报告是BaseReport,似乎不包含实际结果,而只是计算total_errors 的数量。

当省略quiet 标志时,会自动打印结果。

是否有检索结果而不打印结果的功能?

编辑: 使用report.get_file_results() 时,我收到以下结果:

quiet=True:

0

没有quiet

<prints the results>
0 

【问题讨论】:

  • 举一个report.get_file_results()返回的例子
  • 我添加了输出

标签: python pycodestyle


【解决方案1】:

由于结果是“打印”的,您可以像这样重定向打印:

sys.stdout = open(r'C:/YourResultFilePath/Filename.txt', 'w')  # this is 'w' so the previous file is replaced

我建议保存 sys.stdout,以便您可以在代码末尾恢复它:

old_stdout = sys.stdout

然后:

sys.stdout = old_stdout

【讨论】:

    猜你喜欢
    • 2017-09-29
    • 2011-11-09
    • 2020-06-07
    • 1970-01-01
    • 1970-01-01
    • 2012-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多