Get signal names from numbers in Python
With the addition of the signal.Signals
enum
in Python 3.5 this is now as easy as:
>>> import signal>>> signal.SIGINT.name'SIGINT'>>> signal.SIGINT.value2>>> signal.Signals(2).name'SIGINT'>>> signal.Signals['SIGINT'].value2
The Python Standard Library By Example shows this function in the chapter on signals:
SIGNALS_TO_NAMES_DICT = dict((getattr(signal, n), n) \ for n in dir(signal) if n.startswith('SIG') and '_' not in n )
You can then use it like this:
print "Terminated by signal %s" % SIGNALS_TO_NAMES_DICT[signal_number]