ひよっこ。

I want to…

IPythonの拡張機能を作成する

Posted by hikaruworld : 2012 1月 5

bazaarのプラグインを書こうとしてIPythonを起動したら、
何故かIPythonのエクステンションの書き方を調べてしまったのでログとして残しておきます。

IPythonのextensionの書き方は公式サイトIPython extensionsが参考になります。
サンプルとなるエクステンションは、IPython/extensions以下を見るとよいかと思います。

IPythonでextensionを読み込ませるためには2つの手順が必要になります。

1. IPythonのロード設定

IPython起動時に指定したエクステンションをロードするようにProfileに設定を行います。
前回Profileについては簡単に触れていますが、
エクステンションをロードするときは以下のように設定します。

# A list of dotted module names of IPython extensions to load.
c.InteractiveShellApp.extensions = [
    'hello'
]

2.エクステンションの配備

上記のようにロード設定をした後には、
当然エクステンションを配備してあげる必要があります。
IPythonのエクステンションは

os.path.join(self.ipython_dir, 'extensions')

が認識されます。

つまり、ipython起動時に--ipython-dirを指定していない場合、
デフォルトでは$HOME/.config/ipythonが適用されますので、$HOME/.config/ipython/extensionsを作成すればよい事になります。
今回の例ではhelloというエクステンションを読み込んでいるので、ディレクトリ構成は以下のようになります。

$HOME/.config/ipython
└── extensions
    └── hello.py

※関連部分のみ抜粋

3.エクステンションの書き方

IPython起動時の処理

IPythonが起動/終了した場合に何か処理を行いたい場合、以下の関数を定義する事で自動で読み込まれます。

# 起動時の処理
def load_ipython_extension(ipython):
	pass

# 終了時の処理
def unload_ipython_extension(ipython):
	pass

引数で渡されるipythonはTerminalInteractiveShellやInteractiveShellAppなどのインスタンスになります。

マジックコマンドの設定

IPythonはマジックコマンドが定義されていて、色々便利ですよね。
マジックコマンドって何って人は%入力して<TAB>で一覧が確認出来ます。使い方は%XXX?で確認可能です。

In [3]: %
%alias                   %logstart                %pylab
%autocall                %logstate                %quickref
# いっぱいあるので以下省略。

In [3]: %pwd?
Type:       Magic function
Base Class: <type 'instancemethod'>
String Form:<bound method TerminalInteractiveShell.magic_pwd of <IPython.frontend.terminal.interactiveshell.TerminalInteractiveShell object at 0x1017a5ed0>>
Namespace:  IPython internal
File:       ~/.virtualenvs/bazaar/lib/python2.7/site-packages/IPython/core/magic.py
Definition: %pwd(self, parameter_s='')
Docstring:
Return the current working directory path.

Examples
--------
::

  In [9]: pwd
  Out[9]: '/home/tsuser/sprint/ipython'

で、本題。
エクステンションでも独自のマジックコマンドを定義する事が出来ます。
マジックコマンドを定義したい場合、

define_magic(magicname, func)

で登録する事が可能です。

たとえばsayというマジックコマンドを登録したい場合、この様に読み込ませます。

def say_impl(self, parameter_s=''):
	# sayコマンドの実体。引数はparameter_sに設定される。
    print 'Hello %s' % parameter_s

def load_ipython_extension(ipython):
	# ipythonが起動したタイミングで'say'というコマンドを定義する
	ipython.define_magic('say', say_impl)

これをipythonで起動して、実行するとこんな感じで表示されます。

In [1]: %say taro
Hello taro

マクロなどを定義したい場合はdefine_macro(name, themacro)という関数が準備されています。
自分は必要なかったので確認していませんが…。

以上です。
簡単ですね。日本語情報がないことをのぞけば….

Posted in program | タグ: , , | Leave a Comment »

ipython_config.pyの設定メモ

Posted by hikaruworld : 2012 1月 5

メモです。
大したカスタマイズしていませんが。

# -*- coding: utf-8 -*-
# Configuration file for ipython.

c = get_config()

