easy way to dump a class instance?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mark Harrison

    #1

    easy way to dump a class instance?

    Is there a way to automatically print all the instance
    data in a class? This is for debugging, I would like
    to do something like dump(self) to snapshot the state
    of the object.

    Many TIA!
    Mark

    --
    Mark Harrison
    Pixar Animation Studios
  • bruno at modulix

    #2
    Re: easy way to dump a class instance?

    Mark Harrison wrote:[color=blue]
    > Is there a way to automatically print all the instance
    > data in a class? This is for debugging, I would like
    > to do something like dump(self) to snapshot the state
    > of the object.[/color]

    def dump(obj):
    buf = ['%r %s :' % (obj, str(obj)]
    for name in dir(obj):
    attr = getattr(obj, name)
    if not callable(attr):
    buf.append["- %s : %s" % (name, str(attr))]
    return '\n'.join(buf)

    Not tested...

    --
    bruno desthuilliers
    python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
    p in 'onurb@xiludom. gro'.split('@')])"

    Comment

    • Carl J. Van Arsdall

      #3
      Re: easy way to dump a class instance?

      Mark Harrison wrote:[color=blue]
      > Is there a way to automatically print all the instance
      > data in a class? This is for debugging, I would like
      > to do something like dump(self) to snapshot the state
      > of the object.
      >
      >
      >[/color]
      Would something like cPickle work? That allows you to dump objects into
      files as they are and read them again as they were. Its fairly easy to
      use and might work well for you.

      HTH

      -carl


      --

      Carl J. Van Arsdall
      cvanarsdall@mvi sta.com
      Build and Release
      MontaVista Software

      Comment

      • Kamilche

        #4
        Re: easy way to dump a class instance?


        Mark Harrison wrote:[color=blue]
        > Is there a way to automatically print all the instance
        > data in a class?[/color]

        print self.__dict__

        Comment

        • wittempj@hotmail.com

          #5
          Re: easy way to dump a class instance?

          Some Googling yielded this recipe
          http://aspn.activestate.com/ASPN/Coo...7951/index_txt,
          a little test shows promising results of the described recipe. BTW: the
          Python Cookbook is always a good place to browse with questions other
          people probably had as well!

          Comment

          • Bruno Desthuilliers

            #6
            Re: easy way to dump a class instance?

            Kamilche a écrit :[color=blue]
            > Mark Harrison wrote:
            >[color=green]
            >>Is there a way to automatically print all the instance
            >>data in a class?[/color]
            >
            >
            > print self.__dict__
            >[/color]
            Yeps - if you don't mind missing class attributes and data descriptors.

            Comment

            Working...