Skip to content

Comment on Language for Unix command line utilities?parent

Comments

> Since I cannot tell Python to produce succinct unixy error messages instead of rambling stack traces

`sys.excepthook` is how you can do that.

Without:

     x = {}
     def f():
         print(x['foo'])
     f()
     # ...

      Traceback (most recent call last):
	File "stack.py", line 6, in <module>
	  f()
	File "stack.py", line 4, in f
	  print(x['foo'])
      KeyError: 'foo'
With:
     import sys

     def short_err(exc_type, exc, tb):
         sys.stderr.write("error: tracebacks too long\n")

     sys.excepthook=short_err

     x = {}
     def f():
         print(x['foo'])
     f()

     #...

     error: tracebacks too long
So don't worry about catching exceptions if you're just printing errors.
gnOP

> `sys.excepthook` is how you can do that.

Awesome. Thank you kindly.

> error: tracebacks too long

I like your style.

AboutSource Built by g1lg1l

Hackerly is an independent reader for Hacker News, built on the public HN API. Not affiliated with Y Combinator.