Connect other Accounts

Stopping a script for debugging

Dylan Gregersen brings us another nice code snippet which might be especially welcome for IDL users who miss the STOP procedure:

Here's a snippet I'm using often while writing scripts, executing via run -i <file> or execfile(<file>), and I want to quickly truncate the code so that the remaining part of the script doesn't run. Then I can access the variables and do other interactive scripting execution.

Of course the above can be achieved by injecting any simple code error, e.g. stop, here = 0 or the ever-popular 1 / 0, but that wouldn't be so nice.

And it should be mentioned that this is not a substitute for pdb which is an actual debug tool and blows IDL's `stop` out of the water. But this stop() is good for quickly scripting and testing portions of your scripts. As an example:

print("testing script")
stop()
print("lots of other code you don't want to run right now")code here

One cool thing is that once you've stopped like this, in IPython you can issue the %debug magic command and re-enter the context of your script via IPython's enhanced debugger. This lets you walk back up the stack trace and examine variables.

Finally it's worth mentioning that I have this in my ~/.ipython/profile_default/startup/misc.py which means it's loaded every time on startup (like a bashrc or tcshrc) and I can just use it without importing, copying into script, etc.

Tags

To suggest a new feed to our aggregator, fill in the Contact Form on the right !

Contribute to AstroPython

Contributions and feedback from the community are strongly encouraged.If you find a bug or wish to suggest new features for the site , file an issue on Github. If you simply have a question or comment for the AstroPython team please fill in this form and we'll get back to you as soon as possible.
If you have a new resource, question, or any other content that you'd like to post to the web site then likewise fill in the creation form. If you wish to have elevated priveleges , you can use the form to contact us.

Feedback | Contribute | Contact

We would love to hear from you but please login to access the Contact Form !

About Us : Team AstroPython

Tom Aldcroft is a Science
Operations Team member and Flight Director for the Chandra X-ray Observatory and works at the Smithsonian Astrophysical
Observatory. He uses Python for Chandra
spacecraft operations analysis as well as research on several X-ray
survey projects. He is the author of the
asciitable,
cosmocalc, and
deproject
packages. He is also
interested in Python for the web and writes Django and Google App Engine applications
for presenting large multi-wavelength survey datasets. He has been the primary mentor for AstroPython

Thomas Robitaille is a Spitzer
postdoctoral fellow at the Harvard-Smithsonian Center for Astrophysics. He works on
studying Galactic star formation, mostly through mid-infrared to mm
wavelengths. This includes modeling multi-wavelength observations of young
stars and studying the global properties of star formation in the
Milky-Way. In his spare time he is a keen Python programmer and is one of
the co-developers of the APLpy and
ATpy packages. He also developed
the IDLSave package.He has been the secondary mentor for the project.

Aman Jhunjhunwala is an undergraduate Computer Science student with a passion for Big Data,Machine Learning and Web technologies. He has been the primary developer of this site as a part of Google Summer of Code-2015 programme under the AstroPy project.