Result.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 file(self):
  16. """
  17. getter for the according file
  18. :return: file object
  19. """
  20. return self.database.file(self.file_id)
  21. def remove(self):
  22. """
  23. remove this result from the database
  24. :return:
  25. """
  26. with closing(self.database.con.cursor()) as cursor:
  27. cursor.execute('DELETE FROM results WHERE id = ?', [self.identifier])
  28. def set_origin(self, origin: str):
  29. """
  30. set this results origin
  31. :param origin: either 'user' or 'pipeline'
  32. :return:
  33. """
  34. with closing(self.database.con.cursor()) as cursor:
  35. cursor.execute('UPDATE results SET origin = ? WHERE id = ?', (origin, self.identifier))
  36. self.origin = origin
  37. def set_label(self, label: int):
  38. """
  39. set this results label
  40. :param label: label id
  41. :return:
  42. """
  43. with closing(self.database.con.cursor()) as cursor:
  44. cursor.execute('UPDATE results SET label = ? WHERE id = ?', (label, self.identifier))
  45. self.label = label
  46. def set_data(self, data: dict):
  47. """
  48. set this results data object
  49. :param data: data object
  50. :return:
  51. """
  52. if data is None:
  53. data_txt = None
  54. else:
  55. data_txt = dumps(data)
  56. with closing(self.database.con.cursor()) as cursor:
  57. cursor.execute('UPDATE results SET data = ? WHERE id = ?', (data_txt, self.identifier))
  58. self.data = data