8.23.2015

Python: Getting Started with NetworkX Graph Generator

NetworkX のグラフジェネレーターで遊ぶ

 

out-of-the-box な機能が非常に豊富で感動した。

せっかくなので、IPython Notebook でビジュアライズしてみる。

 

準備

全て必須という訳ではないが、一応インストールしておく。

pip install ipython jinja2 tornado pyzmq numpy scipy pylab matplotlib
pip install networkx

作業ディレクトリに移動して、IPython Notebook 起動。

ipython notebook

ブラウザ上で操作し、 New Notebook を作成。

Home

 

実行例

コードの補完もできる。

NetworkXGeneratorExample

ひととおりグラフを作ったあと保存し、そのファイルを Gist にアップロードしてみた。
すると以下のとおり、簡単にコードとイメージを共有することができる。

このカジュアルさに、また感動。Python 素晴らしい。

8.22.2015

Writing Set of Sets in Python

Python: set を要素に持つ set を書く

 

普通に書くと、unhashable type (ハッシュ化できないデータ型) だと怒られる。

>>> {set(), {1}, {1, 2}}
Traceback (most recent call last):
  File "", line 1, in
TypeError: unhashable type: 'set'

内側の set(集合) に frozenset を使えばよい。

>>> {frozenset(), frozenset({1}), frozenset({1, 2})}
{frozenset({1, 2}), frozenset(), frozenset({1})}

fronzenset はイミュータブルなので、後から変更を加えることはできない。

>>> s = frozenset()
>>> s.add(1)
Traceback (most recent call last):
  File "", line 1, in
AttributeError: 'frozenset' object has no attribute 'add'

 

 

References

8.10.2015

Shell: File Override in Safety

Shell: 安全にファイルの内容を入力と同時に書き換える

 

同僚に教えてもらった。

一見、本当に安全なのかわかりづらいが、結果、大丈夫そう。

(rm -f -- "${FILEPATH}" && COMMAND > "${FILEPATH}") < "${FILEPATH}"

これで、一時ファイルなしでファイルを入力しながら書き出すことが可能になる。(inode は変わる)

8.09.2015

CircleCI Failed After Restoring Cache Files

CircleCI がキャッシュ復元処理の後で失敗した

 

事象

CircleCI を回していたら、Restore cache の後でエラー終了となってしまった。

画面には以下のようなメッセージが出現している。

Looks like we had a bug in our infrastructure, or that of our providers (generally GitHub or AWS) We should have automatically retried this build. We've been alerted of the issue and are almost certainly looking into it, please contact us if you're interested in the cause of the problem.

 

原因

チャットでサポートに連絡したら、なんと 30分で回答が返ってきた。

We currently can’t cache files outside the home directory. E.g. they will be cached, but the build will err out on restoring the cache. I see that you are caching a few files in /usr/local, would that be an option for you to store them in the home directory, cache them and then symlink them into the right place?

実は今回、circle.yml で /usr/local 配下のあるディレクトリをキャッシュ対象として指定していた。
現状ではホームディレクトリ以外の場所はキャッシュできないようだ。

キャッシュ保存時は成功したように見えるが、復元の時点でエラーが発生するとのこと。

対応策としては、ホームディレクトリ配下にキャッシュ用のディレクトリを作って、そこにシンボリックリンクを張るとよいと教えてもらった。
後日対応し、問題なく稼動している。

dependencies:
  pre:
    - sudo mkdir -p yyyyy /usr/local/lib/xxxxx
    - sudo ln -s $PWD/yyyyy /usr/local/lib/xxxxx/yyyyy

 

StackShare でもカスタマーサポートが良いと好評だが、それを実感することができてよかった。

 

CircleCI については、こんなものも書きました。

7.27.2015

Python unittest Cheat Sheet

Python: unittest のチートシート

よく使うイディオムをまとめておく。

  • Python 2.6 に対応するには、unittest2 を使うのが一番簡単

7.20.2015

Dragon Quest 5 (PS2) Time Attack - 2:53

PS2版ドラゴンクエスト5のタイムアタック

 

遠い昔、プレイステーション2 版の ドラゴンクエスト5 で遊んでいた頃のノートがつい先日出てきた。

折角なので、Github にラップタイムをまとめておく。

7.12.2015

Safe and Smart Memoization in C++ (or Python)

エレガントなメモ化のやり方

 

同僚から教えてもらったテクニックを忘れそうになったのでメモ。

#include <cstdio>

int memo[100][100];

int main() {
  memset(memo, -1, sizeof memo);
  // call f with parameters
  f(???, ???)
}

int f(int x, int y) {
  int& res = memo[x][y];
  if (res < 0) {
    // do the work when the value has not been calculated
    res = ???
  }
  return res;
}

適当なn次配列をメモ化の領域として使い、1のビット(-1)で初期化するところまでは一般的なやり方。

ポイントは、実際にメモ化したデータを使う部分。
はじめに配列の要素の参照を取り、初期状態でない場合にのみその値を更新する。
いずれのケースでも最後にその参照を返す。

これで処理の流れがシンプルになるし、メモの更新し忘れを防ぐこともできる。

同様に、Python で値が None で初期化されているならこんな感じ。

def f():
    if cache is None:
        # do the work
        cache = ???
    return cache