純Python開發面向文檔的數據庫:TinyDB

jopen 10年前發布 | 21K 次閱讀 TinyDB NoSQL數據庫

TinyDB是為了你的幸福而優化的微小的,面向文檔的數據庫。它采用純Python開發并且沒有額外的需求。它非常適合于小的應用。
logo.png

TinyDB 特性:

  • tiny: 當前源代碼只有1200行(+ 600 lines tests). For comparison: Buzhug has about 2000 lines of code (w/o tests), CodernityDB has about 8000 lines of code (w/o tests).
  • document oriented: 類似 MongoDB, 你可以在TinyDB中存儲任何文檔(represented as dict)
  • optimized for your happiness: TinyDB的設計是簡單而有趣提供了一個簡單和干凈的API來使用。
  • written in pure Python: TinyDB neither needs an external server (as e.g. PyMongo) nor any dependencies from PyPI.
  • works on Python 2.6 – 3.4 and PyPy: TinyDB works on all modern versions of Python and PyPy.
  • easily extensible: You can easily extend TinyDB by writing new storages or modify the behaviour of storages with Middlewares.
  • nearly 100% test coverage: If you don't count that __repr__ methods and some abstract methods are not tested, TinyDB has a test coverage of 100%.

Example Code

>>> from tinydb import TinyDB, where
>>> db = TinyDB('/path/to/db.json')
>>> db.insert({'int': 1, 'char': 'a'})
>>> db.insert({'int': 1, 'char': 'b'})

Query Language

>>> # Search for a field value
>>> db.search(where('int') == 1)
[{'int': 1, 'char': 'a'}, {'int': 1, 'char': 'b'}]

>>> # Combine two queries with logical and
>>> db.search((where('int') == 1) & (where('char') == 'b'))
[{'int': 1, 'char': 'b'}]

>>> # Combine two queries with logical or
>>> db.search((where('char') == 'a') | (where('char') == 'b'))
[{'int': 1, 'char': 'a'}, {'int': 1, 'char': 'b'}]

>>> # More possible comparisons:  !=  <  >  <=  >=
>>> # More possible checks: where(...).matches(regex), where(...).test(your_test_func)

Tables

>>> table = db.table('name')
>>> table.insert({'value': True})
>>> table.all()
[{'value': True}]

Using Middlewares

>>> from tinydb.storages import JSONStorage
>>> from tinydb.middlewares import CachingMiddleware
>>> db = TinyDB('/path/to/db.json', storage=CachingMiddleware(JSONStorage))

項目主頁:http://www.baiduhome.net/lib/view/home/1411981254890

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!