存储

在 1.7.0 版本加入.

警告

该模块仍处于实验阶段,API 在未来的版本中可能会有所变动。

用法

存储模块的设计理念是能够通过索引键加载/存储任意数量的键值对。默认模型是抽象的,因此您不能直接使用它。我们提供了一些实现,例如:

示例

例如,让我们使用一个 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)[源代码]

基类:EventDispatcher

用于实现存储的抽象类。

async_clear(callback)[源代码]

clear() 的异步版本。

async_count(callback)[源代码]

异步返回存储中的条目数量。

async_delete(callback, key)[源代码]

: delete() 的异步版本。

回调参数:
storeAbstractStore 实例

存储实例

key:字符串

要搜索的键名。

result:布尔值

如果存储已更新,则返回True;如果未进行任何操作(无更改),则返回False;如果出现任何错误,则返回None。

async_exists(callback, key)[源代码]

: exists() 的异步版本。

回调参数:
storeAbstractStore 实例

存储实例

key:字符串

要搜索的键名。

result:布尔值

查询结果,若有错误则为None

async_find(callback, **filters)[源代码]

: find() 的异步版本。

回调将对结果中的每个条目进行调用。

回调参数:
storeAbstractStore 实例

存储实例

key:字符串

要搜索的键名,如果到达结果末尾则为 None。

result:布尔值

如果存储已更新,则返回True;如果未进行任何操作(无更改),则返回False;如果出现任何错误,则返回None。

async_get(callback, key)[源代码]

get() 的异步版本。

回调参数:
storeAbstractStore 实例

存储实例

key:字符串

要搜索的键名。

result:字典

查询结果,若有错误则为None

async_keys(callback)[源代码]

异步返回存储中的所有键。

async_put(callback, key, **values)[源代码]

: put() 的异步版本。

回调参数:
storeAbstractStore 实例

存储实例

key:字符串

要搜索的键名。

result:布尔值

如果存储已更新,则返回True;如果未进行任何操作(无更改),则返回False;如果出现任何错误,则返回None。

clear()[源代码]

清空整个存储。

count()[源代码]

返回存储中的条目数量。

delete(key)[源代码]

从存储中删除一个键。如果找不到该键,将抛出 KeyError 异常。

exists(key)[源代码]

检查存储中是否存在某个键。

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')))
get(key)[源代码]

获取存储在`key`处的键值对。如果未找到该键,将抛出`KeyError`异常。

keys()[源代码]

返回存储中所有键的列表。

put(key, **values)[源代码]

将*values*中的新键值对放入存储中。任何现有的键值对将被移除。