Sort of an odd way to debug...

X

xkenneth

All,

Sorry for the vague topic, but I really didn't know how to
describe what I want to do. I'd like to almost do a traceback of my
code for debugging and I thought this would be a really cool way to do
it if possible.

What I'd like to do, is define a base class. This base class would
have a function, that gets called every time another function is
called (regardless of whether in the base class or a derived class),
and prints the doc string of each function whenever it's called. I'd
like to be able to do this without explicitly specifying the function
inside all of the other functions of a base class or derived class.

Here's what I think it would look like:

class Base:
__init__(self,debug=False):
if debug:
self.debug = debug

def functionThatAlwaysGetsCalled(self):
print self.__docstring__

class Derived(Base):
"""This function prints something"""
def printSometing(something)
#ghost function get's called here
print something

Output would be:
This function prints something
something

Thanks for any help!
 
C

Chris Mellon

All,

Sorry for the vague topic, but I really didn't know how to
describe what I want to do. I'd like to almost do a traceback of my
code for debugging and I thought this would be a really cool way to do
it if possible.

What I'd like to do, is define a base class. This base class would
have a function, that gets called every time another function is
called (regardless of whether in the base class or a derived class),
and prints the doc string of each function whenever it's called. I'd
like to be able to do this without explicitly specifying the function
inside all of the other functions of a base class or derived class.

Here's what I think it would look like:

class Base:
__init__(self,debug=False):
if debug:
self.debug = debug

def functionThatAlwaysGetsCalled(self):
print self.__docstring__

class Derived(Base):
"""This function prints something"""
def printSometing(something)
#ghost function get's called here
print something

Output would be:
This function prints something
something

Thanks for any help!

This approach won't work, because you need cooperation from child
classes to trigger your printing. A trace function (see sys.settrace)
is probably more useful.
 
A

Alex Martelli

xkenneth said:
What I'd like to do, is define a base class. This base class would
have a function, that gets called every time another function is
called (regardless of whether in the base class or a derived class),
and prints the doc string of each function whenever it's called. I'd
like to be able to do this without explicitly specifying the function
inside all of the other functions of a base class or derived class.

So you need to write a metaclass that wraps every function attribute of
the class into a wrapper performing such prints as you desire. The
metaclass will be inherited by subclasses (unless metaclass conflicts
intervene in multiple-inheritance situation).

You don't appear to need the printing-wrapper to be a method, and it's
simpler to have it be a freestanding function, such as:

import functools
def make_printing_wrapper(f):
@functools.wraps(f)
def wrapper(*a, **k):
print f.__doc__
return f(*a, **k)
return wrapper

Now, the metaclass could be, say:

import inspect
class MetaWrapFunctions(type):
def __init__(cls, name, bases, attrs):
for k, f in attrs.iteritems():
if inspect.isfunction(f):
attrs[k] = make_printing_wrapper(f)
type.__init__(cls, name, bases, attrs)

and the base class:

class Base:
__metaclass__ = MetaWrapFunctions

Now, the code:
class Derived(Base):
"""This function prints something"""
def printSometing(something)
#ghost function get's called here
print something

Output would be:
This function prints something
something

Should behave as you described. I have not tested the code I'm
suggesting (so there might be some errors of detail) but the general
idea should work.


Alex
 
G

Gabriel Genellina

xkenneth said:
What I'd like to do, is define a base class. This base class would
have a function, that gets called every time another function is
called (regardless of whether in the base class or a derived class),
and prints the doc string of each function whenever it's called. I'd
like to be able to do this without explicitly specifying the function
inside all of the other functions of a base class or derived class.

So you need to write a metaclass that wraps every function attribute of
the class into a wrapper performing such prints as you desire. [...]

import inspect
class MetaWrapFunctions(type):
def __init__(cls, name, bases, attrs):
for k, f in attrs.iteritems():
if inspect.isfunction(f):
attrs[k] = make_printing_wrapper(f)
type.__init__(cls, name, bases, attrs)

Should behave as you described. I have not tested the code I'm
suggesting (so there might be some errors of detail) but the general
idea should work.

After playing a bit with the code I found a problem, __init__ is too late,
changes to `attrs` are not reflected in the class namespace. Using __new__
instead is OK. The metaclass should be then:

import inspect
class MetaWrapFunctions(type):
def __new__(cls, name, bases, attrs):
for k, f in attrs.iteritems():
if inspect.isfunction(f):
attrs[k] = make_printing_wrapper(f)
return super(MetaWrapFunctions, cls).__new__(cls, name, bases,
attrs)

[another way would be to keep __init__ but using setattr(cls, k,
wrapper(...)]
 

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

Forum statistics

Threads
473,780
Messages
2,569,607
Members
45,240
Latest member
pashute

Latest Threads

Top