From d92bd02f5a0655e2cb017486bda549a65f61896e Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Fri, 20 Dec 2013 16:56:03 -0800 Subject: [PATCH] Add PGPPacket._reverse for inverse dictionary lookup Mapping human-readable values to their binary counterparts for serialization. --- gpg-migrate.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/gpg-migrate.py b/gpg-migrate.py index 3041b16..9453494 100755 --- a/gpg-migrate.py +++ b/gpg-migrate.py @@ -240,6 +240,15 @@ class PGPPacket (dict): type = self['type'] return self._clean_type_regex.sub('_', type) + @staticmethod + def _reverse(dict, value): + """Reverse lookups in dictionaries + + >>> PGPPacket._reverse(PGPPacket._packet_types, 'public-key packet') + 6 + """ + return [k for k,v in dict.items() if v == value][0] + def __str__(self): method_name = '_str_{}'.format(self._clean_type()) method = getattr(self, method_name, None) -- 2.26.2