Key/Value Store

The playhouse.kv module contains the implementation of a persistentdictionary.

  • class KeyValue([key_field=None[, value_field=None[, ordered=False[, database=None[, table_name='keyvalue']]]]])

Parameters:

  • key_field (Field) – field to use for key. Defaults toCharField. Must have primary_key=True.
  • value_field (Field) – field to use for value. Defaults toPickleField.
  • ordered (bool) – data should be returned in key-sorted order.
  • database (Database) – database where key/value data is stored. If notspecified, an in-memory SQLite database will be used.
  • table_name (str) – table name for data storage.

Dictionary-like API for storing key/value data. Like dictionaries, supportsthe expected APIs, but also has the added capability of acceptingexpressions for getting, setting and deleting items.

Table is created automatically (if it doesn’t exist) when the KeyValueis instantiated.

Uses efficient upsert implementation for setting and updating/overwritingkey/value pairs.

Basic examples:

  1. # Create a key/value store, which uses an in-memory SQLite database
  2. # for data storage.
  3. KV = KeyValue()
  4.  
  5. # Set (or overwrite) the value for "k1".
  6. KV['k1'] = 'v1'
  7.  
  8. # Set (or update) multiple keys at once (uses an efficient upsert).
  9. KV.update(k2='v2', k3='v3')
  10.  
  11. # Getting values works as you'd expect.
  12. assert KV['k2'] == 'v2'
  13.  
  14. # We can also do this:
  15. for value in KV[KV.key > 'k1']:
  16. print(value)
  17.  
  18. # 'v2'
  19. # 'v3'
  20.  
  21. # Update multiple values at once using expression:
  22. KV[KV.key > 'k1'] = 'vx'
  23.  
  24. # What's stored in the KV?
  25. print(dict(KV))
  26.  
  27. # {'k1': 'v1', 'k2': 'vx', 'k3': 'vx'}
  28.  
  29. # Delete a single item.
  30. del KV['k2']
  31.  
  32. # How many items are stored in the KV?
  33. print(len(KV))
  34. # 2
  35.  
  36. # Delete items that match the given condition.
  37. del KV[KV.key > 'k1']
  • contains(expr)

Parameters:expr – a single key or an expressionReturns:Boolean whether key/expression exists.

Example:

  1. >>> kv = KeyValue()
  2. >>> kv.update(k1='v1', k2='v2')
  3.  
  4. >>> 'k1' in kv
  5. True
  6. >>> 'kx' in kv
  7. False
  8.  
  9. >>> (KV.key < 'k2') in KV
  10. True
  11. >>> (KV.key > 'k2') in KV
  12. False
  • len()

Returns:Count of items stored.

  • getitem(expr)

Parameters:expr – a single key or an expression.Returns:value(s) corresponding to key/expression.Raises:KeyError if single key given and not found.

Examples:

  1. >>> KV = KeyValue()
  2. >>> KV.update(k1='v1', k2='v2', k3='v3')
  3.  
  4. >>> KV['k1']
  5. 'v1'
  6. >>> KV['kx']
  7. KeyError: "kx" not found
  8.  
  9. >>> KV[KV.key > 'k1']
  10. ['v2', 'v3']
  11. >>> KV[KV.key < 'k1']
  12. []
  • setitem(expr, value)

Parameters:

  1. - **expr** a single key or an expression.
  2. - **value** value to set for key(s)

Set value for the given key. If expr is an expression, then anykeys matching the expression will have their value updated.

Example:

  1. >>> KV = KeyValue()
  2. >>> KV.update(k1='v1', k2='v2', k3='v3')
  3.  
  4. >>> KV['k1'] = 'v1-x'
  5. >>> print(KV['k1'])
  6. 'v1-x'
  7.  
  8. >>> KV[KV.key >= 'k2'] = 'v99'
  9. >>> dict(KV)
  10. {'k1': 'v1-x', 'k2': 'v99', 'k3': 'v99'}
  • delitem(expr)

Parameters:expr – a single key or an expression.

Delete the given key. If an expression is given, delete all keys thatmatch the expression.

Example:

  1. >>> KV = KeyValue()
  2. >>> KV.update(k1=1, k2=2, k3=3)
  3.  
  4. >>> del KV['k1'] # Deletes "k1".
  5. >>> del KV['k1']
  6. KeyError: "k1" does not exist
  7.  
  8. >>> del KV[KV.key > 'k2'] # Deletes "k3".
  9. >>> del KV[KV.key > 'k99'] # Nothing deleted, no keys match.
  • keys()

Returns:an iterable of all keys in the table.

  • values()

Returns:an iterable of all values in the table.

  • items()

Returns:an iterable of all key/value pairs in the table.

  • update([__data=None[, **mapping]])
  • Efficiently bulk-insert or replace the given key/value pairs.

Example:

  1. >>> KV = KeyValue()
  2. >>> KV.update(k1=1, k2=2) # Sets 'k1'=1, 'k2'=2.
  3.  
  4. >>> dict(KV)
  5. {'k1': 1, 'k2': 2}
  6.  
  7. >>> KV.update(k2=22, k3=3) # Updates 'k2'->22, sets 'k3'=3.
  8.  
  9. >>> dict(KV)
  10. {'k1': 1, 'k2': 22, 'k3': 3}
  11.  
  12. >>> KV.update({'k2': -2, 'k4': 4}) # Also can pass a dictionary.
  13.  
  14. >>> dict(KV)
  15. {'k1': 1, 'k2': -2, 'k3': 3, 'k4': 4}
  • get(expr[, default=None])

Parameters:

  1. - **expr** a single key or an expression.
  2. - **default** default value if key not found.Returns:

value of given key/expr or default if single key not found.

Get the value at the given key. If the key does not exist, the defaultvalue is returned, unless the key is an expression in which case anempty list will be returned.

  • pop(expr[, default=Sentinel])

Parameters:

  1. - **expr** a single key or an expression.
  2. - **default** default value if key does not exist.Returns:

value of given key/expr or default if single key not found.

Get value and delete the given key. If the key does not exist, thedefault value is returned, unless the key is an expression in whichcase an empty list is returned.

  • clear()
  • Remove all items from the key-value table.