人気のあるオープンソースのシステムと対応するPython APIのまとめ
Lucene
pylucene
Solr
SolPython
ElasticSearch
pyes
Sphinx
sphinxapi
人気のあるオープンソースのシステムと対応するPython APIのまとめ
Lucene
pylucene
Solr
SolPython
ElasticSearch
pyes
Sphinx
sphinxapi
from timeit import timeit def make_list_1(): result = [] for value in range(1000): result.append(value) return result def make_list_2(): result = [value for value in range(1000)] return result print('make_list_1 takes', timeit(make_list_1, number=1000), 'seconds') print('make_list_2 takes', timeit(make_list_2, number=1000), 'seconds')
結果
リスト内包表記 make_list_2()の方が、append()を使ったリストへの要素の追加と比べて高速。
time1.py
from time import time t1 = time() num = 5 num += 2 print(time() - t1)
time2.py
from time import time, sleep t1 = time() sleep(1.0) print(time() - t1)
標準モジュール timeit
timeit1.py
from timeit import timeit print(timeit('num = 5; num *= 2', number=1))
timeit2.py
from timeit import repeat print(repeat('num = 5; num *= 2', number=1, repeat=3))
ロギングのPython標準ライブラリ
loggingモジュールは次のようなコンセプトから構成されている
・ログに保存したいメッセージ
・ランク付けのための優先順位レベルとそれに対応する関数(debug()、info()、warn()、error()、critical())
・モジュールとの主要な通信経路となるひとつ以上のロガーオブジェクト
・メッセージを端末、ファイル、データベース、その他の場所に送るハンドラ
・出力を作成するフォーマッタ
・入力に基づいて判断を下すフィルタ
>>> import logging >>> logging.debug("Looks like rain") >>> logging.info("And hail") >>> logging.warn("Did I hear thinder?") <stdin>:1: DeprecationWarning: The 'warn' function is deprecated, use 'warning' instead WARNING:root:Did I hear thinder? >>> logging.warning("Did I hear thinder?") WARNING:root:Did I hear thinder? >>> logging.error("Was that lightning?") ERROR:root:Was that lightning? >>> logging.critical("Stop fencing and get inside!") CRITICAL:root:Stop fencing and get inside! >>>
>>> import logging >>> logging.basicConfig(level='DEBUG') >>> logger = logging.getLogger('bunyan') >>> logger.debug('Timber!') DEBUG:bunyan:Timber!
※ basicConfig()でデフォルトの表示レベルを指定できる。
デフォルトレベルはWARNING
もっとも低いレベルはDEBUGで、これを指定すると、すべてのレベルがログに表示される
>>> import logging >>> logging.basicConfig(level='DEBUG', filename='blue_ox.log') >>> logger = logging.getLogger('bunyan') >>> logger.debug("Where's my axe?") >>> logger.warning("I need my axe")
※ blue_ox.logにログが出力される(はず?)
>>> import logging >>> fmt = '%(asctime)s %(levelname)s %(lineno)s %(message)s' >>> logging.basicConfig(level='DEBUG', format=fmt) >>> logger = logging.getLogger('bunyan') >>> logger.error("Where's my other plaid shirt?")
※ 指定した形式でログが出力される(はず?)
Pythonの標準デバッガ
使い方
python -m pdb xxx.py
こうすると、プログラムが起動し、1行目で止まる。
(Pdb) c
c(continue、継続)と入力すると、プログラムを正常に、あるおはエラーで止まるところまで動く。
(Pdb) s
s(step、ステップ)と入力すると、ステップ実行する。
(Pdb) n
n(next)と入力すると、関数の中に入り込まずにステップオーバー(通り越し)する。
(Pdb) l
l(list)を入力すると、プログラムの数行先まで見ることができる
(Pdb) ll
ll(long list)を入力すると、プログラム全行を表示します。
(Pdb) b 3
3行目にブレークポイントを置く。
(Pdb) b
ただのb(ブレーク)と入力すると、すべてのブレークポイントを見ることができる。
(Pdb) q
q(quit)でデバッガを終了します。
サードパーティによるテストパッケージ
インストール
pip install nose
cap2
def just_do_it(test): # "引数testに含まれるすべての単語をタイトルケースとして返す" from string import capwords return capwords(test) #return test.title() #return test.capitalize()
test_cap_nose.py
import cap2 from nose.tools import eq__ #??? importできない def test_one_word(): text = 'duck' result = cap2.just_do_it(text) eq__(result, 'Duck') def test_multiple_words(self): text = 'a varitable flock of ducks' result = cap2.just_do_it(text) eq__(result, 'A Varitable Flock Of Ducks') def test_words_with_apostrophes(self): text = "I'm fresh out of ideas" result = cap2.just_do_it(text) eq__(result, "I'm Fresh Out Of Ideas") def test_words_with_quotes(self): text = "\"You're despicable,\" said Daffy Duck" result = cap2.just_do_it(text) eq__(result, "\"You're Despicable,\" Said Daffy Duck")