Result.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. from contextlib import closing
  2. from json import dumps, loads
  3. class Result:
  4. """
  5. database class for results
  6. """
  7. def __init__(self, database, row):
  8. self.database = database
  9. self.identifier = row[0]
  10. self.file_id = row[1]
  11. self.origin = row[2]
  12. self.type = row[3]
  13. self.label = row[4]
  14. self.data = loads(row[5]) if row[5] is not None else None
  15. def remove(self):
  16. """
  17. remove this result from the database
  18. :return:
  19. """
  20. with closing(self.database.con.cursor()) as cursor:
  21. cursor.execute('DELETE FROM results WHERE id = ?', [self.identifier])
  22. def set_origin(self, origin: str):
  23. """
  24. set this results origin
  25. :param origin: either 'user' or 'pipeline'
  26. :return:
  27. """
  28. with closing(self.database.con.cursor()) as cursor:
  29. cursor.execute('UPDATE results SET origin = ? WHERE id = ?', (origin, self.identifier))
  30. self.origin = origin
  31. def set_label(self, label: int):
  32. """
  33. set this results label
  34. :param label: label id
  35. :return:
  36. """
  37. with closing(self.database.con.cursor()) as cursor:
  38. cursor.execute('UPDATE results SET label = ? WHERE id = ?', (label, self.identifier))
  39. self.label = label
  40. def set_data(self, data: dict):
  41. """
  42. set this results data object
  43. :param data: data object
  44. :return:
  45. """
  46. if data is None:
  47. data_txt = None
  48. else:
  49. data_txt = dumps(data)
  50. with closing(self.database.con.cursor()) as cursor:
  51. cursor.execute('UPDATE results SET data = ? WHERE id = ?', (data_txt, self.identifier))
  52. self.data = data