トップ «前の日記(2017/07/03) 最新 次の日記(2017/07/06)» 編集

kaztomo日記


2017/07/04

_ [Python]IPython 結構便利じゃん

Pythonを使いはじめて約半年。今更ながらに IPython というインタラクティブシェルの 活用をはじめました (^^;;

「結構おんなじ事を書くんだよなぁ。 jupyter notebook ならセルを再度選択して実行するだけなのに、 コマンドラインの python は面倒くさいんだよなぁ」

なんて思っていたけど、IPython をよくよく調べてみると、 履歴も簡単に辿れるし出力結果の再確認も楽だと感じました。

更に macro の便利さに脱帽。

  • つらつらと入力したコマンドに対して、macro を設定する
In [1]: from math import *
In [2]: log(1.0)
Out[2]: 0.0

In [3]: %macro test 1-2
Macro `test` created. To execute, type its name (without quotes).
=== Macro contents: ===
from math import *
log(1.0)
  • マクロを保存する
In [4]: %store test
Stored 'test' (Macro)
  • 登録されている内容を確認してみる
In [5]: %macro
Out[5]: [u'test']

In [6]: print test
from math import *
log(1.0)
  • 一度、IPython を終了してから再度起動し、保存したマクロを読み込んで見る
In [1]: %macro
Out[1]: []

In [2]: %store -r test

In [3]: %macro
Out[3]: [u'test']

In [4]: print test
from math import *
log(1.0)

便利すぎる\(^o^)/


参考にさせて頂いたサイト: