Version Number Comparison Function

Robert Kern rkern at ucsd.edu
Fri Mar 25 18:03:26 CET 2005


[Pardon the piggybacking. My news-server does not see the OP's message.]

Fredrik Lundh wrote:
> "Keith" wrote:
> 
> 
>>Is there a function for comparing version numbers?
>>
>>E.g.
>>
>>0.1.0 < 0.1.2
>>1.876b < 1.876c
>>3.2.2 < 3.4

distutils has a set of version classes with comparisons.

In [1]:from distutils import version

In [2]:version?
Type:           module
Base Class:     <type 'module'>
String Form:    <module 'distutils.version' from 
'/System/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/distutils/version.pyc'>
Namespace:      Interactive
File: 
/System/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/distutils/version.py
Docstring:
     Provides classes to represent module version numbers (one class for
     each style of version numbering).  There are currently two such classes
     implemented: StrictVersion and LooseVersion.

     Every version number class implements the following interface:
       * the 'parse' method takes a string and parses it to some internal
         representation; if the string is an invalid version number,
         'parse' raises a ValueError exception
       * the class constructor takes an optional string argument which,
         if supplied, is passed to 'parse'
       * __str__ reconstructs the string that was passed to 'parse' (or
         an equivalent string -- ie. one that will generate an equivalent
         version number instance)
       * __repr__ generates Python code to recreate the version number 
instance
       * __cmp__ compares the current instance with either another instance
         of the same class or a string (which will be parsed to an instance
         of the same class, thus must follow the same rules)



-- 
Robert Kern
rkern at ucsd.edu

"In the fields of hell where the grass grows high
  Are the graves of dreams allowed to die."
   -- Richard Harter



More information about the Python-list mailing list