vimでコマンドの出力をキャプチャする

特定のファイルにコマンドの出力を書き出したいときはこれ
!をつけることで新規のファイルでも書き込める

:redir! > file
:echo 'aaa'
:redir end

これでaaaという文字列がfileに書かれる
ちなみにredirはrediと省略して書ける
また、rediはredirectの略
ファイルに追記したい場合は>>を使えばよい

:redi >> %
:echo 'bbb'
:redi end

これでbbbという文字列が現在編集中のファイルの末尾に追記される(bufferに反映させるには:e!する必要あり)

出力をレジスタに入れることもできる

:redi @a
:echo 'testtest'
:redi end

レジスタに入れてしまえば後はbufferで好きなように使える

redi endが実行されるまでの出力すべてがキャプチャされるため、その間にエラーなどが発生すればそれもすべてキャプチャされることに注意

es6でreactを使うときのメモ

reactを触ってみた
せっかくなので新しい形で使いたくes6の形にしたら、reactの元の書き方では動かないところがあったのでメモしておく

1. getInitialState

# es6以前
var Component = React.createClass({
  getInitialState: function() {
    return {data: []};
  },

es6ではgetInitialStateというメソッドを定義しても動かない。以下のように定義する。

# es6
class Component extends React.Component {  
    constructor() {                                            
        super();                                               
        this.state = {                                         
            data: [],
        };                                                     
    }                                                          

2. props

render: function() {
    <Component id=1 />
}

このように呼び出されるComponentがあった場合、

# es6以前
var Component = React.createClass({
  getInitialState: function() {
    return {data: Store.getData(this.props.id)};
  },
# es6
class Component extends React.Component {  
    constructor(props) {                                            
        super(props);                                               
        this.state = {                                         
            data: Store.getData(props.id)},
        };                                                     
    }                                                          

es6ではconstructor内でpropsにアクセスするために引数として受け取る必要がある

3. callbackのthis

callbackとして登録したメソッド内で呼ばれるthisがundefinedになる
例えば、es6で以下のように書いてcallbackとして登録すると実行時にthisがundefinedとなる

render() {
    return <button onClick={this.callback} />
}

callback(event) {
    console.log(this.state.data);
}

なので以下のようにbindを利用する

render() {
    return <button onClick={this.callback.bind(this)} />
}

参考

入門 React ―コンポーネントベースのWebフロントエンド開発

入門 React ―コンポーネントベースのWebフロントエンド開発

macでmatplotlibによる画像出力

mac上でskimageを入れた際にエラーになったのでメモ

pyenvにてinstallしたpython3.5にて、pipで入れたskimageによって画像を出力しようとした際エラーになった

発生したエラーはこちら

>from skimage import io
>image = io.imread('example.png')
>io.imshow(image)
RuntimeError: No suitable plugin registered for imshow

描画に必要なライブラリを入れる必要があるのかと考え、matplotlibでグラフを表示できるか試してみるとこれもエラー

>>> import matplotlib.pyplot as plt
RuntimeError: Python is not installed as a framework. The Mac OS X backend will not be able to function correctly if Python is not installed as a framework. See the Python documentation for more information on installing Python as a framework on Mac OS X. Please eith
er reinstall Python as a framework, or try one of the other backends. If you are Working with Matplotlib in a virtual enviroment see 'Working with Matplotlib in Virtual environments' in the Matplotlib FAQ

ググッてみるとこんなのを見つけた http://qiita.com/katryo/items/918667f28301fdec89ba

今回のケースと同じようなのでこれに従ってbackendを別のものを使うよう変更してみる

$ cat 'backend : TkAgg' >> ~/.matplotlib/matplotlibrc
$ python -c 'import matplotlib.pyplot as pyplot; pyplot.pie([1,2,3]); pyplot.show()' 

これで無事グラフが表示された

もともとやりたかったexample.pngの出力もうまくいくようになった