IPythonの使い方メモ †概要 †IPythonは、Pythonの対話型インタプリタを拡張したもので、入力補完や変数操作、デバッグに役立つ様々な機能が使用できる。 インストール †pip install ipython lineマジック と cellマジック †IPythonではマジックコマンドと呼ばれる便利な拡張機能があり、1行分のコマンド実行にかかる lineマジックと複数行の実行にかかる cellマジック が利用できる。 例) In [1]: %time var1 = 'test1' CPU times: user 3 µs, sys: 0 ns, total: 3 µs Wall time: 6.2 µs In [2]: %%time ...: count = 0 ...: for i in range(10000): ...: count += 1 ...: CPU times: user 1.18 ms, sys: 3 µs, total: 1.18 ms Wall time: 1.18 ms マジックコマンドのヘルプを表示する †マジックコマンドの一覧表示 †In [1]: %lsmagic Out[1]: Available line magics: %alias %alias_magic %autocall %autoindent %automagic %bookmark %cat %cd %colors %config %cp %cpaste %debug %dhist %dirs %doctest_mode %ed %edit %env %gui %hist %history %killbgscripts %ldir %lf %lk %ll %load %load_ext %loadpy %logoff %logon %logstart %logstate %logstop %ls %lsmagic %lx %macro %magic %matplotlib %mkdir %mv %notebook %page %paste %pastebin %pdb %pdef %pdoc %pfile %pinfo %pinfo2 %popd %pprint %precision %profile %prun %psearch %psource %pushd %pwd %pycat %pylab %quickref %recall %rehashx %reload_ext %rep %rerun %reset %reset_selective %rm %rmdir %run %save %sc %set_env %store %sx %system %tb %time %timeit %unalias %unload_ext %who %who_ls %whos %xdel %xmode Available cell magics: %%! %%HTML %%SVG %%bash %%capture %%debug %%file %%html %%javascript %%js %%latex %%markdown %%perl %%prun %%pypy %%python %%python2 %%python3 %%ruby %%script %%sh %%svg %%sx %%system %%time %%timeit %%writefile Automagic is ON, % prefix IS NOT needed for line magics. マジックコマンドのヘルプ †コマンド名? を入力する事でコマンドの具体的なヘルプが表示される。 例) In [3]: time? Docstring: Time execution of a Python statement or expression. The CPU and wall clock times are printed, and the value of the expression (if any) is returned. Note that under Win32, system time is always reported as 0, since it can not be measured. . . . Examples -------- :: In [1]: %time 2**128 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s Wall time: 0.00 Out[1]: 340282366920938463463374607431768211456L . . . マジックコマンド一覧 †https://ipython.readthedocs.io/en/stable/interactive/magics.html Line magics †https://ipython.readthedocs.io/en/stable/interactive/magics.html#line-magics
Cell magics †https://ipython.readthedocs.io/en/stable/interactive/magics.html#cell-magics %lsmagic で確認した所、以下のものが利用できる模様。(殆どはPython以外の言語の実行用コマンド) Available cell magics %%! %%HTML %%SVG %%bash %%capture %%debug %%file %%html %%javascript %%js %%latex %%markdown %%perl %%prun %%pypy %%python %%python2 %%python3 %%ruby %%script %%sh %%svg %%sx %%system %%time %%timeit %%writefile 便利な使い方 †IPythonを起動したまま最新の自作モジュールを自動読込する †TODO:
|