再考 IPython 1(そもそもIPythonって)

Facebook にシェア
Pocket
LINEで送る
このエントリーを Google ブックマーク に追加

IPythonは普段から使っているけれど、

Pythonエンジニア養成読本[いまどきの開発ノウハウ満載!] (Software Design plus)

を読んで再考。

 

そもそもIPythonって

pythonはインタープリターとして実行できる。

[shell] $ python
Python 3.4.2 (default, Jan 7 2015, 11:54:58)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.56)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> ans = 0
>>> for i in range(1, 100):
… ans += i

>>> ans
4950
>>>
[/shell]

で、より使いやすく拡張されたのがIPython。

 

インストール

pip でインストール。

[shell] $ pip install ipython
[/shell]

 

便利な使い方

  1. 補完機能

Tabを押すことで、ライブラリ名、変数名などの補完できます。

[shell] ipython
Python 3.4.2 (default, Jan 7 2015, 11:54:58)
Type "copyright", "credits" or "license" for more information.

IPython 3.1.0 — An enhanced Interactive Python.
? -> Introduction and overview of IPython’s features.
%quickref -> Quick reference.
help -> Python’s own help system.
object? -> Details about ‘object’, use ‘object??’ for extra details.

In [1]: import a <-Tabを押すと候補が出てくる
abc aifc argparse ast asyncio atexit autoreload
activate_this antigravity array asynchat asyncore audioop
[/shell]

 

2.「 ?」「 ??」 でクラス、関数、変数の詳細を表示

クラス、関数、変数のあとに「?」「??」を入力すると詳細な情報が表示される。

「?」の場合は概要、「??」の場合はソースが表示される。

[shell] In [1]: import random

In [2]: random.Random.gauss?
Signature: random.Random.gauss(self, mu, sigma)
Docstring:
Gaussian distribution.

mu is the mean, and sigma is the standard deviation. This is
slightly faster than the normalvariate() function.

Not thread-safe without a lock around calls.
File: ~/Documents/market/lib/python3.4/random.py
Type: function

In [3]: random.Random.gauss??
Signature: random.Random.gauss(self, mu, sigma)
Source:
def gauss(self, mu, sigma):
"""Gaussian distribution.

mu is the mean, and sigma is the standard deviation. This is
slightly faster than the normalvariate() function.

Not thread-safe without a lock around calls.

"""

# When x and y are two variables from [0, 1), uniformly
# distributed, then
#
# cos(2*pi*x)*sqrt(-2*log(1-y))
# sin(2*pi*x)*sqrt(-2*log(1-y))
#
# are two *independent* variables with normal distribution
# (mu = 0, sigma = 1).
# (Lambert Meertens)
# (corrected version; bug discovered by Mike Miller, fixed by LM)

# Multithreading note: When two threads call this function
# simultaneously, it is possible that they will receive the
# same return value. The window is very small though. To
# avoid this, you have to use a lock around all calls. (I
# didn’t want to slow this down in the serial case by using a
# lock here.)

random = self.random
z = self.gauss_next
self.gauss_next = None
if z is None:
[/shell]

 

  1. 一部コマンドが使える

lsコマンドやclearコマンドなど、一部のコマンドが扱える。

<

pre class=””>
[shell] In [10]: ls
Applications/ Documents/ Library/ Pictures/ futago.txt project_django/
Box Sync/ Downloads/ Movies/ Public/ libcxx/ python3/
Desktop/ Dropbox/ Music/ aws* log test.rb

In [11]: current_dir = %pwd
In [12]: current_dir
Out[12]: ‘/Users/umentu’
[/shell]

 
再考 IPython 2(notebookの活用)
再考 IPython 3(matplotlibを使ったグラフの描画)

Related posts

コメントを残す