Kivy – Storage


Kivy – Storage



”;


The Storage class in Kivy framework is provided to load and store any number of key-value pairs via an indexed entry. The “kivy.storage” module defines the AbstractStore class. Its implementations − DictStore, JsonStore and RedisStore − provide the concrete classes.

  • kivy.storage.dictstore.DictStore: use a python dict as a store.

  • kivy.storage.jsonstore.JsonStore: use a JSON file as a store.

  • kivy.storage.redisstore.RedisStore: use a Redis database with redis-py.

To use any of the above storage classes, import the relevant class, declare an object and call its put() method to store the k-v pairs. For JsonStore −


from kivy.storage.jsonstore import JsonStore

store = JsonStore(''hello.json'')

# put some values
store.put(name, key1=val1, key2=val2)

This will create hello.json file in the current directory. You can retrieve the information with get() method.


print (store.get(name)[key])

Following methods are defined in AbstractStore class, which need to be overridden by the concrete implementations like DictStore −

  • clear() − Wipe the whole storage.

  • count() − Return the number of entries in the storage.

  • delete(key) − Delete a key from the storage. If the key is not found, a KeyError exception will be thrown.

  • exists(key) − Check if a key exists in the store.

  • find(**filters) − Return all the entries matching the filters. The entries are returned through a generator as a list of (key, entry) pairs where entry is a dict of key-value pairs.

  • get(key) − Get the key-value pairs stored at key. If the key is not found, a KeyError exception will be thrown.

  • keys() − Return a list of all the keys in the storage.

  • put(key, **values) − Put new key-value pairs (given in values) into the storage. Any existing key-value pairs will be removed.

The methods (get(), put() , exists(), delete(), find()) have an asynchronous version. These methods can be called with or without a callback parameter. If given, the callback returns the result to the user when available, as the request will be asynchronous. If the callback is None, then the request will be synchronous and the result will be returned directly.

Example

Here is the example −


# synchronous
res=store.get(key)
print (res)

# asynchronous
def my_callback(store, key, result):
   print (result)
   
store.get(key) 

The callback function should have these following parameters −

  • store − the ”Store” instance currently used.

  • key − the key sought for.

  • result − the result of the lookup for the key.

Example


from kivy.storage.jsonstore import JsonStore
from kivy.storage.dictstore import DictStore

store = JsonStore(''store.json'')

# put some values
store.put(''state'', name=''Maharashtra'', capital=''Mumbai'',
population=''Eleven Cr'')
store.put(''os'', name=''Windows'', version=11, released=2021)
store.put(''shape'', type=''circle'', radius=5)

# using the same index key erases all previously added k-v pairs
# get a value using a index key and key
print(''Population of '', store.get(''state'')[''name''], ''is '',
store.get(''state'')[''population''])
print (store.get(''state'').keys())
for k,v in store.get(''state'').items():
   print (k,":",v)
   
# or guess the key/entry for a part of the key
for item in store.find(type=''circle''):
   print(''Store:'',item[0])
   print(''K-V pairs: '',str(item[1]))

Output

It will produce the following output −


Population of Maharashtra is Eleven Cr
dict_keys([''name'', ''capital'', ''population''])
name : Maharashtra
capital : Mumbai
population : Eleven Cr
Store: shape
K-V pairs: {''type'': ''circle'', ''radius'': 5}

Advertisements

”;

Leave a Reply

Your email address will not be published. Required fields are marked *