|
|
|
|
|
by gorb314
2697 days 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. |
|
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.