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:
+45
-1
@@ -14,7 +14,10 @@
|
|||||||
|
|
||||||
import re
|
import re
|
||||||
import string
|
import string
|
||||||
import exceptions
|
|
||||||
|
import gettext
|
||||||
|
__trans = gettext.translation('pisi', fallback=True)
|
||||||
|
_ = __trans.ugettext
|
||||||
|
|
||||||
import pisi
|
import pisi
|
||||||
import pisi.util as util
|
import pisi.util as util
|
||||||
@@ -35,6 +38,47 @@ keywords = {
|
|||||||
"p" : 6,
|
"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
|
# helper functions
|
||||||
def has_keyword(versionitem):
|
def has_keyword(versionitem):
|
||||||
if versionitem._keyword != "NOKEY":
|
if versionitem._keyword != "NOKEY":
|
||||||
|
|||||||
Reference in New Issue
Block a user