On Wed, Apr 21, 2010 at 3:35 PM, Dmitry Ponyatov <fo...@km.ru> wrote: > Hello > > Help please with such problem: > > I need to build program object graph (data structure) with additional > parameters for nodes and edges: > > include nxgraph # data structure module allowes any py objects for > node/edge id > # (nxgraph ignores 2+ node/edge adding thus no checking need at node/ > edge adding) > > OBJ_TREE = nxgraph.DiGraph() # directed graph > > OBJ_TREE.add_node(object,color='red') # root object > > class A(object): > def __init__(self): > object.__init__(self) # (1) maybe map(lambda > s:s.__init__(self), self.__super__) better if I have __super__ > OBJ_TREE.add_node(self.__class__,color='red') # (2) add self > class as new node > for SUP in self.__super__: > OBJ_TREE.add_edge(SUP,self.__class__,color='red') # (3) > add super class > # to self class red arrow (directed > edge) > OBJ_TREE.add_node(self,color='blue') # (4) add self object > instance node > OBJ_TREE.add_edge(self.__class__,self,color='blue') # (5) add > object producing from class edge > OBJ_TREE.plot(sys.argv[0]+'.objtree.png') # dump obj tree as > picture to .png file > > class B(A): > pass > > class C(A): > pass > > Using Python 2.5 I can't realize line (3) in A class, but lines 2) (4) > (5) works well > How can I realize some function get_super(self) giving list of > superclasses for self.__class__ ? > Preferable using py2.5 > > How days I must reimplement __init__ by dumb copy from parent class to > child class like > > class B(A): > def __init__(self): > A.__init__(self) > OBJ_TREE.add_node(A,B) ; OBJ_TREE.plot('OBJ_TREE.png') > > class C(A): > def __init__(self): > A.__init__(self) > OBJ_TREE.add_node(A,C) ; OBJ_TREE.plot('OBJ_TREE.png') > > class D(B,C): > def __init__(self): > B.__init__(self) ; OBJ_TREE.add_node(B,D) > C.__init__(self) ; OBJ_TREE.add_node(C,D) > OBJ_TREE.plot('OBJ_TREE.png') > > This is not good -- a lot of dumb code with chance to make mistakes
Why not make things simple and just write a function that computes the edges of a class tree ? Something like this: http://codepad.org/xWKDlS52 cheers James -- http://mail.python.org/mailman/listinfo/python-list