跳转至

Operation log

labridge.callback.base.operation_log

labridge.callback.base.operation_log.OperationOutputLog

Bases: object

This class record the log of a specific callback operation. The operation_output will be a part of the corresponding tool output. The log_to_user and references in log_to_system will be presented to the users.

PARAMETER DESCRIPTION
operation_name

The operation name.

TYPE: str

operation_output

The operation output.

TYPE: str

log_to_user

This log might be presented to the users.

TYPE: str

log_to_system

This log is more structured, specifically, it is a dictionary in JSON format. The keys 'operation_description' and 'references' are required. The values of references are either None or List[str], where the str is in JSON format, for example, the dumped string of a PaperInfo.

TYPE: dict

Source code in labridge\callback\base\operation_log.py
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
class OperationOutputLog(object):
	r"""
	This class record the log of a specific callback operation.
	The `operation_output` will be a part of the corresponding tool output.
	The `log_to_user` and `references` in `log_to_system` will be presented to the users.

	Args:
		operation_name (str): The operation name.
		operation_output (str): The operation output.
		log_to_user (str): This log might be presented to the users.
		log_to_system (dict): This log is more structured, specifically, it is a dictionary in JSON format.
			The keys 'operation_description' and 'references' are required. The values of `references` are either
			None or List[str], where the `str` is in JSON format, for example, the dumped string of a `PaperInfo`.
	"""
	def __init__(
		self,
		operation_name: str,
		operation_output: Optional[str],
		log_to_user: Optional[str],
		log_to_system: Dict[str, Union[str, Optional[List[str]]]],
		operation_abort: Optional[bool] = False,

	):
		self.operation_name = operation_name
		self.operation_output = operation_output
		self.log_to_user = log_to_user
		self.operation_abort = operation_abort

		for key in LOG_TO_SYSTEM_KEYS:
			if key not in log_to_system.keys():
				raise ValueError(f"The key {key} is required in the log_to_system.")

		ref = log_to_system[OP_REFERENCES]
		if ref and not isinstance(ref, list):
			raise ValueError(f"The value of '{OP_REFERENCES}' can only be list or None.")
		self.log_to_system = log_to_system

	@classmethod
	def construct(
		cls,
		operation_name: str,
		operation_output: str,
		op_description: str,
		op_references: Optional[List[str]] = None,
		log_to_user: Optional[str] = None,
		operation_abort: Optional[bool] = False,
	):
		return cls(
			operation_name=operation_name,
			operation_output=operation_output,
			log_to_user=log_to_user,
			log_to_system={
				OP_DESCRIPTION: op_description,
				OP_REFERENCES: op_references,
			},
			operation_abort = operation_abort,
		)

	def dumps(self) -> str:
		r""" Dump to JSON string. """
		output_logs = {
			"operation_name": self.operation_name,
			"operation_output": self.operation_output,
			"log_to_user": self.log_to_user,
			"log_to_system": self.log_to_system,
			"operation_abort": self.operation_abort
		}
		return json.dumps(output_logs)

	@classmethod
	def loads(
		cls,
		log_str: str,
	):
		r""" Load from JSON string. """
		try:
			output_logs = json.loads(log_str)
			operation_name = output_logs["operation_name"]
			operation_output = output_logs["operation_output"]
			log_to_user = output_logs["log_to_user"]
			log_to_system = output_logs["log_to_system"]
			operation_abort = output_logs["operation_abort"]
			return cls(
				operation_name=operation_name,
				operation_output=operation_output,
				log_to_user=log_to_user,
				log_to_system=log_to_system,
				operation_abort=operation_abort,
			)
		except Exception:
			raise ValueError("Invalid operation log string.")

labridge.callback.base.operation_log.OperationOutputLog.dumps()

Dump to JSON string.

Source code in labridge\callback\base\operation_log.py
74
75
76
77
78
79
80
81
82
83
def dumps(self) -> str:
	r""" Dump to JSON string. """
	output_logs = {
		"operation_name": self.operation_name,
		"operation_output": self.operation_output,
		"log_to_user": self.log_to_user,
		"log_to_system": self.log_to_system,
		"operation_abort": self.operation_abort
	}
	return json.dumps(output_logs)

labridge.callback.base.operation_log.OperationOutputLog.loads(log_str) classmethod

Load from JSON string.

Source code in labridge\callback\base\operation_log.py
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
@classmethod
def loads(
	cls,
	log_str: str,
):
	r""" Load from JSON string. """
	try:
		output_logs = json.loads(log_str)
		operation_name = output_logs["operation_name"]
		operation_output = output_logs["operation_output"]
		log_to_user = output_logs["log_to_user"]
		log_to_system = output_logs["log_to_system"]
		operation_abort = output_logs["operation_abort"]
		return cls(
			operation_name=operation_name,
			operation_output=operation_output,
			log_to_user=log_to_user,
			log_to_system=log_to_system,
			operation_abort=operation_abort,
		)
	except Exception:
		raise ValueError("Invalid operation log string.")