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

Discussion in 'Python' started by Dmitry Ponyatov, Apr 21, 2010.

  1. 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
     
    Dmitry Ponyatov, Apr 21, 2010
    #1
    1. Advertising

  2. Dmitry Ponyatov

    James Mills Guest

    Re: HOW TO build object graph or get superclasses list forself.__class__ ?

    On Wed, Apr 21, 2010 at 3:35 PM, Dmitry Ponyatov <> 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
     
    James Mills, Apr 21, 2010
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ralf W. Grosse-Kunstleve
    Replies:
    16
    Views:
    586
    Lonnie Princehouse
    Jul 11, 2005
  2. Ralf W. Grosse-Kunstleve
    Replies:
    18
    Views:
    599
    Bengt Richter
    Jul 11, 2005
  3. Paul McGuire

    Assigning to self.__class__

    Paul McGuire, Jan 26, 2006, in forum: Python
    Replies:
    4
    Views:
    457
    Terry Reedy
    Jan 26, 2006
  4. Gabriel Rossetti

    ClassName.attribute vs self.__class__.attribute

    Gabriel Rossetti, Jun 5, 2008, in forum: Python
    Replies:
    14
    Views:
    712
    Terry Reedy
    Jun 13, 2008
  5. Emilio Mayorga
    Replies:
    6
    Views:
    341
    Martien Verbruggen
    Oct 8, 2003
Loading...

Share This Page