Metaclass Recipe: Automatic Debugger Methods

Step 1

For now, let's not worry about invoking the debugger. We'll start by printing a traceback whenever an error occurs. For that, we'll use a decorator function called get_debugger_func, which is already defined for you.

Your task is to define the __debugger__ metaclass.

import inspect, traceback, pdb

def get_debugger_func(func):
    def new_func(*args):
        try:
            return func(*args)
        except:
            traceback.print_exc()

    return new_func

class __debugger__(type):
    """Define this class"""

class Example:
    __metaclass__ = __debugger__

    def divide(self, v):
        # exception if v == 0
        result = 100 / v
        return result

    def interpolate(self, *args):
        # exception if len(args) != 2
        result = "%s is %s!" % args
        return result

if __name__ == '__main__':
    e = Example()

    print e.divide(0)

    print '-'*80

    print e.interpolate('Feihong', 'very', 'cool')

Expected output:

Traceback (most recent call last):
  File "D:\Projects\PyCon 2008 Talks\framework-tutorial\src\metaclass\debugger\solutions\debugger1.py", line 9, in new_func
    return func(*args)
  File "D:\Projects\PyCon 2008 Talks\framework-tutorial\src\metaclass\debugger\solutions\debugger1.py", line 29, in divide
    result = 100 / v
ZeroDivisionError: integer division or modulo by zero
None
--------------------------------------------------------------------------------
Traceback (most recent call last):
  File "D:\Projects\PyCon 2008 Talks\framework-tutorial\src\metaclass\debugger\solutions\debugger1.py", line 9, in new_func
    return func(*args)
  File "D:\Projects\PyCon 2008 Talks\framework-tutorial\src\metaclass\debugger\solutions\debugger1.py", line 34, in interpolate
    result = "%s is %s!" % args
TypeError: not all arguments converted during string formatting
None
>>>

Hints:

  1. Show hint
  2. Show hint
  3. Show hint
  4. Show hint
  5. Show hint

Solution: debugger1.py

Go to next step