# lines of code to run at IPython startup.
# 頻繁に利用するライブラリをimport
c.InteractiveShellApp.exec_lines = [
    'import os',
    'import sys',
    'import numpy'
]

#------------------------------------------------------------------------------
# TerminalInteractiveShell configuration
#------------------------------------------------------------------------------
# Set the editor used by IPython (default to $EDITOR/vi/notepad).
# c.TerminalInteractiveShell.editor = 'vi'
# デフォルトエディタをvimに
c.TerminalInteractiveShell.editor = '/usr/bin/vim'

# Enable auto setting the terminal title.
# Terminalにタイトル表示
c.TerminalInteractiveShell.term_title = True

# Automatically call the pdb debugger after every exception.
# エラー発生時にpdbを起動
c.TerminalInteractiveShell.pdb = True

#------------------------------------------------------------------------------
# PrefilterManager configuration
#------------------------------------------------------------------------------
# 複数行のスクリプトを有効化...何だけどなんかうまく行かん...
c.PrefilterManager.multi_line_specials = True

#------------------------------------------------------------------------------
# AliasManager configuration
#------------------------------------------------------------------------------
# lsやgrepをカラー設定
c.AliasManager.user_aliases = [
    ('ls', 'ls -G'),
    ('grep', 'grep --color-auto')
]

Posted in program | タグ: , , | Leave a Comment »

ipythonのvirtualenvsごとのprofile設定

Posted by hikaruworld : 2012 1月 3

virtualenvでipythonを利用する場合は、
ipython と virtualenv を同時に使う方法が参考になります。
自分の場合は、上記リンクの第一の方法で設定しています。

で、上記サイトでも言及されている訳ですが。
ipythonは起動時に設定ファイル(いわゆるprofile)を読み込みます。

ipython0.12の場合、読み込み順はConfiguration file locationに従って行われます。

If the ipython_dir command line flag is given, its value is used.
If not, the value returned by IPython.utils.path.get_ipython_dir() is used. This function will first look at the IPYTHON_DIR environment variable and then default to a platform-specific default.

なおipython-dirは引数に指定しない限りデフォルトでは、$HOME/.config/ipythonが参照されます。

ipythonで任意のプロファイルを利用したい場合は、

ipython --profile XXX

のXXXで指定する事が可能です。

ipython-dirをVIRTUAL_ENV以下を見るようにカスタマイズしてもよいのですが、
自分の場合は&HOME以下はbazaarでバージョン管理しているので、virtualenvのpostactivateをこんな感じで編集しておきます。

#!/bin/bash
# This hook is run after every virtualenv is activated.
# cat ~/.virtualenvs/postactivate 

which ipython >/dev/null 2>&1
if [ $? -eq 0 ]; then
    # 面倒なのでpythonでsplit
    current_ipython=`python -c "print '$VIRTUAL_ENV'.split('/')[-1]"`
    alias ipython="ipython --profile $current_ipython"
fi

これでworkonで任意のvirtualenvsを有効にした場合にipythonのコマンドが設定されていれば、
–profileにvirtualenvsの環境名がエイリアスに設定されます。

以上です。

Posted in program | タグ: , , | Leave a Comment »

ipythonの>>>

Posted by hikaruworld : 2012 1月 1

久々にipythonをインストールしたら0.12になっていて

ipython -cl

とかやったら、怒られた。

んー、と思ってドキュメント読んでたら、%doctest_modeってのがあるんですな。

ipython
In [1]: %doctest_mode
Exception reporting mode: Plain
Doctest mode is: ON
>>> 

となります。

なるへそ。

以上です。

Posted in program | タグ: , | Leave a Comment »

npm completionがあると聞いて

Posted by hikaruworld : 2011 12月 27

npmについてまとめてみるというブログのエントリーをを見ていて、
npm completionという便利なコマンドがあると知ったので設定してみた。

自分の環境ではnave経由なので、
こんな感じに~/.bashrcに書いてみました。気持ちはpythonのvirtualenv風味。
起動時に$NAVEが設定されていた場合対象バージョンのinstalledディレクトリのpostactivateが存在した場合に読み込みます。

