Archive for the 'debugging' Category

14
Jul
09

awesome python alternative to pdb


I hope you know about ipython. It’s a very cool Python interactive shell, with colored output and tab completion ( among other features ). I use it all the time.

For my debugging needs, I’m using pdb. I think most Python coders do this too.

Today, I was thinking how cool it would be if I could drop into ipython instead of pdb, so I asked on stackoverflow.
Here’s a link to the question : SO link.

All you have to do is install the ipdb gem. You can do this using easy_install, like this :


easy_install ipdb

and then you use it in your code, like this :


import ipdb

... some code here
ipdb.set_trace()
... rest of the code

A small tip: if your ipdb shell isn’t coloured, make sure you install the pyreadline library.

Advertisements
01
May
09

rails debugging trick

Lately, I’ve been spending some time in Rails. As you know, no code is 100% bug-free … in most cases, you can figure out errors/bugs by checking the log files, present in your log directory or by checking the traceback.

What do you do when you need to step through your code? I searched for some solutions on the internet, and I asked on stackoverflow. I didn’t receive any answers to my question, but, I remembered a gem I was using in my ruby code: ruby-debug. This gem allows you to stop your script at a given moment and step through the rest of the code.

I’m sure most/all of you know how to install a gem. If not, here’s the command:

gem install ruby-debug

After you install the gem, you include it in your code like this :

require "rubygems"
require "ruby-debug"

You need the require “rubygems” line if you install the gem through ruby’s gem management system. After you included the gem in your source file, you can place “debugger calls” inside your code, like this:

def my_method
    if some_condition?
       debugger
    end
    additional_processing()
    more_processing()
end

When the ruby interpreter reaches the debugger line, you’ll be dropped to a irb-like shell, from where you can step through your code and inspect your variables. A small catch: the shell you get back isn’t a full-featured REPL. If for example, you write the name of a variable and expect it’s value to be printed, it’s not gonna happen. The gem treats it like a command, so, you need to say p name_of_variable or puts name_of_variable .

I suggest you take out those “debugger” lines after you’re done debugging.

If you know of other solutions to this type of problem, please mention them in a comment.

09
Mar
09

cool python debugging trick


I’ve been debugging on some code, and I thought it would be great to be able to decorate functions that would perform something when called with a desired argument. I asked about in stackoverflow, and MizardX provided me with a great solution ( which I’ve hacked a bit ). Here it is :

from functools import wraps
import pdb	

def intercept(target=None,**trigger):
    def decorator(func):
        names = getattr(func,'_names',None)
        if names is None:
            code = func.func_code
            names = code.co_varnames[:code.co_argcount]
        @wraps(func)
        def decorated(*args,**kwargs):
            all_args = kwargs.copy()
            for n,v in zip(names,args):
                all_args[n] = v
            for k,v in trigger.iteritems():
                if k in all_args and all_args[k] == v:
					if not target is None:                    
						return target(all_args)
					else:
						pdb.set_trace()
            return func(*args,**kwargs)
        decorated._names = names
        return decorated
    return decorator

Assuming we have the following function:

def whatever(a,b):
	# do something with a and b

We can do some cool hacking on it :

  • by decorating it like this : @intercept(a=20), the python debugger will kick in and stop the function’s execution if a was passed the value 20
  • by decorating it like this : @intercept(function_here,@a=20), the function will be called and the debugger won’t fire up

Cool, right?




Blog Stats

  • 229,119 hits