1. Armin Ronacher
  2. werkzeug-main


Armin Ronacher  committed f0aacf2

Micro optimizations and refactoring

  • Participants
  • Parent commits 5a21059
  • Branches default

Comments (0)

Files changed (2)

File werkzeug/datastructures.py

View file
  • Ignore whitespace
         return cls(_list=headerlist)
-    def __getitem__(self, key, _index_operation=True):
-        if _index_operation:
+    def __getitem__(self, key, _get_mode=False):
+        if not _get_mode:
             if isinstance(key, (int, long)):
                 return self._list[key]
             elif isinstance(key, slice):
         for k, v in self._list:
             if k.lower() == ikey:
                 return v
+        # micro optimization: if we are in get mode we will catch that
+        # exception one stack level down so we can raise a standard
+        # key error instead of our special one.
+        if _get_mode:
+            raise KeyError()
         raise self.KeyError(key)
     def __eq__(self, other):
                      by this callable the default value is returned.
-            rv = self.__getitem__(key, _index_operation=False)
+            rv = self.__getitem__(key, _get_mode=True)
         except KeyError:
             return default
         if type is None:
     def __contains__(self, key):
         """Check if a key is present."""
-            self.__getitem__(key, _index_operation=False)
+            self.__getitem__(key, _get_mode=True)
         except KeyError:
             return False
         return True
     def __eq__(self, other):
         return self.environ is other.environ
-    def __getitem__(self, key, _index_operation=False):
-        # _index_operation is a no-op for this class as there is no index but
+    def __getitem__(self, key, _get_mode=False):
+        # _get_mode is a no-op for this class as there is no index but
         # used because get() calls it.
         key = key.upper().replace('-', '_')
         if key in ('CONTENT_TYPE', 'CONTENT_LENGTH'):

File werkzeug/routing.py

View file
  • Ignore whitespace
             self.strict_slashes = map.strict_slashes
         if self.subdomain is None:
             self.subdomain = map.default_subdomain
+        self.compile()
+    def compile(self):
+        """Compiles the regular expression and stores it."""
+        assert self.map is not None, 'rule not bound'
         rule = self.subdomain + '|' + (self.is_leaf and self.rule
                                        or self.rule.rstrip('/'))
                 self._trace.append((False, variable))
-                convobj = get_converter(map, converter, arguments)
+                convobj = get_converter(self.map, converter, arguments)
                 regex_parts.append('(?P<%s>%s)' % (variable, convobj.regex))
                 self._converters[variable] = convobj
                 self._trace.append((True, variable))