目录
- 存储
- 用法
- 示例
- 同步 / 异步 API
- 同步容器类型
AbstractStoreAbstractStore.async_clear()AbstractStore.async_count()AbstractStore.async_delete()AbstractStore.async_exists()AbstractStore.async_find()AbstractStore.async_get()AbstractStore.async_keys()AbstractStore.async_put()AbstractStore.clear()AbstractStore.count()AbstractStore.delete()AbstractStore.exists()AbstractStore.find()AbstractStore.get()AbstractStore.keys()AbstractStore.put()
存储¶
在 1.7.0 版本加入.
警告
该模块仍处于实验阶段,API 在未来的版本中可能会有所变动。
用法¶
存储模块的设计理念是能够通过索引键加载/存储任意数量的键值对。默认模型是抽象的,因此您不能直接使用它。我们提供了一些实现,例如:
kivy.storage.dictstore.DictStore:使用 Python 字典作为存储。kivy.storage.jsonstore.JsonStore:使用一个 JSON 文件作为存储。kivy.storage.redisstore.RedisStore:使用 redis-py 与 Redis 数据库进行交互。
示例¶
例如,让我们使用一个 JsonStore:
from kivy.storage.jsonstore import JsonStore
store = JsonStore('hello.json')
# put some values
store.put('tito', name='Mathieu', org='kivy')
store.put('tshirtman', name='Gabriel', age=27)
# using the same index key erases all previously added key-value pairs
store.put('tito', name='Mathieu', age=30)
# get a value using a index key and key
print('tito is', store.get('tito')['age'])
# or guess the key/entry for a part of the key
for item in store.find(name='Gabriel'):
print('tshirtmans index key is', item[0])
print('his key value pairs are', str(item[1]))
由于数据是持久化的,您可以稍后检查该键是否存在::
from kivy.storage.jsonstore import JsonStore
store = JsonStore('hello.json')
if store.exists('tito'):
print('tite exists:', store.get('tito'))
store.delete('tito')
同步 / 异步 API¶
所有标准方法(get()、put()、exists()、delete()、find())都有对应的异步版本。
例如,get 方法有一个 callback 参数。如果设置了该参数,则 callback 将用于在结果可用时将其返回给用户:此时请求将是异步的。如果 callback 为 None,则请求将是同步的,结果将直接返回。
无回调(同步API):
entry = mystore.get('tito')
print('tito =', entry)
使用回调(异步API):
def my_callback(store, key, result):
print('the key', key, 'has a value of', result)
mystore.get('plop', callback=my_callback)
回调签名(几乎所有方法)为:
def callback(store, key, result):
"""
store: the `Store` instance currently used.
key: the key sought for.
result: the result of the lookup for the key.
"""
同步容器类型¶
存储API模拟了同步API的容器类型:
store = JsonStore('hello.json')
# original: store.get('tito')
store['tito']
# original: store.put('tito', name='Mathieu')
store['tito'] = {'name': 'Mathieu'}
# original: store.delete('tito')
del store['tito']
# original: store.count()
len(store)
# original: store.exists('tito')
'tito' in store
# original: for key in store.keys()
for key in store:
pass
- class kivy.storage.AbstractStore(**kwargs)[源代码]¶
-
用于实现存储的抽象类。
- async_delete(callback, key)[源代码]¶
:
delete()的异步版本。- 回调参数:
- store:
AbstractStore实例 存储实例
- key:字符串
要搜索的键名。
- result:布尔值
如果存储已更新,则返回True;如果未进行任何操作(无更改),则返回False;如果出现任何错误,则返回None。
- store:
- async_exists(callback, key)[源代码]¶
:
exists()的异步版本。- 回调参数:
- store:
AbstractStore实例 存储实例
- key:字符串
要搜索的键名。
- result:布尔值
查询结果,若有错误则为None
- store:
- async_find(callback, **filters)[源代码]¶
:
find()的异步版本。回调将对结果中的每个条目进行调用。
- 回调参数:
- store:
AbstractStore实例 存储实例
- key:字符串
要搜索的键名,如果到达结果末尾则为 None。
- result:布尔值
如果存储已更新,则返回True;如果未进行任何操作(无更改),则返回False;如果出现任何错误,则返回None。
- store:
- async_get(callback, key)[源代码]¶
get()的异步版本。- 回调参数:
- store:
AbstractStore实例 存储实例
- key:字符串
要搜索的键名。
- result:字典
查询结果,若有错误则为None
- store:
- async_put(callback, key, **values)[源代码]¶
:
put()的异步版本。- 回调参数:
- store:
AbstractStore实例 存储实例
- key:字符串
要搜索的键名。
- result:布尔值
如果存储已更新,则返回True;如果未进行任何操作(无更改),则返回False;如果出现任何错误,则返回None。
- store:
- find(**filters)[源代码]¶
返回所有符合过滤条件的条目。条目通过生成器以(key, entry)对列表的形式返回,其中*entry*是一个键值对字典:
for key, entry in store.find(name='Mathieu'): print('key:', key, ', entry:', entry)
因为它是生成器,你不能直接将其用作列表。你可以这样做:
# get all the (key, entry) availables entries = list(store.find(name='Mathieu')) # get only the entry from (key, entry) entries = list((x[1] for x in store.find(name='Mathieu')))