3 ms·
I like the basic script template that the author provides. However, when I start development of new code I always use a script that starts off with traceback a
by gorb314 8y ago
I like the basic script template that the author provides.
However, when I start development of new code I always use a script that starts off with traceback and pdb, something like this:
#!/usr/bin/env python
import traceback
import pdb
import sys
def main():
# some WIP code that maybe raises an exception
raise BaseException("oh no, exception!")
return 0
if __name__ == "__main__":
try:
ret = main()
except:
traceback.print_exc()
pdb.post_mortem()
sys.exit(ret)
This means that whenever an uncaught exception gets raised, I immediately get told what happened (full backtrace) plus I get dumped into the debugger, from where I can inspect why this happened. I liberally sprinkle assert()s through my code, so this gives me a good edit-run-debug cycle to work with.
- smittywerben 8y agoThis is pretty smart. When I'm making one-off scripts I use the interactive flag python -i script.py I've programmed Python for years and never knew this. I'm not sure how well this handles errors. Tab complete works, only thing that is missing is needing to wrap help(<func>) to see signatures. I'm enjoying the standard lib interpreter.
- ont 8y agoI use ipython for similar task. It has wrapper for 'help' function ('?' symbol after function name), tab completion and syntax highlighting. Also it has special %hist command for easy copy-paste code.
- smittywerben 8y agoI like IPython unless working with large arrays, because of weird memory issues I experienced (maybe they're fixed?). I miss the %run, !<cd, ls, pip>, %timeit magic and the IPython.terminal.debugger.set_trace(). If you're on linux it's not _that_ bad without it since the shell sort of acts like the notebook (with an equally horrible markup language). The help() and tab-complete and _ __ ___ for last 3 values works, time python <file>.py can benchmark, -i or pdb.set_trace does the debugging. It doesn't keep a persistent history though so the workflow is sort of different. So I use both, particularly for plots and images knowing IPython is very helpful for working with the notebooks.