pythonクラスオブジェクトエージェントの貫通
3789 ワード
pythonクラスオブジェクトエージェントの貫通
# coding:utf-8
import os
import logging
#
#
# : redis , redis
# :
# ,
#
# , ,
# : , abc_agent_
class AgentContainer(object):
# ,
def __init__(self, current_object):
self.abc_agent_current_object = current_object
self.abc_agent_current_func = ""
self.abc_agent_error = ""
self.abc_agent_debug = True
# ,
def abc_agent_container(self, *args, **kargs):
result = None
self.abc_agent_before_container(*args, **kargs)
func = getattr(self.abc_agent_current_object, self.abc_agent_current_func)
logging.error("func: %s" % func)
try:
result = func(*args, **kargs)
except Exception, e:
self.abc_agent_object_error(e)
self.abc_agent_after_container(*args, **kargs)
return result
#
def abc_agent_before_container(self, *args, **kargs):
self.abc_agent_log("before container")
#
def abc_agent_after_container(self, *args, **kargs):
self.abc_agent_log("after container")
#
def abc_agent_object_error(self, except_info):
self.abc_agent_log("object error")
self.abc_agent_error = except_info
return except_info
#
def __getattr__(self, func_name):
logging.error("INTO __GETATTR__")
self.abc_agent_log("container function is:",func_name)
self.abc_agent_current_func = func_name
func = getattr(self.abc_agent_current_object, func_name)
if not callable(func):
return func
else:
return self.abc_agent_container
def __setattr__(self, attr_name, attr_value):
logging.error("INTO __SETATTR__")
pre_str = "abc_agent_"
if attr_name.startswith(pre_str):
object.__setattr__(self, attr_name, attr_value)
else:
self.abc_agent_before_container(attr_name, attr_value)
setattr(self.abc_agent_current_object, attr_name, attr_value)
self.abc_agent_after_container(attr_name, attr_value)
def __get__(self, instance, owner):
pass
def abc_agent_log(self, *args):
if self.abc_agent_debug:
print "".join(args)
# :
# : redis, redis , redis
#
class Redis(object):
def hget(self, *args,**kargs):
return "show is ok"
def hset(self, *args):
logging.error(args)
# redis hset
raise IOError("ioerror")
def incr(self, *args, **kwargs):
logging.error("into incr")
logging.error(args)
logging.error(kwargs)
return "success"
def __init__(self):
self.name = "polo"
# redis
# redis redis ,
class HRedis(AgentContainer):
def __init__(self):
redis = Redis()
AgentContainer.__init__(self, redis)
# ,
self.abc_agent_debug = True
# error , redis io , io : redis
def abc_agent_object_error(self, except_info):
self.abc_agent_current_object = Redis()
# raise except_info
return "error"
redis = HRedis()
# print redis.hget("ssssss")
logging.error('############')
redis.hset('a', 'b', 123)
logging.error("============")
logging.error(redis.incr(1, 2, 3, 4, arg1='arg'))