Add PGPPacket._reverse for inverse dictionary lookup
authorW. Trevor King <wking@tremily.us>
Sat, 21 Dec 2013 00:56:03 +0000 (16:56 -0800)
committerW. Trevor King <wking@tremily.us>
Mon, 23 Dec 2013 02:32:14 +0000 (18:32 -0800)
Mapping human-readable values to their binary counterparts for
serialization.

gpg-migrate.py

index 3041b16ca55f325addd7aec311f6dc147c82bb04..9453494a9dafbcb0f03ab5f401d4fbf19aac71ac 100755 (executable)
@@ -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)