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

    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:

    • __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
    • ()

      Returns:Count of items stored.
    • 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:

    • keys()

      Returns:an iterable of all keys in the table.
    • values()

    • items()

      Returns:an iterable of all key/value pairs in the table.
    • ([__data=None[, \*mapping*]])

      Example:

      1. >>> KV = KeyValue()
      2. >>> dict(KV)
      3. {'k1': 1, 'k2': 2}
      4. >>> KV.update(k2=22, k3=3) # Updates 'k2'->22, sets 'k3'=3.
      5. >>> dict(KV)
      6. {'k1': 1, 'k2': 22, 'k3': 3}
      7. >>> KV.update({'k2': -2, 'k4': 4}) # Also can pass a dictionary.
      8. >>> dict(KV)
      9. {'k1': 1, 'k2': -2, 'k3': 3, 'k4': 4}

      Attention

      Because Postgresql does not support INSERT + REPLACE, the 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])

      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.