* I had written a really horrible routine to compare two numeric

sequences, and it caused some trouble. Now I did a much better
routine. Sorry for all the inconvenience. fiXX0Red
This commit is contained in:
Eray Özkural
2005-10-17 22:37:02 +00:00
parent 462748f3d5
commit e5f528a3da
3 changed files with 53 additions and 31 deletions
+8 -1
View File
@@ -24,6 +24,7 @@ import sys
import sha import sha
import shutil import shutil
import statvfs import statvfs
import operator
import gettext import gettext
__trans = gettext.translation('pisi', fallback=True) __trans = gettext.translation('pisi', fallback=True)
@@ -67,9 +68,15 @@ class Checks:
print_errors = staticmethod(print_errors) print_errors = staticmethod(print_errors)
######################### #########################
# string/list functions # # string/list/functional#
######################### #########################
def every(seq, pred):
return reduce(operator.and_, map(pred, seq))
def any(seq, pred):
return reduce(operator.or_, map(pred, seq))
def unzip(seq): def unzip(seq):
return zip(*seq) return zip(*seq)
+42 -29
View File
@@ -9,10 +9,17 @@
# #
# Please read the COPYING file. # Please read the COPYING file.
# #
# version structure
# Authors: Eray Ozkural <eray@uludag.org.tr> # Authors: Eray Ozkural <eray@uludag.org.tr>
# Baris Metin <baris@uludag.org.tr>
#
# History:
# Eray wrote the first version
# Baris added support for the fancy keywords and
# alphanumeric version string components
"""version structure"""
import re import re
@@ -59,6 +66,9 @@ class VersionItem:
# one! # one!
self._value = itemstring[len(self._keyword):] self._value = itemstring[len(self._keyword):]
def __str__(self):
return str(self._value)
def __lt__(self,rhs): def __lt__(self,rhs):
l = keywords[self._keyword] l = keywords[self._keyword]
r = keywords[rhs._keyword] r = keywords[rhs._keyword]
@@ -130,39 +140,42 @@ class Version:
def string(self): def string(self):
return self.verstring return self.verstring
def pred(self, rhs, pred): def compare(self, rhs):
"""this comparison routine is essentially a comparison routine
loop = len(self.comps) for two rationals in (0,1) interval. we compare two sequences
if len(rhs.comps) > loop: of digits one by one. We start with the leftmost digit
loop = len(rhs.comps) in the expansion. If they are equal, we proceed to the next. If
not we use the comparison operator. And we iterate to the left.
The result is, 0 if two are equal, -1 if self < rhs, and +1
if self>rhs"""
lhs = self.comps
rhs = rhs.comps
# pad the short version string with zeros
if len(lhs) < len(rhs):
lhs.extend( [VersionItem('0')] * (len(rhs) - len(lhs)) )
elif len(lhs) > len(rhs):
rhs.extend( [VersionItem('0')] * (len(lhs) - len(rhs)) )
# now let's iterate from left to right
for (litem, ritem) in zip(lhs, rhs):
if litem < ritem:
return -1
elif litem > ritem:
return +1
return 0
for i in range(0, loop): # premature optimization is the root of all evil
try:
litem = self.comps[i]
except IndexError:
litem = VersionItem("")
try:
ritem = rhs.comps[i]
except IndexError:
ritem = VersionItem("")
if not pred(litem, ritem):
return False
return True
def __lt__(self,rhs): def __lt__(self,rhs):
return self.pred(rhs, lambda x,y: x<y) return self.compare(rhs) < 0
def __le__(self,rhs): def __le__(self,rhs):
return self.pred(rhs, lambda x,y: x<=y) return self.compare(rhs) <= 0
def __gt__(self,rhs): def __gt__(self,rhs):
return self.pred(rhs, lambda x,y: x>y) return self.compare(rhs) > 0
def __ge__(self,rhs): def __ge__(self,rhs):
return self.pred(rhs, lambda x,y: x>=y) return self.compare(rhs) >= 0
def __eq__(self,rhs): def __eq__(self,rhs):
return self.pred(rhs, lambda x,y: x==y) return self.compare(rhs) == 0
+3 -1
View File
@@ -37,6 +37,7 @@ def run_all():
import versiontests import versiontests
import configfiletests import configfiletests
import packagetests import packagetests
import dependency
alltests = unittest.TestSuite(( alltests = unittest.TestSuite((
utiltests.suite, utiltests.suite,
@@ -54,7 +55,8 @@ def run_all():
graphtests.suite, graphtests.suite,
versiontests.suite, versiontests.suite,
configfiletests.suite, configfiletests.suite,
packagetests.suite packagetests.suite,
dependencytests.suite
)) ))
runTestSuite(alltests) runTestSuite(alltests)