     <link rel="alternate" type="application/atom+xml" title="sebthom.de Category: Jython" href="https://sebthom.de/de/category/it/development/jython/feed/" />
{"id":136,"date":"2009-03-14T00:03:57","date_gmt":"2009-03-13T23:03:57","guid":{"rendered":"http:\/\/sebthom.de\/?p=136"},"modified":"2010-02-20T00:29:39","modified_gmt":"2010-02-19T23:29:39","slug":"comparing-version-numbers-in-jython-pytho","status":"publish","type":"post","link":"https:\/\/sebthom.de\/de\/136-comparing-version-numbers-in-jython-pytho\/","title":{"rendered":"Comparing version numbers in Jython \/ Python"},"content":{"rendered":"<p>Here comes a function to compare version numbers of e.g. Maven artifacts in Jython \/ Python.<\/p>\n<pre name=\"code\" class=\"python\">\r\nimport re\r\n\r\ndef cmpver(vA, vB):\r\n    \"\"\"\r\n    Compares two version number strings\r\n    @param vA: first version string to compare\r\n    @param vB: second version string to compare\r\n    @author <a href=\"http:\/\/sebthom.de\/\">Sebastian Thomschke<\/a>\r\n    @return negative if vA < vB, zero if vA == vB, positive if vA > vB.\r\n   \r\n    Examples:\r\n    >>> cmpver(\"0\", \"1\")\r\n    -1\r\n    >>> cmpver(\"1\", \"0\")\r\n    1\r\n    >>> cmpver(\"1\", \"1\")\r\n    0\r\n    >>> cmpver(\"1.0\", \"1.0\")\r\n    0\r\n    >>> cmpver(\"1.0\", \"1\")\r\n    0\r\n    >>> cmpver(\"1\", \"1.0\")\r\n    0\r\n    >>> cmpver(\"1.1.0\", \"1.0.1\")\r\n    1\r\n    >>> cmpver(\"1.0.1\", \"1.1.1\")\r\n    -1\r\n    >>> cmpver(\"0.3-SNAPSHOT\", \"0.3\")\r\n    -1\r\n    >>> cmpver(\"0.3\", \"0.3-SNAPSHOT\")\r\n    1\r\n    >>> cmpver(\"1.3b\", \"1.3c\")\r\n    -1\r\n    >>> cmpver(\"1.14b\", \"1.3c\")\r\n    1\r\n    \"\"\"\r\n    if vA == vB: return 0\r\n\r\n    def num(s): \r\n        if s.isdigit(): return int(s)\r\n        return s\r\n\r\n    seqA = map(num, re.findall('\\d+|\\w+', vA.replace('-SNAPSHOT', '')))\r\n    seqB = map(num, re.findall('\\d+|\\w+', vB.replace('-SNAPSHOT', '')))\r\n\r\n    # this is to ensure that 1.0 == 1.0.0 in cmp(..)\r\n    lenA, lenB = len(seqA), len(seqB)\r\n    for i in range(lenA, lenB): seqA += (0,) \r\n    for i in range(lenB, lenA): seqB += (0,)\r\n    \r\n    rc = cmp(seqA, seqB)\r\n    \r\n    if rc == 0:\r\n        if vA.endswith('-SNAPSHOT'): return -1\r\n        if vB.endswith('-SNAPSHOT'): return 1\r\n    return rc\r\n<\/pre>\n<p>Theoretically lines 43 &#8211; 49 could be written in a more compact way but using the short circuit evaluations (as below) lead to wrong results &#8211; at least in Jython 2.1:<\/p>\n<pre name=\"code\" class=\"python\">\r\n    seqa = map(lambda s: s.isdigit() and int(s) or s, re.findall('\\d+|\\w+', vA.replace('-SNAPSHOT', '')))\r\n    seqb = map(lambda s: s.isdigit() and int(s) or s, re.findall('\\d+|\\w+', vB.replace('-SNAPSHOT', '')))\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Here comes a function to compare version numbers of e.g. Maven artifacts in Jython \/ Python. import re def cmpver(vA, vB): &#8220;&#8221;&#8221; Compares two version number strings @param vA: first version string to compare @param vB: second version string to compare @author Sebastian Thomschke @return negative if vA < vB, zero if vA == vB,...\n<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[63],"tags":[105,65],"class_list":["post-136","post","type-post","status-publish","format-standard","hentry","category-jython","tag-jython","tag-python"],"_links":{"self":[{"href":"https:\/\/sebthom.de\/de\/wp-json\/wp\/v2\/posts\/136","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sebthom.de\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sebthom.de\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sebthom.de\/de\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sebthom.de\/de\/wp-json\/wp\/v2\/comments?post=136"}],"version-history":[{"count":0,"href":"https:\/\/sebthom.de\/de\/wp-json\/wp\/v2\/posts\/136\/revisions"}],"wp:attachment":[{"href":"https:\/\/sebthom.de\/de\/wp-json\/wp\/v2\/media?parent=136"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sebthom.de\/de\/wp-json\/wp\/v2\/categories?post=136"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sebthom.de\/de\/wp-json\/wp\/v2\/tags?post=136"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}