Commits

Mikhail Korobov  committed 5ffa4ac

cache Enum values (it turns out this is important sometimes)

  • Participants
  • Parent commits d6a88a1

Comments (0)

Files changed (1)

File src/psd_tools/constants.py

 class Enum(object):
 
     _attributes_cache = None
+    _values_dict_cache = None
 
     @classmethod
     def _attributes(cls):
         return cls._attributes_cache
 
     @classmethod
+    def _values_dict(cls):
+        if cls._values_dict_cache is None:
+            cls._values_dict_cache = dict([
+                (getattr(cls, name), name)
+                for name in cls._attributes()
+            ])
+        return cls._values_dict_cache
+
+    @classmethod
     def is_known(cls, value):
-        for name in cls._attributes():
-            if getattr(cls, name) == value:
-                return True
-        return False
+        return value in cls._values_dict()
 
     @classmethod
     def name_of(cls, value):
-        for name in cls._attributes():
-            if getattr(cls, name) == value:
-                return name
-        return "<unknown>"
+        return cls._values_dict().get(value, "<unknown>")
 
 
 class ColorMode(Enum):