1

I am looking at this tutorial in twisted python. https://github.com/jdavisp3/twisted-intro/blob/master/twisted-client-3/get-poetry.py

def get_poetry(host, port, callback):
    """
    Download a poem from the given host and port and invoke

      callback(poem)

    when the poem is complete.
    """
    from twisted.internet import reactor
    factory = PoetryClientFactory(callback)#I am interested in checking the instances alive here
    reactor.connectTCP(host, port, factory)


def poetry_main():
addresses = parse_args()

from twisted.internet import reactor

poems = []

def got_poem(poem):
    poems.append(poem)
    if len(poems) == len(addresses):
        reactor.stop()

for address in addresses:
    host, port = address
    get_poetry(host, port, got_poem)

reactor.run()

for poem in poems:
    print poem


if __name__ == '__main__':
    poetry_main()

I have never really debugged python before.

I wanted to see which classes's instances are alive before the reactor.stop fires.

I was checking this Printing all instances of a class

with this code

import gc
for obj in gc.get_objects():

How can I selectively view the top most information and then further inherited data and so on?

From a twisted point of view, I want see which factory instances are currently active and how is it related to the protocols

Community
  • 1
  • 1

1 Answers1

1

But, if you really just want to poke things to get a feel for how to debug Python, check out 'dir(obj)', which will list all the properties and methods of an object.

class Blah(object):
    pass

b = Blah()

for x in dir(b):
    try:
        print getattr(b,x,False)
    except Exception, e:
        print x,e

Will yield:

<class '__main__.Blah'>
<method-wrapper '__delattr__' of Blah object at 0x1028ba490>
{}
None
<built-in method __format__ of Blah object at 0x1028ba490>
<method-wrapper '__getattribute__' of Blah object at 0x1028ba490>
<method-wrapper '__hash__' of Blah object at 0x1028ba490>
<method-wrapper '__init__' of Blah object at 0x1028ba490>
__main__
<built-in method __new__ of type object at 0x10276a4e0>
<built-in method __reduce__ of Blah object at 0x1028ba490>
<built-in method __reduce_ex__ of Blah object at 0x1028ba490>
<method-wrapper '__repr__' of Blah object at 0x1028ba490>
<method-wrapper '__setattr__' of Blah object at 0x1028ba490>
<built-in method __sizeof__ of Blah object at 0x1028ba490>
<method-wrapper '__str__' of Blah object at 0x1028ba490>
<built-in method __subclasshook__ of type object at 0x7fd522c6e490>

Now, your mileage may vary with stuff like objc - since it's a thin Python wrapper around making shared library calls. They won't have docstrings, or in some cases respond to 'dir' if function lookup is lazy-lookup against shared libraries. But, you never know.

Most of the time when it came to the objc stuff, I just dug around in their source code to figure out how they did things when the normal methods of digging up dirt didn't work.

Speaking of normal methods:

A neat feature with Twisted, you can also serve a telnet or SSH accessible interactive Python shell that can actually poke and prod things 'live'. Check here for details on TwistedConch.

Or..

Another trick is to add a 'del(self)', function to your objects that prints something out as the object gets cleaned up by the garbage collector (when it's deleted / out of scope)

Or..

You could also play with pdb, or if you like ncurses pudb is awesome. Check out this question for a couple nifty tricks for using pdb. starting-python-debugger-automatically-on-error

And, if worse comes to worse - you can always use help(object).

Those are pretty much the debugging methods that get me through the day. If anyone else has some clever ideas don't be shy.

Community
  • 1
  • 1
synthesizerpatel
  • 27,321
  • 5
  • 74
  • 91
  • thanks...How can I list all the current factories and its protocols associated with a reactor? –  Apr 28 '13 at 10:17
  • Unsure about backtracking whats registered with the reactor - in that cases I'd strongly suggest setting up TwistedConch so you can just SSH into an interactive Python CLI running inside your app. You can run 'dir()', by itself to see the top level 'global' objects - and start drilling down from there. – synthesizerpatel Apr 28 '13 at 10:29