# read nave postactivate script
if [ -n $NAVE ]; then
    echo $NAVE_ROOT/$NAVEVERSION/postactivate
    if [ -f $NAVE_ROOT/$NAVEVERSION/postactivate ]; then
        . $NAVE_ROOT/$NAVEVERSION/postactivate
    fi
fi

postactivateはこんな感じ。
node有効にしたときの処理を色々書けていいかも。

#!/bin/bash

# read npm completion
. <(npm completion)

快適になった♪

naveのやつはないのかな。
あと、postactivateするとpostinstalledとか作って必要なnpmパッケージをインストールしたくなりますね。
nave.sh的のソース見てたんですが、サブシェルの起動まわりをちゃんと理解してなかったのでまた気が向いたときにでも。

以上です。

Posted in program | タグ: , , , | Leave a Comment »

bazaarのプラグインを作る(2)

Posted by hikaruworld : 2011 12月 26

前回の続きです。

hello_world.pyだと色々とやりづらいので、
plugins以下にhello_worldという形でモジュール化しておきます。

$ ls ~/.bazaar/plugins
hello_world.py
$ cd ~/.bazaar/plugins
$ mkdir hello_world
$ mv hello_world.py hello_world/__init__.py

2-4.helloコマンドに引数を与える

bzr stなどにLOCATIONを指定できるように、
自分で作ったプラグインにもコマンドライン引数を設定する事が可能です。
というわけで、helloコマンドにユーザ名を指定して
hello usernameという形に出力するようにプラグインを拡張してみます。

引数を指定したい場合はtakes_argsというプロパティをクラス内に定義して、
取得したい要素を配列で定義します。そしてそこで定義した値をrun関数の引数で受け取る事が可能です。

# __init__.py
# クラス部分だけ抜粋
class cmd_hello(Command):
    takes_args = ["username?"]
    
    def run(self, username=None):
        print "Hello %s" % username

末尾に?をつけると任意要素に、つけない場合は必須要素になります。
他に末尾に指定できる項目は、?, *, +, $あたりがあるようですが詳しくは追っていません。
bzlib.command.pyの_match_argformを見る感じだと、+だと最低1つは存在し、かつ以降の値をまとめるとかできる感じですね。
詳しくはTODOにして後で検証してみます。
必須項目にしていた場合に設定せずにコマンド起動すると、ちゃんとbzrが叱ってくれます。

bzr: ERROR: command ‘hello’ requires argument USERNAME

2-5.オプションの設定

bzrのコマンドには引数ではなく、各種Optionの設定を行う事が出来ます。
どのコマンドでも利用できる-vや-hと言ったオプションと、例えばremoveコマンド独自の–forceといったような2つに大別されます。
これらのオプションはtakes_optionsをtakes_argsと同じように設定する事で利用が可能になります。

但し、前者(-vや-h)はそのオプション名を文字列で指定すればよいのですが、
独自オプションを作る場合は、個別にOptionクラスRegistryOptionを定義する必要があります。

TODO RegistryOption周りの説明

ここでは、verboseというどのコマンドでも利用可能オプションと、
このコマンド特有のhideというオプションの設定を行います。

# __init__.py
from bzrlib.commands import Command, register_command
from bzrlib.option import Option

class cmd_hello(Command):

    takes_args = ["username"]
    takes_options = [Option("hide", "username print asterisk"), "verbose"]

    def run(self, username, hide=False, verbose=False):
        print "hello %s" % ("*" * len(username) if hide else username)

これで–hideというオプションを設定した場合は出力が***になります

$ bzr hello hogepiyo
hello hogepiyo
$ bzr hello hogepiyo --hide
hello ********

2-6. プラグインのテスト

プラグインのテストはselftestというコマンドで実行する事が可能です。
但し、testtoolsに依存していますので、
存在していないと、こんな感じで怒られます。

bzr: ERROR: No module named testtools
You may need to install this Python library separately.

pipなりeasy_installなりでインストールしておきます。

selftestの仕組みはプラグインにtest_suite関数が定義されていれば自動でテストを実行してくれます。
なので、こんな感じで関数を定義しておきます。

