python开发数据库_TinyDB:一个轻量级的面向文档的数据库

logo.png

68747470733a2f2f696d672e736869656c64732e696f2f617a7572652d6465766f70732f6275696c642f6d7369656d656e732f33653562616137352d313265632d343361632d393732382d3839383233656538633765322f322e7376673f7374796c653d666c61742d737175617265687474703a2f2f696d672e736869656c64732e696f2f636f766572616c6c732f6d7369656d656e732f74696e7964622e7376673f7374796c653d666c61742d737175617265687474703a2f2f696d672e736869656c64732e696f2f707970692f762f74696e7964622e7376673f7374796c653d666c61742d737175617265

Quick Links

Introduction

TinyDB is a lightweight document oriented database optimized for your happiness :) It's written in pure Python and has no external dependencies. The target are small apps that would be blown away by a SQL-DB or an external database server.

TinyDB is:

tiny: The current source code has 1800 lines of code (with about 40% documentation) and 1600 lines tests.

document oriented: Like MongoDB, you can store any document (represented as dict) in TinyDB.

optimized for your happiness: TinyDB is designed to be simple and fun to use by providing a simple and clean API.

written in pure Python: TinyDB neither needs an external server (as e.g. PyMongo) nor any dependencies from PyPI.

works on Python 3.5+ and PyPy: TinyDB works on all modern versions of Python and PyPy.

powerfully extensible: You can easily extend TinyDB by writing new storages or modify the behaviour of storages with Middlewares.

100% test coverage: No explanation needed.

To dive straight into all the details, head over to the TinyDB docs. You can also discuss everything related to TinyDB like general development, extensions or showcase your TinyDB-based projects on the discussion forum.

Supported Python Versions

TinyDB has been tested with Python 3.5 - 3.8 and PyPy.

Example Code

>>> from tinydb import TinyDB, Query

>>> db = TinyDB('/path/to/db.json')

>>> db.insert({'int': 1, 'char': 'a'})

>>> db.insert({'int': 1, 'char': 'b'})

Query Language

>>> User = Query()

>>> # Search for a field value

>>> db.search(User.name == 'John')

[{'name': 'John', 'age': 22}, {'name': 'John', 'age': 37}]

>>> # Combine two queries with logical and

>>> db.search((User.name == 'John') & (User.age <= 30))

[{'name': 'John', 'age': 22}]

>>> # Combine two queries with logical or

>>> db.search((User.name == 'John') | (User.name == 'Bob'))

[{'name': 'John', 'age': 22}, {'name': 'John', 'age': 37}, {'name': 'Bob', 'age': 42}]

>>> # 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))

Contributing

Whether reporting bugs, discussing improvements and new ideas or writing extensions: Contributions to TinyDB are welcome! Here's how to get started:

Check for open issues or open a fresh issue to start a discussion around a feature idea or a bug

Fork the repository on Github, create a new branch off the master branch and start making your changes (known as GitHub Flow)

Write a test which shows that the bug was fixed or that the feature works as expected

Send a pull request and bug the maintainer until it gets merged and published

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值