
import os
import binascii


class Preferences:

    defaults = [
        ("session-key", binascii.b2a_hex(os.urandom(24))),
    ]

    def __init__(self, db):

        self.db = db
        self.db.execute([
            'CREATE TABLE IF NOT EXISTS preferences (',
            '   key VARCHAR PRIMARY KEY, value VARCHAR',
            ');'])

        for (key, value) in Preferences.defaults:
            if self.get(key) is None:
                self.set(key, value)

    def get(self, key, default=None):
        rv = self.db.execute(
                'SELECT value FROM preferences WHERE key=?', (key, )).fetchone()

        if rv is None:
            return default

        return rv[0]

    def set(self, key, value):
        self.db.execute(
            'INSERT INTO preferences (key, value) VALUES (?, ?)', (key, value))


import os
import binascii


class Preferences:

    defaults = [
        ("session-key", binascii.b2a_hex(os.urandom(24)).decode('utf-8')),
    ]

    def __init__(self, db):

        self.db = db
        self.db.execute([
            'CREATE TABLE IF NOT EXISTS preferences (',
            '   key VARCHAR PRIMARY KEY, value VARCHAR',
            ');'])

        for (key, value) in Preferences.defaults:
            if self.get(key) is None:
                self.set(key, value)

    def get(self, key, default=None):
        rv = self.db.execute(
                'SELECT value FROM preferences WHERE key=?', (key, )).fetchone()

        if rv is None:
            return default

        return rv[0]

    def set(self, key, value):
        self.db.execute(
            'INSERT INTO preferences (key, value) VALUES (?, ?)', (key, value))

Save the session-key as a unicode string in the db
Save the session-key as a unicode string in the db

The session-key should be saved as a string, not a byte string.

  
  import os
  import binascii
  
  
  class Preferences:
  
      defaults = [
-         ("session-key", binascii.b2a_hex(os.urandom(24))),
+         ("session-key", binascii.b2a_hex(os.urandom(24)).decode('utf-8')),
      ]
  
      def __init__(self, db):
  
          self.db = db
          self.db.execute([
              'CREATE TABLE IF NOT EXISTS preferences (',
              '   key VARCHAR PRIMARY KEY, value VARCHAR',
              ');'])
  
          for (key, value) in Preferences.defaults:
              if self.get(key) is None:
                  self.set(key, value)
  
      def get(self, key, default=None):
          rv = self.db.execute(
                  'SELECT value FROM preferences WHERE key=?', (key, )).fetchone()
  
          if rv is None:
              return default
  
          return rv[0]
  
      def set(self, key, value):
          self.db.execute(
              'INSERT INTO preferences (key, value) VALUES (?, ?)', (key, value))
  
Save the session-key as a unicode string in the db
## Code Before:

import os
import binascii


class Preferences:

    defaults = [
        ("session-key", binascii.b2a_hex(os.urandom(24))),
    ]

    def __init__(self, db):

        self.db = db
        self.db.execute([
            'CREATE TABLE IF NOT EXISTS preferences (',
            '   key VARCHAR PRIMARY KEY, value VARCHAR',
            ');'])

        for (key, value) in Preferences.defaults:
            if self.get(key) is None:
                self.set(key, value)

    def get(self, key, default=None):
        rv = self.db.execute(
                'SELECT value FROM preferences WHERE key=?', (key, )).fetchone()

        if rv is None:
            return default

        return rv[0]

    def set(self, key, value):
        self.db.execute(
            'INSERT INTO preferences (key, value) VALUES (?, ?)', (key, value))

## Instruction:
Save the session-key as a unicode string in the db
## Code After:

import os
import binascii


class Preferences:

    defaults = [
        ("session-key", binascii.b2a_hex(os.urandom(24)).decode('utf-8')),
    ]

    def __init__(self, db):

        self.db = db
        self.db.execute([
            'CREATE TABLE IF NOT EXISTS preferences (',
            '   key VARCHAR PRIMARY KEY, value VARCHAR',
            ');'])

        for (key, value) in Preferences.defaults:
            if self.get(key) is None:
                self.set(key, value)

    def get(self, key, default=None):
        rv = self.db.execute(
                'SELECT value FROM preferences WHERE key=?', (key, )).fetchone()

        if rv is None:
            return default

        return rv[0]

    def set(self, key, value):
        self.db.execute(
            'INSERT INTO preferences (key, value) VALUES (?, ?)', (key, value))
