Get signal names from numbers in Python Get signal names from numbers in Python python python

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


There is none, but if you don't mind a little hack, you can generate it like this:

import signaldict((k, v) for v, k in reversed(sorted(signal.__dict__.items()))     if v.startswith('SIG') and not v.startswith('SIG_'))


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]