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:_default
db.insert(...)
default_table
TinyDB
DEFAULT_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_size
table(...)
>>> 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_size
None
cache_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