HOW TO build object graph or get superclasses list for self.__class__?

D

Dmitry Ponyatov

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
 
J

James Mills

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top