# from hello_world.py
# 対象箇所のみ抜粋
def test_suite():
	pass
    from bzrlib.tests.TestUtil import TestLoader
    import tests.test_hello
    from unittest import TestSuite

    result = TestSuite()

    result.addTest(TestLoader().loadTestsFromModule(tests.test_hello))

これだけだと、テストの実体がないのでテストを読み込んで実行するようにします。
以下のように構成されているとします。

$ cd ~/.bazaar/plugins/hello_world
$ mkdir tests
$ touch tests/__init__.py tests/test_hello.py
$ tree .
.
├── __init__.py
└── tests
    ├── __init__.py
    └── test_hello.py

test_hello.pyを以下のように記述します。

# test_hello.py
from bzrlib.tests import TestCase

class TestHelloWorld(TestCase):
    def test_hello(self):
    	# とりあえず失敗させる
        self.assertEqual(1, 0)

__init__.py側でも該当のモジュール読み込むようにします。
# この辺りはbzrtools辺りのtest_suiteの実装を参考にしています。

# __init__.py
def test_suite():
    from bzrlib.tests.TestUtil import TestLoader
    from unittest import TestSuite

    import tests.test_hello

    result = TestSuite()
    result.addTest(TestLoader().loadTestsFromModule(tests.test_hello))

    return result

pythonのunittest.TestSuiteにaddTestしているだけですね。
TestUtil.TestLoaderはunittest.TestLoaderを拡張しているので基本は同じはず…。

これでselftestを実行してみます。

$ bzr selftest hello_world
FAIL: bzrlib.plugins.hello_world.tests.test_hello.TestBase.test_hellost_hello                                                                                                       
    Empty attachments:
  log

Traceback (most recent call last):
  File "~/.bazaar/plugins/hello_world/tests/test_hello.py", line 6, in test_hello
    self.assertEqual(1, 0)
AssertionError: not equal:
a = 1
b = 0

======================================================================                                                                                                              
FAIL: bzrlib.plugins.hello_world.tests.test_hello.TestBase.test_hello
----------------------------------------------------------------------
_StringException: Empty attachments:
  log

Traceback (most recent call last):
  File "~/.bazaar/plugins/hello_world/tests/test_hello.py", line 6, in test_hello
    self.assertEqual(1, 0)
AssertionError: not equal:
a = 1
b = 0

----------------------------------------------------------------------
Ran 1 test in 0.154s

FAILED (failures=1)

テストが成功するように修正して実行してみます。

# test_hello.py
#self.assertEqual(1, 0)
self.assertEqual(0, 0)
$ bzr selftest hello_world
bzr selftest: /usr/local/Cellar/bazaar/2.4.2/libexec/bzr
   /usr/local/Cellar/bazaar/2.4.2/libexec/bzrlib
   bzr-2.4.2 python-2.6.1 Darwin-10.8.0-i386-64bit

----------------------------------------------------------------------                                                                                                              
Ran 1 test in 0.154s

ok

何も出ませんが、成功しています。
どのテストが実行されたかは、-vをつけると分かります。

running 1 tests...
bzr selftest: /usr/local/Cellar/bazaar/2.4.2/libexec/bzr
   /usr/local/Cellar/bazaar/2.4.2/libexec/bzrlib
   bzr-2.4.2 python-2.6.1 Darwin-10.8.0-i386-64bit

bzrlib.plugins.hello_world.tests.test_hello.TestBase.test_hello                                                                                                       OK        2ms
----------------------------------------------------------------------
Ran 1 test in 0.156s

OK

3. 補足

3-1. プラグインロード時の問題

bzrは起動時にこれらのコマンドを読み込むため、__init__.pyにいわゆる『重い』処理を書いておくと
最初のbzrの起動がとても重くなってしまうそうです(2回目以降はキャッシュされますが…)。
そのための回避策としてチュートリアルには2つ方法が提示されています。

初期の読み込みを最小限にする

bzrの起動時に読み込まれるのが最初の起動ファイル?だけのようです。
ここで記述されている必要があるのがCommandクラスの定義とコマンドの登録のみなので、
それ以外を全て別のファイルに追い出してしまうという方法のようです。

