SQLite3とdatasetよりもjsonとの親和性が高い。
成果物
Python.TinyDB.Hello.201706191414
インストール
(webapi) $ pip install tinydb Collecting tinydb Downloading tinydb-3.3.0.tar.gz Installing collected packages: tinydb Running setup.py install for tinydb ... done Successfully installed tinydb-3.3.0
あっさりできた。
参考
HelloWorld
$ python hello.py [{'name': '山田', 'created': '2017-06-19 14:23:01.104576'}]
hello.py
import tinydb import datetime db = tinydb.TinyDB('human.json') table = db.table('Human') table.insert({'name': '山田', 'created': '{0:%Y-%m-%d %H:%M:%S.%f}'.format(datetime.datetime.now())}) #db.purge_tables()
human.json
{"_default": {}, "Human": {"1": {"name": "\u5c71\u7530", "created": "2017-06-19 14:23:01.104576"}}}
このファイル形式は残念。悪い所を集めたかのような形式。
おそらく文字コード絡みの不具合をなくすためなのだろうが…。ファイルサイズやアクセス速度的に考えて、SQLite3よりもさらに小規模なものにしか使えない。
短所
http://tinydb.readthedocs.io/en/latest/intro.html#why-use-tinydb
- ファイルサイズが大きい
- Unicode文字列がプレーンテキストで編集できない
- 制約が使えない
unique
,check
,default
,not null
, 外部キー制約
- 検索の手順が面倒
長所
SQLite3やdatasetで不満だった正規表現検索ができる。
- 正規表現が使える
- any, allが使える
所感
SQLite3の代わりにするのは難しそう。より小規模で使う分には候補になるが、それならjsonファイルで十分な気がする。使いどころがわからない。