IPythonを使おう2 Django編
September 25, 2007 at 10:00 AM | View CommentsIPythonをインストールしておけば
% python manage.py shell
でIPythonが立ち上がります。通常の対話モードを立ち上げるときは
% python manage.py shell -plain
ですが、やはりテストを書きやすくするためにも-clオプションのIPythonを立ち上げたいところです。
MiCHiLUさんのパッチをあてる
MiCHiLUさんがパッチを書いてくれたのでそれを使うといいかもしれません。これはいいので是非本家にも採用してほしいです。
manage.py shellでclassic promptのIPythonを使えるようにする - MiCHiLU.com
通常のIPython上からclモードのIPythonを立ち上げる
IPythonはPythonのプログラムなので、通常の対話モードからも立ち上げることができます。もちろんIPython上からさらに別のIPythonを立ち上げることも可能です。 ということで、通常のIPythonを立ち上げた後に-clオプションのIPythonを立ち上げてみましょう。
% python manage.py shell
Python 2.4.3 (#69, Mar 29 2006, 17:35:34) [MSC v.1310 32 bit (Intel)]
Type "copyright", "credits" or "license" for more information.
IPython 0.8.1 -- An enhanced Interactive Python.
? -> Introduction to IPython's features.
%magic -> Information about IPython's 'magic' % functions.
help -> Python's own help system.
object? -> Details about 'object'. ?object also works, ?? prints more.
In [1]: import IPython
In [2]: IPython.Shell.IPShell(user_ns=locals(), argv=['-cl']).mainloop(sys_exit=1)
Python 2.4.3 (#69, Mar 29 2006, 17:35:34) [MSC v.1310 32 bit (Intel)]
Type "copyright", "credits" or "license" for more information.
IPython 0.8.1 -- An enhanced Interactive Python.
? -> Introduction to IPython's features.
%magic -> Information about IPython's 'magic' % functions.
help -> Python's own help system.
object? -> Details about 'object'. ?object also works, ?? prints more.
>>> import myproject
>>> from myproject.bbs.models import Entry
>>> e = Entry.objects.all()
>>>
>>> e[1]
<Entry: 1 faf>
>>>
ちょっと面倒ですが、パッチをあてたくないときにはいいかもしれません。
設定ファイルを変更する
IPythonの設定ファイルipythonrcを変更するというのも方法の一つです。
- Windows: C:Documents and Settingsユーザー名_ipythonipythonrc.ini
- Unix系: ~/.ipython/ipythonrc
の中盤にある
# Use \# to represent the current prompt number, and quote them to protect
# spaces.
prompt_in1 'In [\#]: '
# \D is replaced by as many dots as there are digits in the
# current value of \#.
prompt_in2 '.\D. '
prompt_out 'Out [\#]: '
を
# Use \# to represent the current prompt number, and quote them to protect
# spaces.
prompt_in1 '>>> '
# \D is replaced by as many dots as there are digits in the
# current value of \#.
prompt_in2 '.\D. '
prompt_out ''
に変えてしまいましょう。変更するまえに元のファイルにもどせるように、元のipythonrcを名前を変えて保存しておくとようでしょう。
空白行が1つ余計につきますが、DocTestはできるはずです。