Skip to content

Instantly share code, notes, and snippets.

@luciengaitskell
Last active February 18, 2021 20:44
Show Gist options
  • Save luciengaitskell/27a3b7dbf98f0c7dcbc609824cb89721 to your computer and use it in GitHub Desktop.
Save luciengaitskell/27a3b7dbf98f0c7dcbc609824cb89721 to your computer and use it in GitHub Desktop.
DGNet Standards Compliance Utility

Standards Compliance Utility

MethodExecution

Create instance for every method body.

Backwards compatible with dictionary return data.

Check example.py for usage example.

def exampleFunction(self):
""" Show how MethodExecution is supposed to work. """
mexec = MethodExecution(func=ClassName.exampleFunction)
mexec.success = True # Default success status
# Example call 1
cmd = mexec.call(anotherFunction())
# Example call 2:
cmd = mexec.call(aThirdFunction())
return mexec
exampleFunction()
# {'sucess': True, 'return_msg': "ClassName:exampleFunction: "}
# {'sucess': False, 'return_msg': "ClassName:exampleFunction: some sort of error"}
""" DGNet return standards utility
Originally authored by Lucien Gaitskell
"""
class MethodExecution(dict):
key_success = 'success'
key_return_msg = 'return_msg'
def __init__(self, func=None, name=None, **kwargs):
super().__init__(**kwargs)
if name is None:
name = "{}:{}".format(func.__class__.__name__, func.__name__)
self.return_msg = "{}: ".format(name)
self.debug_data = []
self.call_result = {}
self.success = None
def return_standard(self, **other):
""" Generate standard return call, with extra content if required. """
print("`return_standard` DEPRECATED, just return object directly and use `add` to include other data")
if self.success is None:
raise ValueError("Need to set `success` status")
return_data = {self.__class__.key_success: self.success, self.__class__.key_return_msg: self.return_msg}
return_data.update(other)
return return_data
def call(self, func_result, success_log=None, failure_log=None):
""" Log function call to `debug_data` and write to call_result.
If failure, then set success flag to false.
return: Input value given, which is appended to debug_data
"""
self.debug_data.append(func_result)
self.call_result = func_result
if isinstance(func_result, dict):
if self.success_status(func_result.get(self.key_success, None)):
if success_log:
self.log(success_log)
elif failure_log:
self.log(failure_log)
return func_result
def log(self, msg: str):
""" Log message to return_msg. """
self.return_msg += "{}; ".format(msg)
def success_status(self, success: bool):
""" Update success status.
Will only set success to false if failure, otherwise not changed.
return: given success value
"""
if success is False:
self.success = False
return success
def add(self, **data):
""" Add additional data to output """
self.update(data)
@property
def return_msg(self):
return self[self.key_return_msg]
@return_msg.setter
def return_msg(self, value):
self[self.key_return_msg] = value
@property
def success(self):
return self[self.key_success]
@success.setter
def success(self, value):
self[self.key_success] = value
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment