VMM/common: added a caching dict for version_hex()/version_str() v0.6.x
authorPascal Volk <neverseen@users.sourceforge.net>
Thu, 29 Apr 2010 03:05:22 +0000
branchv0.6.x
changeset 273 77fc7138ef6a
parent 272 446483386914
child 274 45ec5c3cfef4
VMM/common: added a caching dict for version_hex()/version_str()
VirtualMailManager/common.py
--- a/VirtualMailManager/common.py	Wed Apr 28 09:00:02 2010 +0000
+++ b/VirtualMailManager/common.py	Thu Apr 29 03:05:22 2010 +0000
@@ -19,6 +19,7 @@
 
 _version_re = re.compile(r'^(\d+)\.(\d+)\.(?:(\d+)|(alpha|beta|rc)(\d+))$')
 _version_level = dict(alpha=0xA, beta=0xB, rc=0xC)
+_version_cache = {}
 _ = lambda msg: msg
 
 
@@ -73,6 +74,9 @@
     version_hex('1.2.3') -> 270548736
     hex(version_hex('1.2.3')) -> '0x10203f00'
     """
+    global _version_cache, _version_level, _version_re
+    if version_string in _version_cache:
+        return _version_cache[version_string]
     version = 0
     version_mo = _version_re.match(version_string)
     if not version_mo:
@@ -97,6 +101,7 @@
     if serial:
         version += serial
 
+    _version_cache[version_string] = version
     return version
 
 
@@ -106,6 +111,9 @@
     Raises a `TypeError` if *version* is not an int/long.
     Raises a `ValueError` if *version* is an incorrect int version.
     """
+    global _version_cache, _version_level
+    if version in _version_cache:
+        return _version_cache[version]
     if not isinstance(version, (int, long)):
         raise TypeError('Argument is not a int/long: %r', version)
     major = (version >> 28) & 0xFF
@@ -122,6 +130,7 @@
     else:
         raise ValueError('Invalid version: %r' % hex(version))
 
+    _version_cache[version] = version_string
     return version_string
 
 del _