odbm
プロジェクトの最適化=>ストレージの最適化=> dbm => dictスタイル=> :-( => odmb => :-)
バックエンド
- tokyo cabinet (via tokyo-python )-アナログよりも数倍速い、dbファイルの方がコンパクトです
- kyoto cabinet ( ネイティブwrapperを介して)-tokyo-cabinetの作者とその説明はよりクールですが、私のテストによると2〜3倍遅いです
- gdbm
- fsdbm-大きなドキュメントを保存するのに便利
- ...何かが欠けている場合に書き込みます
設置
pip install odbm
カブ: bitbucket
構文
from datetime import datetime, date import odbm # class User(odbm.Model): username = odbm.UnicodeProperty(primary_key=True) birthday = odbm.DateProperty(key='b') is_active = odbm.Property(key='a', default=True) is_admin = odbm.Property(key='r', default=False) created = odbm.DateTimeProperty(key='c') __filename__ = 'var/user.odbm.tch' __db_type__ = 'tc' # User(username='fizban', birthday=date(1917, 1, 1), created=datetime.now()).save() # dict-style : # db['fizban'] = {'b': timestamp , 'c': timestamp } # (. key=... property) # u = User.get('fizban') print u.birthday # datetime.date(1917, 1, 1) # u.is_active = False u.save() # for i in xrange(10): User(username=('test-%i' % i), birthday=date(1950 + i, 1, 1)).save() # User.find() # c birthday > 1955, username for obj in User.find( filter = lambda u: u.birthday.year > 1955, order = lambda u: u.username): print obj # print User.count(lambda u: u.username.startswith('f')) # 1 # print User.count() # 11 User.find_one().delete() print User.count() # 10
物件
- odbm.Property()-ここでは、マーシャライズ可能な任意の型を突き出すことができます (たとえば、{'a':[True、1、None]})
- odbm.UnicodeProperty()-Unicode文字列の場合、ラテンアスキーおよびUnicodeキーとの混乱を避けるため
- odbm.DateTimeProperty()-datetime.datetime
- odbm.DateProperty() - datetime.date
- odbm.CompressedProperty()-odbm.Propertyに似ていますが、圧縮されています
- odbm.CompressedUnicodeProperty-odbm.UnicodePropertyに似ていますが、圧縮されています
性能
テストのために、上記のモデルの10kユーザーをランダムな順序で記述してカウントします。 そして、cPickle辞書の形で同じデータを読み書きすることと比較してください:
seconds to write/read 10000 rows
pickle (write, read): (1.0030829906463623, 0.25429201126098633)
odbm (write, read): (0.99477910995483398, 0.14065694808959961)
pickle file size: 2768704
odbm file size: 1348752
上記のキーの長さの削減により、ファイルサイズの削減が達成されました。 テストコードはこちらです。
[offtop] dbファイルのコンパクト性の基本的な重要性について
1年前、一連のテストを実施しました。subdエンジンに関係なく、使用されているファイルがファイルシステムキャッシュに置かれないとすぐに、ランダムサンプリングの速度が数桁低下します。 数字に興味がある人のために、これらのテストの残りすべてを以下に示します。