Key/Value Store

The playhouse.kv module contains the implementation of a persistent dictionary.

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 to CharField. Must have primary_key=True.
  • value_field (Field) – field to use for value. Defaults to PickleField.
  • ordered (bool) – data should be returned in key-sorted order.
  • database (Database) – database where key/value data is stored. If not specified, 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, supports the expected APIs, but also has the added capability of accepting expressions for getting, setting and deleting items.

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

Basic examples:

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

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

    Example:

    1. >>> kv = KeyValue()
    2. >>> kv.update(k1='v1', k2='v2')
    3. >>> 'k1' in kv
    4. True
    5. >>> 'kx' in kv
    6. False
    7. >>> (KV.key < 'k2') in KV
    8. True
    9. >>> (KV.key > 'k2') in KV
    10. 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. >>> KV['k1']
    4. 'v1'
    5. >>> KV['kx']
    6. KeyError: "kx" not found
    7. >>> KV[KV.key > 'k1']
    8. ['v2', 'v3']
    9. >>> KV[KV.key < 'k1']
    10. []
  • __setitem__(expr, value)

    Parameters:
    • expr – a single key or an expression.
    • value – value to set for key(s)

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

    Example:

    1. >>> KV = KeyValue()
    2. >>> KV.update(k1='v1', k2='v2', k3='v3')
    3. >>> KV['k1'] = 'v1-x'
    4. >>> print(KV['k1'])
    5. 'v1-x'
    6. >>> KV[KV.key >= 'k2'] = 'v99'
    7. >>> dict(KV)
    8. {'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 that match the expression.

    Example:

    1. >>> KV = KeyValue()
    2. >>> KV.update(k1=1, k2=2, k3=3)
    3. >>> del KV['k1'] # Deletes "k1".
    4. >>> del KV['k1']
    5. KeyError: "k1" does not exist
    6. >>> del KV[KV.key > 'k2'] # Deletes "k3".
    7. >>> 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. >>> dict(KV)
    4. {'k1': 1, 'k2': 2}
    5. >>> KV.update(k2=22, k3=3) # Updates 'k2'->22, sets 'k3'=3.
    6. >>> dict(KV)
    7. {'k1': 1, 'k2': 22, 'k3': 3}
    8. >>> KV.update({'k2': -2, 'k4': 4}) # Also can pass a dictionary.
    9. >>> dict(KV)
    10. {'k1': 1, 'k2': -2, 'k3': 3, 'k4': 4}

    Attention

    Because Postgresql does not support INSERT + REPLACE, the KeyValue.update() method is not supported for Postgresql databases (as it cannot be implemented efficiently).

  • get(expr[, default=None])

    Parameters:
    • expr – a single key or an expression.
    • 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 default value is returned, unless the key is an expression in which case an empty list will be returned.

  • pop(expr[, default=Sentinel])

    Parameters:
    • expr – a single key or an expression.
    • 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, the default value is returned, unless the key is an expression in which case an empty list is returned.

  • clear()

    Remove all items from the key-value table.