Insane crazy question - printing commands

Guilherme Polo ggpolo at gmail.com
Tue Nov 6 12:31:13 EST 2007


2007/11/6, Donn Ingle <donn.ingle at gmail.com>:
> Hi,
> I'm doing something odd with pycairo and friends and I want to see what
> commands are coming out of my objects.
>
> Here's some code:
>
> class Box:
>  def draw()
>   self.context.set_source_rgb(1, 0, 0)
>   self.context.rectangle(0, 00, 50, 50)
>   self.context.fill()
>
> Box.draw() draws a red box, all fine. But, I *also* want it to output the
> actual commands within the draw def to the console (or a file).
>
> At the moment I am doing this:
> class Box:
>  def draw()
>   self.context.set_source_rgb(1, 0, 0)
>   self.context.rectangle(0, 00, 50, 50)
>   self.context.fill()
>   print """
>   self.context.set_source_rgb(1, 0, 0)
>   self.context.rectangle(0, 00, 50, 50)
>   self.context.fill()
>   """
> Do you see the form? Is there some <voodoo magic> python introspection way I
> can perform that automagically without having to use the print statement?
>
> Something like:
> class Box:
>  def draw()
>   self.context.set_source_rgb(1, 0, 0)
>   self.context.rectangle(0, 00, 50, 50)
>   self.context.fill()
>  def dump():
>   <mystical mindblowing stuff involving deep magic>


You could use inspect, something like this:

import inspect

class Box:
    def draw(self):
        print "hi"
        return 3

x = Box()
print inspect.getsource(x.draw)

-- 
-- Guilherme H. Polo Goncalves



More information about the Python-list mailing list