# HG changeset patch # User Pascal Volk # Date 1272510322 0 # Node ID 77fc7138ef6a4b22844f652bbf0f9e6203b533a6 # Parent 4464833869142d4aa667904dc69fa2db7f8b06ac VMM/common: added a caching dict for version_hex()/version_str() diff -r 446483386914 -r 77fc7138ef6a 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 _