I try python introspection in some weird manner. For example, I have Class LoggerManager which incapsulate pool of specific loggers classes for statistic. I know this is no standard way to do it, but I can't use mixins due to my object of class SpecificLogger is serializable to json in code and I really don't want to pass self.data throw params to init (in this case I need to rewrite many rows code and get problem with garbage in serialization again).
class LoggerManager(object):
def __init__(self):
self.data
self.b = SpecificLogger()
self.c = SpecificLogger2()
...
class SpecificLogger(LoggerGeneral):
def get_data(self):
global data #now I want get object from namespace object of LoggerManager, not really global
do_something_with_data(data)
I want behavior like this code with mixins:
import json
class A(object):
def __init__(self):
self.data = 'something'
def create_pool_of_specific_objects(self):
self.obj = B()
class B(A):
def __init__(self):
A.__init__(self)
def do_something_with_data(self):
print(self.data)
self.data = 'new_data'
print(self.data)
def save(self):
return json.dumps(self.__dict__, ensure_ascii=False)
def hack_save(self):
data_to_dumped = {x:y for x,y in self.__dict__.iteritems() if x != 'obj'}
return json.dumps(data_to_dumped, ensure_ascii=False)
b=B()
b.create_pool_of_specific_objects()
b.do_something_with_data()
b.save() #will raise exception due to we have all stuff from instance of class A there
b.hack_save() #will work but it is very ugly and unusable with runtime modify object of A class
Any suggestion to do it with introspection and not inheritance (access like global statement but namespace one level above not global)?
Aucun commentaire:
Enregistrer un commentaire