Pythonでクラス内の関数を並列演算させる - 技術の切れはし. Pythonでクラス内の関数を並列演算させる オブジェクト指向プログラミング+multiprocessing.Poolでの並列処理でハマりました。
クラスの中でPoolライクな文法で並列演算を行なうことのできるMyPoolを自作したのでその紹介です。 Pythonでの並列処理について Pythonにはver2.6以降からmultiprocessingという便利なものがあります。 これのPoolという機能を使うと、 test.py from multiprocessing import Pool def fuga(x): return x*x def hoge(): p = Pool(8) print p.map(fuga, range(10)) if __name__ == "__main__": hoge() 出力結果 $ python test.py [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] このようにたったこれだけで並列処理ができてしまいます。 とっても便利。 Fugaという関数に、range(10)つまり0~9までのそれぞれの値を入れたときの結果を8スレッドの並列演算をして求めているわけです。 Python でUTF-8, shift_jis, euc_jpなど日本語を使う方法. [Home] [Setting up Mac OS X] [Python] : [ファイルを読む] 日本語を使う Python 2.4以降では、標準で日本語を扱うことができます。
PythonのソースコードをUTF-8で書くには 日本語を扱うPythonのスクリプトの中では、UTF-8の文字コードを使うのが 楽です。 Mac OS Xのターミナルで日本語を扱う場合は、 ここの「4. Terminalの設定を変える」 の 指示に従ってください。 以下、ソースコードの簡単な例を示します。 #! 2行目の "# -*- coding: utf-8 -*-" は、このソースがUTF-8で書かれていること を示します。 UTF-8の文字をプリントするには 日本語の入った文字列を標準出力にprintするには、次のようにするのが楽です #! Sys.stdout = codecs.getwriter('utf_8')(sys.stdout) を 一回実行すると、 それ以降、標準出力はUTF-8で書き出されます。
標準入出力(sys.stdin, sys.stdout)で用いる文字コードを指定するには 例えば、文字コード euc_jp で書かれたファイルを標準入力から読み込み、 標準出力に文字コード shift_jis で書き出すスクリプトは次のように書けます。 #! 文字コードがUTF-8の場合は、'euc_jp', 'shift_jis'の代わりに'utf_8' を指定します。 文字コードを指定してファイルを開き、ファイルの読み書きをするには. Python. Machine learning in Python — scikit-learn 0.13-git documentation. Find Key for value in python when key associated with multiple values. DataStore « python練習帳. DataStoreとは 本記事内のDataStoreは、GoogleAppEngineの データストア のことを指す。
GoogleAppEngineは、ファイルの書き込みが一切できないため、DataStoreを使う以外にデータを永続化する方法はない。 短期であれば memcached に保存することもできるが、memcachedに保存されたデータは保存期間が保証されないため、一時データをキャッシュして高速化(or 負荷軽減)以外には実質つかえない。 DataStoreの特徴 DataStoreの特徴は、スケーラブルであること。 苦手な処理 件数のカウント ⇒件数のカウントは、データ全体を取ってくる処理に近いだけの処理時間が掛かります。 出来ない操作 Joinできない ReferencePropertyを使うとデータ間の関連を表現できます。 Db.Modelとdb.Expandoとpolymodel.PolyModel DataStoreには、以下の3つのクラスを継承したクラスのインスタンスを保存できます。 Db.Model あらかじめ定義されたプロパティ(固定プロパティ)しか保存できない。 使用例 db.Modelを使ったBBSプログラムの例を示す。 5行目 dbモジュールをインポートした。 ソースコード 実行画面 Relation 冒頭で述べたように、DataStoreではJOIN演算を使えない。 1:nのリレーション 1対nのリレーションを使った例を示す。
10行目~13行目 コメントの一覧表示と、コメント投稿用のHTMLフォームの表示 18行目~21行目 コメントをDataStoreに保存。 1:nのリレーション n:nのリレーション n:nのリレーションはデータモデルの部分の例だけ紹介する。 1:1のリレーション.
Style Guide for Python Code. Code should be written in a way that does not disadvantage other implementations of Python (PyPy, Jython, IronPython, Cython, Psyco, and such).For example, do not rely on CPython's efficient implementation of in-place string concatenation for statements in the form a += b or a = a + b.
This optimization is fragile even in CPython (it only works for some types) and isn't present at all in implementations that don't use refcounting. In performance sensitive parts of the library, the ''.join() form should be used instead. This will ensure that concatenation occurs in linear time across various implementations.Comparisons to singletons like None should always be done with is or is not, never the equality operators.Also, beware of writing if x when you really mean if x is not None -- e.g. when testing whether a variable or argument that defaults to None was set to some other value. The other value might have a type (such as a container) that could be false in a boolean context! GAEO ドキュメント日本語和訳バージョン.