Doug wrote:
> Two more basic questions I'm hoping you can help with:
> 
> 1. My workstyle so far is to edit my .sage file in emacs and then load
> it into sage on the command line.  Sometimes I want my program to stop
> in the middle so I can more closely examine/verify what it's doing.
> I've been inserting a line that just says "stop" to do this and it
> causes sage/Python to stop with an error message and a short stack
> trace.  This works, but it's kind of messy.  "raise Exception
> ('spam','eggs')" does pretty much the same thing.  Is there a way to
> tell sage/Python to stop running without raising an error?
> 


One thing you can do is use pdb, the python debugger (example from 
http://www.electricmonk.nl/log/2008/06/25/breakpoint-induced-python-debugging-with-ipython/).

from IPython.Debugger import Tracer; debug_here = Tracer()

def ham():
        x = 5
        debug_here()
        raise NotImplementedError('Use the source, luke!')

ham()

will put you into the python debugging loop, where you can examine 
variables, step through the code, etc.  Just put these two lines 
somewhere in your code where you want it to stop.  See also 
http://www.nabble.com/debugging-in-ipython-td20047930.html for another 
way to set a breakpoint.

Another way to enter this is to just turn pdb on in Sage (actually ipython):

sage: %pdb on

Now any errors will drop you into the debugger.


And thirdly, you can just wait until an error shows up and type

sage: %debug

to examine the variable values, etc. when that error was thrown.


Fourthly, you can use pdb to run the function directly:

import pdb
pdb.run('Networks.FindPathLengthsFromNode(g, 0)')


> 2. Is there a way to "reset" sage/Python from the command-line as if I
> was restarting?  I can't even find a command that will clear all my
> global variables, although that might be enough.


Check out the (oddly enough named :) reset function:

sage: reset?

from the reset docs:

             sage: x = 5
             sage: reset()
             sage: x
             x


Thanks,

Jason


--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to sage-support@googlegroups.com
To unsubscribe from this group, send email to 
sage-support-unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/sage-support
URLs: http://www.sagemath.org
-~----------~----~----~----~------~----~------~--~---

Reply via email to