【学库】TinyDB(其他知识点)

TinyDB(其他一些很有必要知道的内容)

Document IDs的使用

 .doc_id :单一字段的ID

 .doc_ids :字段列表的ID列表

>>> el = db.get(User.name == 'John')
>>> el.doc_id
3 # 直接返回ID数字

使用 .doc_id 和 .doc_ids ,可以类似query一样,作为查询条件进行操作。

>>> db.update({'value': 2}, doc_ids=[1, 2])
>>> db.contains(doc_ids=[1])
True
>>> db.remove(doc_ids=[1, 2])
>>> db.get(doc_id=3)
{...}

Tables 

一个json文件中可以存多张表。操作和TinyDB类一样。

 db.table(name) :创建一个新表,或者读取以name命名的表

 db.purge_table('table_name') :清空‘table_name’这张表

 db.purge_tables() :清空所有的表

 db.tables() :返回表名字的列表,但是是用 { '_default', 'table_name' }。

还有一些很复杂的设置可以操作:

Default Table

TinyDB uses a table named as the default table. All operations on the database object (like ) operate on this table. The name of this table can be modified by either passing to the constructor or by setting the class variable to modify the default table name for all instances:_defaultdb.insert(...)default_tableTinyDBDEFAULT_TABLE

>>> #1: for a single instance only
>>> TinyDB(storage=SomeStorage, default_table='my-default')
>>> #2: for all instances
>>> TinyDB.DEFAULT_TABLE = 'my-default'

You also can modify the keyword arguments that are passed to the default table by setting . For example, you can disable the query cache for the default table by setting like this:TinyDB.DEFAULT_TABLE_KWARGS

>>> TinyDB.DEFAULT_TABLE_KWARGS = {'cache_size': 0}

Query Caching

TinyDB caches query result for performance. You can optimize the query cache size by passing the to the function:cache_sizetable(...)

>>> table = db.table('table_name', cache_size=30) 

Hint:

You can set to to make the cache unlimited in size. Also, you can set to 0 to disable it.cache_sizeNonecache_size

Storage & Middleware

1、JSON格式文件:默认形式,直接指定文件路径获取

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

2、in-memory形式:可能就是直接放在内存里面,对位置参数设置成 MemoryStorage

from tinydb.storages import MemoryStorage
db = TinyDB(storage=MemoryStorage)

下面的基本上没有看懂:

In case you need to access the storage instance directly, you can use the property of your TinyDB instance. This may be useful to call method directly on the storage or middleware:storage

>>> db = TinyDB(storage=CachingMiddleware(MemoryStorage))
<tinydb.middlewares.CachingMiddleware at 0x10991def0>
>>> db.storage.flush()

Middleware

Middleware wraps around existing storage allowing you to customize their behaviour.

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

Hint:

You can nest middleware:

>>> db = TinyDB('/path/to/db.json',
                storage=FirstMiddleware(SecondMiddleware(JSONStorage)))

CachingMiddleware

The improves speed by reducing disk I/O. It caches all read operations and writes data to disk after a configured number of write operations.CachingMiddleware

To make sure that all data is safely written when closing the table, use one of these ways:

# Using a context manager:
with database as db:
    # Your operations
# Using the close function
db.close()

到这来就全部结束了,需要扩展查看官方文档。

Extending TinyDB

API Documentation

上一节:TinyDB(玩数据)

OVER

猜你喜欢

转载自www.cnblogs.com/watalo/p/12395870.html
今日推荐