遅延読み込みの実装を行う

lazy_import.lazy_importを利用する方法です。
この方法はコマンドが起動されるまで依存するモジュールの読み込みを遅延できるようです。
ドキュメントやビルドインコマンドでは以下のように読み込まれる事が確認できます。

from bzrlib.lazy_import import lazy_import
lazy_import(globals(), """
from bzrlib import (
    branch as _mod_branch,
    option,
    workingtree,
    )
""")

ただ、一部のプラグインは、commands.plugin_cmds.register_lazyを利用して読み込んでいるものもありました。
この辺はまだ調べていないのでTODOで。

3-2. 参考にしたサイト

Developing a plugin

以上です。大体こんな感じでプラグインが作成できます。
簡単ですね。あとはIntegrating with Bazaarあたりを参考にいじっていけると思います。

以上です。

Posted in program | タグ: , , , | Leave a Comment »

bazaarのプラグインを作る(1)

Posted by hikaruworld : 2011 12月 25

bazaarのよいところの一つはpythonで簡単にプラグインを書ける事です。
知ってはいたんですが、作った事なかったのでHelloWorldしてみました。

1.基本的なルール

1-1.プラグインのインストール場所

bzrのプラグインは$HOME/.bazaar/plugins以下に配備すると自動でモジュールをロードしてくれます。
実際にどんなプラグインをインストールしているかは、

bzr plugins

とコマンドを打つことで確認できます。

1-2.参考にする実装

bzrは起動時にビルドインコマンドを読み込みます(bzr infoとかです)。
そのコマンド自体はbzrlib/builtins.pyで定義されているのでプラグインを作成するときには参考にしましょう。

2.helloプラグインの実装

2-1.プラグインの作成

プラグイン自体をbzrに認識させる方法は3つありますが、1つはビルドインコマンドなどが使う方法なので、通常は$HOME/.bazaar/pluginsに配備する方法で実現します。

1つ目のもっとも単純な方法は、plugins以下に任意のpythonファイルを置くことです。
例えば今回の場合であれば、以下のように配置し、bzr pluginsで確認してみると

$ cd ~/.bazaar/plugins
$ touch hello.py

hello
(no description)

という風に認識してくれます。

もう一つは、任意のディレクトリを作成して、__init__.pyを置くことです。
こちらの方が主流ですね(って、スクリプトだけのプラグインってみたことないですが)。

$ cd ~/.bazaar/plugins
$ mkdir hello
$ touch hello/__init__.py

但し、この状態ではプラグインのコマンドなどは定義されていないので、

$ bzr hello

とかやっても、こんな感じで怒られます。

invalid syntax (, line 1)
Unable to load ‘.hello’ in ‘~/.bazaar/plugins’ as a plugin because the file path isn’t a valid module name; try renaming it to ‘_hello’.
bzr: ERROR: unknown command “hello”

2-2.helloコマンドの作成

今回の目的はbzr helloとコマンドを打つと、
標準出力にHello Worldと出力するプラグインなので、
helloコマンドを新しく定義します。

bzrのコマンド(infoやbranchのようなもの)は
bzrlib.commands.Commandクラスを継承して実装する必要があります。
今回はhelloコマンドなので定義だけならこんな感じになります。
Defining a new commandに書いてあるままです。

from bzrlib.commands import Command, register_command

class cmd_hello(Command):
     pass

register_command(cmd_hello)

注意点は、クラス名のprefixとしてcmd_が付与されること、
アンダースコア(_)でつないだ場合にはコマンド時にはハイフンつなぎになる事くらいです。
# cmd_hoge_piyoはhoge-piyoというコマンドになります。

但し、このままではhelloコマンドを実行してもエラーになってしまいます。

bzr: ERROR: exceptions.NotImplementedError: no implementation of command ‘hello’

新しいコマンドを作成した場合は、run関数を実装してあげる必要があります。

from bzrlib.commands import Command, register_command

class cmd_hello(Command):
    def run(self):
        print “Hello World”

register_command(cmd_hello)

これでbzr helloと実行するとHelloWorldと表示してくれるプラグインが出来上がります。

