もた日記

くだらないことを真面目にやる

Pythonメモ : better-exceptionsで例外情報を見やすくする

better-exceptions



github.com

better-exceptionsを使用すると例外情報が図のようにみやすくなる。

f:id:wonder-wall:20170724160321p:plain

インストー


pipでインストールできるので下記コマンドを実行。

$ pip install better_exceptions


使い方


以下のようにbetter_exceptionsをインポートすればよい。

import better_exceptions

試しに下記コードを実行した場合の出力を比べてみる。

import better_exceptions

foo = 52


def shallow(a, b):
    deep(a + b)


def deep(val):
    global foo
    assert val > 10 and foo == 60


bar = foo - 50
shallow(bar, 15)
shallow(bar, 2)



better_exceptionsをインポートした場合。

f:id:wonder-wall:20170724160743p:plain

better_exceptionsをインポートしない場合。

f:id:wonder-wall:20170724160757p:plain

出力が長い場合は省略されるようなので、省略したくない場合は下記コードを追加する。

better_exceptions.MAX_LENGTH = None