Add make_version function

pisi.version.Version is one of most frequently used class in pisi
(esp. when calculating dependencies). This new function is a fast
alternative which returns a tuple representation of version strings.
Since python is able to compare tuple and list objects, we do not
have to write a special compare function.

Here are some results:

In [7]: %timeit -n100000 pisi.version.make_version("1.2.3.4.5")
100000 loops, best of 3: 8.12 us per loop

In [8]: %timeit -n100000 pisi.version.Version("1.2.3.4.5")
100000 loops, best of 3: 60.6 us per loop

In [9]: %timeit -n100000 pisi.version.make_version("1.2") < pisi.version.make_version("1.3")
100000 loops, best of 3: 10.1 us per loop

In [10]: %timeit -n100000 pisi.version.Version("1.2") < pisi.version.Version("1.3")
100000 loops, best of 3: 85.4 us per loop
This commit is contained in:
Fatih Aşıcı
2010-03-07 21:31:39 +00:00
parent f8328491e6
commit 813e93e702
+45 -1
View File
@@ -14,7 +14,10 @@
import re
import string
import exceptions
import gettext
__trans = gettext.translation('pisi', fallback=True)
_ = __trans.ugettext
import pisi
import pisi.util as util
@@ -35,6 +38,47 @@ keywords = {
"p" : 6,
}
__keywords = (
("alpha", -5),
("beta", -4),
("pre", -3),
("rc", -2),
("m", -1),
("p", 1),
)
class InvalidVersionError(pisi.Error):
pass
def __make_version_item(v):
try:
return int(v), None
except ValueError:
return int(v[:-1]), v[-1]
def make_version(version):
ver, sep, suffix = version.partition("_")
try:
if sep:
# "s" is a string greater than the greatest keyword "rc"
if "a" <= suffix <= "s":
for keyword, value in __keywords:
if suffix.startswith(keyword):
return map(__make_version_item, ver.split(".")), value, \
map(__make_version_item, suffix[len(keyword):].split("."))
else:
# Probably an invalid version string. Reset ver string
# to raise an exception in __make_version_item function.
ver = ""
else:
return map(__make_version_item, ver.split(".")), 0, \
map(__make_version_item, suffix.split("."))
return map(__make_version_item, ver.split(".")), 0, [(0, None)]
except ValueError:
raise InvalidVersionError(_("Invalid version string: '%s'") % version)
# helper functions
def has_keyword(versionitem):
if versionitem._keyword != "NOKEY":