Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>How about using Python's <code>distutils.version.StrictVersion</code>?</p> <pre><code>&gt;&gt;&gt; from distutils.version import StrictVersion &gt;&gt;&gt; StrictVersion('10.4.10') &gt; StrictVersion('10.4.9') True </code></pre> <p>So for your <code>cmp</code> function:</p> <pre><code>&gt;&gt;&gt; cmp = lambda x, y: StrictVersion(x).__cmp__(y) &gt;&gt;&gt; cmp("10.4.10", "10.4.11") -1 </code></pre> <p>If you want to compare version numbers that are more complex <code>distutils.version.LooseVersion</code> will be more useful, however be sure to only compare the same types.</p> <pre><code>&gt;&gt;&gt; from distutils.version import LooseVersion, StrictVersion &gt;&gt;&gt; LooseVersion('1.4c3') &gt; LooseVersion('1.3') True &gt;&gt;&gt; LooseVersion('1.4c3') &gt; StrictVersion('1.3') # different types False </code></pre> <p><code>LooseVersion</code> isn't the most intelligent tool, and can easily be tricked:</p> <pre><code>&gt;&gt;&gt; LooseVersion('1.4') &gt; LooseVersion('1.4-rc1') False </code></pre> <p>To have success with this breed, you'll need to step outside the standard library and use <a href="http://packages.python.org/distribute/">distribute</a>'s parsing utility <a href="http://packages.python.org/distribute/pkg_resources.html#parsing-utilities"><code>parse_version</code></a>.</p> <pre><code>&gt;&gt;&gt; from pkg_resources import parse_version &gt;&gt;&gt; parse_version('1.4') &gt; parse_version('1.4-rc2') True </code></pre> <p>So depending on your specific use-case, you'll need to decide whether the builtin <code>distutils</code> tools are enough, or if it's warranted to add as a dependency <code>distribute</code>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload