Parsing and comparing version strings (was: Odd version scheme)

Tim Chase python.list at tim.thechases.com
Thu Feb 12 20:34:50 EST 2015


On 2015-02-13 12:20, Ben Finney wrote:
> > Not sure why this is "ridiculous".
> 
> Right, versions are effectively a special type [0], specifically
> *because* they intentionally don't compare as scalar numbers or
> strings. It's not “ridiculous” to need custom comparisons when
> that's the case.
> 
> Python even comes with support for version parsing and comparison
> in the standard library [1]. So if anything's “ridiculous”, it's
> the act of re-implementing that and getting it wrong.
> 
> (Or maybe that such an important part of the standard library is
> largely undocumented.)

I was surprised (pleasantly) to learn of that corner of distutils,
but can never remember where it is in the stdlib or what it's
called.  So it's pretty standard for my process to be something like

 - Hmm, I need to compare version-strings
 - search the web for "python compare version number"
 - note the top-ranked Stack Overflow answer
 - spot that it uses distutils.version.{LooseVersion,StrictVersion}
 - use that in my code, optionally searching to get full docs with 
   "site:python.org LooseVersion StrictVersion", only to be
   surprised when something like
   https://docs.python.org/2/library/{module}
   isn't anywhere in the top umpteen hits.

"largely undocumented" is an understatement :-)

-tkc





More information about the Python-list mailing list