[Distutils] Comparison semantics for alphanumeric components of a version number

Brandon Craig Rhodes brandon at rhodesmill.org
Fri Jun 5 16:03:09 CEST 2009


Ben Finney <ben+python at benfinney.id.au> writes:

>     >>> from verlib import RationalVersion as V
>     >>> (V('1.0')
>     ...  < V('1.0.a1')
>     ...  < V('1.0.a2')
>     ...  < V('1.0.a2.1')
>     ...  < V('1.0.b2')
>     ...  < V('1.0.c1')
>     ...  < V('1.0.dev456post623')
>     ...  < V('1.0.post456'))
>     True

+1

With a caveat (or maybe this is the way it already works?) that every
series of adjacent digits in the version be compared as an integer
occupying a single character's worth of string, regardless of how many
digits compose the integer - which, I personally think, is how strings
should always be compared, and is the first thing I'd fix if I could go
back in time and improve something about Unix on the PDP in 1970 so that
it would have followed as the default string comparison mechanism
through the rest of time.  The fact that "log.7" sorts after "log.10" is
simply stupid, and trying to fix the problem with leading zeros is a
silly hack that breaks as soon as you get more log files that you
thought you'd need.

In other words:

  - Take the strings you are comparing:
         "1.0.post45pre12"
         "1.0.post9pre12"

  - Consider adjacent series of digits to be integers, not strings:
         (1, '.', 0, '.post', 45, 'pre', 12)
         (1, '.', 0, '.post',  9, 'pre', 12)

  - Compare the results, making (in this case) the number 9 come before
    the number 45, correcting the normal problem that the string "9" is
    lexicographically after the string "45".

-- 
Brandon Craig Rhodes   brandon at rhodesmill.org   http://rhodesmill.org/brandon


More information about the Distutils-SIG mailing list