'''
Abstraction of TinyDB table for storing config
'''
from tinydb import Query


class KeyValueTable:
    """Wrapper around a TinyDB table.

    """

    def __init__(self, tdb, name='_default'):
        self.table = tdb.table(name)
        self.setting = Query()

    def get(self, key):
        """Get the value of named setting or None if it doesn't exist.

        """
        result = self.table.get(self.setting.key == key)
        if result:
            return result['value']
        return None

    def set(self, key, value):
        """Insert or update named setting with given value.

        """
        if self.table.contains(self.setting.key == key):
            self.table.update({'value': value}, self.setting.key == key)
        else:
            self.table.insert({'key': key, 'value': value})

    def __getitem__(self, key):
        return self.get(key)

    def __setitem__(self, key, value):
        return self.set(key, value)

@@ -142,16 +142,38 @@
 %0A    %22%22%22
+%0A    setting = Query()
 %0A%0A    de
@@ -252,39 +252,8 @@
 ame)
-%0A        self.setting = Query()
 %0A%0A  
@@ -602,19 +602,16 @@
         
-if 
 self.tab
@@ -617,42 +617,40 @@
 ble.
-contains(self.setting.key ==
+upsert(%7B%0A            'key':
  key
-):
+,
 %0A   
@@ -662,27 +662,8 @@
     
-self.table.update(%7B
 'val
@@ -672,16 +672,25 @@
 ': value
+%0A        
 %7D, self.
@@ -712,82 +712,8 @@
 key)
-%0A        else:%0A            self.table.insert(%7B'key': key, 'value': value%7D)
 %0A%0A  

Use upsert to reduce chance of duplicates