@ show0kは、私の質問に(マジックメソッドに関して)正しい答えを与えました。どうもありがとう! :)
この回答は、少し深く掘り下げてくれました。私はのカスタム例外ハンドラを定義できるIPythonメソッドを見つけました。
from IPython.core.ultratb import AutoFormattedTB # initialize the formatter for making the tracebacks into strings itb = AutoFormattedTB(mode = 'Plain', tb_offset = 1) # this function will be called on exceptions in any cell def custom_exc(shell, etype, evalue, tb, tb_offset=None): # still show the error within the notebook, don't just swallow it shell.showtraceback((etype, evalue, tb), tb_offset=tb_offset) # grab the traceback and make it into a list of strings stb = itb.structured_traceback(etype, evalue, tb) sstb = itb.stb2text(stb) print (sstb) # <--- this is the variable with the traceback string print ("sending mail") send_mail_to_myself(sstb) # this registers a custom exception handler for the whole current notebook get_ipython().set_custom_exc((Exception,), custom_exc)
だから、これはどのノートブックの上部にある単一のセルに入れることができ、結果としてそれが何かがうまくいかない場合の郵送を行います。 は、私はそれがこのように動作するようになりました。 自己紹介/ TODO:ノートに取り込み、ラインマジックで起動できる小さなPythonモジュールにこのスニペットを作成します。
ご注意ください。ドキュメントには、このset_custom_exc
メソッドの警告が含まれています: "警告:独自の例外ハンドラをIPythonのメイン実行ループに入れることで、クラッシュする可能性が非常に高くなります。 "
OPが提起された電子メール*を受信したい場合、 'try ... finally ...'ブロックがより適しているかもしれません。 'try ... except ...'では、例外を処理することが期待されています。今はそうではありません。もう一つの方法は、それを再評価することです。 IMO、「最終的に」はより強固です。 – x0s
@ x0sを 'try ... finally ...'ブロックで実行すると、実行されたすべてのセルに対してメールが送信されます。 OPが望むものではありません。しかし、それを再評価することは良い考えです。私は編集しました。 – show0k