$ bzr hello
Hello World

2-3.helloコマンドのプラグイン情報の記述

bzr pluginsやbzr help XXX(プラグインのコマンドとか)をしてみるとわかりますが、
プラグインにはさまざまなメタ情報が設定されています。

launchpad 2.4.1
Launchpad.net integration plugin for Bazaar.

この設定を行います。

バージョンの記述

バージョンの記述はプラグインを定義しているファイルにversion_infoという値をタプルで定義します。

version_info  = (0, 1, 2, "dev", 0)

上記のように定義してbzr pluginsで確認してみると0.1.2devという風に表示されます。
5番目の要素に0を指定した場合は空気読んで表示されないみたいですね。

プラグインの概要を記述する

bzr pluginsを実行したときにそのプラグインに関する概要が表示されると思います。
この設定もプラグインファイルの冒頭にコメントを記述することで表記可能です。

""" This plugin description"""

複数行書いても概要で表示されるのは1行目だけです。

これでHelloWorldプラグインは完成です。
こんな感じになります。

"""This plugin is print HelloWorld
"""
from bzrlib.commands import Command, register_command

class cmd_hello(Command):
    def run(self):
        print “Hello World”

version_info = (0, 1, 2, "dev", 0)

register_command(cmd_hello)

長くなってきたのでに続きます…。

Posted in program | タグ: , , | Leave a Comment »

MacOSXでbzrのbash-completionを利用する

Posted by hikaruworld : 2011 12月 23

MacのTerminalをbash環境で利用している場合に、bzrのbash-completionが欲しくなります。
bash-completionとは何かはこの辺りが参考になるかと。

bash-completionをhomebrewで

自分はbash-completionをhomebrewでインストールしたので、こんな感じ。

$ brew install bash-completion

で言われるがままに、~/.bashrcにこれを追記するとコード補完が有効になります。

if [ -f `brew --prefix`/etc/bash_completion ]; then
    . `brew --prefix`/etc/bash_completion
fi

bazaarのbash-completionを有効に

残念ながらそのままではgitやsvnは補完してくれますが、
bazaarは補完してくれません(Macの場合)。
bazaarを補完してもらうためには、bzr bash-completionをインストールします。

通常のプラグインと同じように、~/.bazaar/plugins以下にbranchを取得します。

bzr branch lp:bzr-bash-completion bash-completion

有効化するためにREADMEにあるように、~/.bashrcに以下を追記。

# read bash_completion for bzr
if [ -f $HOME/.bazaar/plugins/bash_completion/lazy.sh ]; then
    . $HOME/.bazaar/plugins/bash_completion/lazy.sh
fi

eval “`bzr bash-completion`”でもよいようですが、遅延初期化の方が無駄な待ちがないので。

これで、bzr sとか入れてTABを押すとこんな感じで補完されます。

bash-3.2$ bzr s
s-c              server           shelve           st
selftest         shelf1           shelve1          stat
send             shell            sign-my-commits  status
serve            shell-complete   split            switch

これで、変なタイポを減らせます。
# なぜかuncommitのコマンドばかり間違える私もこれでさよなら。

Posted in program | タグ: , , | Leave a Comment »

plaxoの退会方法

Posted by hikaruworld : 2011 12月 16

plaxoというアドレス同期サービスがあります。
なんで登録したか記憶にないんですが、いらないので退会しようとしたところ、
少し迷ったのでメモ。

How Do I Delete My Plaxo Account?
Click hereからアカウントを削除できます。

分かりづらい…
HelpCenterから検索して見つけましたよ…

Posted in program | Leave a Comment »

iterm2を導入したらcdtoが動かなくなった

Posted by hikaruworld : 2011 12月 7

というわけで、

cdtoのバージョンを2.5にversion up。

それでも動かなくてissue見てたら、Does not work with iTerm2 を発見。
これの下の方にあるcomment 7に添付されているiTerm2.zipをダウンロードして、
iterm2.bundleをpluginsに突っ込んだら無事動きましたとさ。

感謝。

Posted in program | タグ: , | Leave a Comment »