n611x007
n611x007

Reputation: 9272

Python 2.x get unix signal name for signalnum (code)?

Is there a more pythonic way to get the signal name from a signal code? My current approach:

import signal
dict((getattr(signal,na),na) for na in dir(signal) if na[:3]=='SIG')

I looked the 2.7 signal docs without success for such a map. If it has one it eluded me. Is there a better approach?

Upvotes: 1

Views: 456

Answers (1)

dano
dano

Reputation: 94891

You can use a dict comprehension, which I think looks a little nicer:

{getattr(signal, n) : n for n in dir(signal) if n.startswith('SIG')}

Also, your original comprehension is over-complicated. You've added an extra for n in when you don't need one. You could just write it like this:

dict((getattr(signal,n),n) for n in dir(signal) if n[:3] == 'SIG')

Upvotes: 1

Related Questions