数据的封装思想--单例模式的应用

. 获取日志的堆栈信息

'''
应用了python提供的traceback模块,traceback.format_exc
import traceback
def process(self,hostname,ssh_func):
"""
执行命令,去获取主板
:return:
"""
info = {'status': True, 'data': None, 'error': None}
try:
content = ssh_func(hostname, 'sudo dmidecode -t1')
data = self.parse(content)
info['data'] = data
except Exception as e:
msg = traceback.format_exc()
logger.log(msg)
info['status'] = False
info['error'] = msg
return info
'''

数据的封装思想

'''
将需要的信息封装成一个字典
class BaseResponse(object):
def init(self):
self.status = True
self.data = None
self.error = None

@property
def dict(self):
return self.dict

def process():
info = BaseResponse()
try:
info.status = True
info.data = "sfdsdf"
except Exception:
pass
return info.dict

result = process()
print(result) # {'status': True, 'data': 'sfdsdf', 'error': None}

'''

posted @ 2020-04-04 19:56  阿浪阿浪  阅读(137)  评论(0)    收藏  举报