Skip to content

sebthom.de

Menu
  • Home
  • Über mich
  • Impressum
    • Datenschutzerklärung
  • Gästebuch
Menu

Comparing version numbers in Jython / Python

Posted on Samstag, der 14. März 2009Samstag, der 20. Februar 2010 by sebthom

Here comes a function to compare version numbers of e.g. Maven artifacts in Jython / Python.

view plaincopy to clipboardprint?
  1. import re  
  2.   
  3. def cmpver(vA, vB):  
  4.     """ 
  5.     Compares two version number strings 
  6.     @param vA: first version string to compare 
  7.     @param vB: second version string to compare 
  8.     @author <a href="//sebthom.de/">Sebastian Thomschke</a> 
  9.     @return negative if vA < vB, zero if vA == vB, positive if vA > vB. 
  10.     
  11.     Examples: 
  12.     >>> cmpver("0", "1") 
  13.     -1 
  14.     >>> cmpver("1", "0") 
  15.     1 
  16.     >>> cmpver("1", "1") 
  17.     0 
  18.     >>> cmpver("1.0", "1.0") 
  19.     0 
  20.     >>> cmpver("1.0", "1") 
  21.     0 
  22.     >>> cmpver("1", "1.0") 
  23.     0 
  24.     >>> cmpver("1.1.0", "1.0.1") 
  25.     1 
  26.     >>> cmpver("1.0.1", "1.1.1") 
  27.     -1 
  28.     >>> cmpver("0.3-SNAPSHOT", "0.3") 
  29.     -1 
  30.     >>> cmpver("0.3", "0.3-SNAPSHOT") 
  31.     1 
  32.     >>> cmpver("1.3b", "1.3c") 
  33.     -1 
  34.     >>> cmpver("1.14b", "1.3c") 
  35.     1 
  36.     """  
  37.     if vA == vB: return 0  
  38.   
  39.     def num(s):   
  40.         if s.isdigit(): return int(s)  
  41.         return s  
  42.   
  43.     seqA = map(num, re.findall('\d+|\w+', vA.replace('-SNAPSHOT', '')))  
  44.     seqB = map(num, re.findall('\d+|\w+', vB.replace('-SNAPSHOT', '')))  
  45.   
  46.     # this is to ensure that 1.0 == 1.0.0 in cmp(..)  
  47.     lenA, lenB = len(seqA), len(seqB)  
  48.     for i in range(lenA, lenB): seqA += (0,)   
  49.     for i in range(lenB, lenA): seqB += (0,)  
  50.       
  51.     rc = cmp(seqA, seqB)  
  52.       
  53.     if rc == 0:  
  54.         if vA.endswith('-SNAPSHOT'): return -1  
  55.         if vB.endswith('-SNAPSHOT'): return 1  
  56.     return rc  
import re

def cmpver(vA, vB):
    """
    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, positive if vA > vB.
   
    Examples:
    >>> cmpver("0", "1")
    -1
    >>> cmpver("1", "0")
    1
    >>> cmpver("1", "1")
    0
    >>> cmpver("1.0", "1.0")
    0
    >>> cmpver("1.0", "1")
    0
    >>> cmpver("1", "1.0")
    0
    >>> cmpver("1.1.0", "1.0.1")
    1
    >>> cmpver("1.0.1", "1.1.1")
    -1
    >>> cmpver("0.3-SNAPSHOT", "0.3")
    -1
    >>> cmpver("0.3", "0.3-SNAPSHOT")
    1
    >>> cmpver("1.3b", "1.3c")
    -1
    >>> cmpver("1.14b", "1.3c")
    1
    """
    if vA == vB: return 0

    def num(s): 
        if s.isdigit(): return int(s)
        return s

    seqA = map(num, re.findall('\d+|\w+', vA.replace('-SNAPSHOT', '')))
    seqB = map(num, re.findall('\d+|\w+', vB.replace('-SNAPSHOT', '')))

    # this is to ensure that 1.0 == 1.0.0 in cmp(..)
    lenA, lenB = len(seqA), len(seqB)
    for i in range(lenA, lenB): seqA += (0,) 
    for i in range(lenB, lenA): seqB += (0,)
    
    rc = cmp(seqA, seqB)
    
    if rc == 0:
        if vA.endswith('-SNAPSHOT'): return -1
        if vB.endswith('-SNAPSHOT'): return 1
    return rc

Theoretically lines 43 – 49 could be written in a more compact way but using the short circuit evaluations (as below) lead to wrong results – at least in Jython 2.1:

view plaincopy to clipboardprint?
  1. seqa = map(lambda s: s.isdigit() and int(s) or s, re.findall('\d+|\w+', vA.replace('-SNAPSHOT', '')))  
  2. seqb = map(lambda s: s.isdigit() and int(s) or s, re.findall('\d+|\w+', vB.replace('-SNAPSHOT', '')))  
    seqa = map(lambda s: s.isdigit() and int(s) or s, re.findall('\d+|\w+', vA.replace('-SNAPSHOT', '')))
    seqb = map(lambda s: s.isdigit() and int(s) or s, re.findall('\d+|\w+', vB.replace('-SNAPSHOT', '')))

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert

Kategorien

  • Blog (1)
  • IT (21)
    • Entwicklung (16)
      • Java (7)
      • Jython (4)
      • Visual Basic (5)
    • Linux (3)
    • WebSphere Application Server (1)
    • WebSphere Portal (2)
    • Windows (1)
  • My Freeware (2)
  • My Music (3)

Neueste Beiträge

  • Logging WebSphere API calls in wsadmin scripts
  • [Solved] Windows 7 “Safely Remove Hardware” pop-up menu horrendously slow
  • Bash: Capturing stderr in a variable while still printing to the console.
  • Configuring EMF Teneo with Hibernate, Commons DBCP, Spring Hibernate Transaction Manager, and the OpenSessionInViewFilter
  • Using EMF ECore model objects with Wicket components
  • Installing Tomcat 6 on Debian Squeeze
  • Leveraging PyDev’s auto-completion for indirectly created objects
  • OVal 1.40 released
  • Installing WebSphere Portal in a local network
  • Comparing version numbers in Jython / Python

Blogroll

  • E L S U A
  • elektrofever.de
  • OVal
  • Sweettt.com
  • Twins’ Running Blog

Neueste Kommentare

  • Annibale bei Visual Basic – Multiple Undos Class v2.04
  • Annibale bei Visual Basic – Multiple Undos Class v2.04
  • koliko2k3 bei Gästebuch
  • hdkid bei MyPad v1.1.6 – ein PHP Editor
  • Luis Diego Villarreal bei Excel – VBA Timer Example v1.0.1

Archive

  • Juni 2014
  • Mai 2012
  • Januar 2011
  • Oktober 2010
  • September 2010
  • März 2010
  • Februar 2010
  • September 2009
  • Juli 2009
  • März 2009
  • Februar 2009
  • November 2008
  • September 2008
  • Mai 2008
  • September 2007
  • Juli 2007
  • Juli 2004
  • März 2003
  • August 2002
  • April 2002
  • Januar 2002
  • Deutsch (de)Deutsch
  • English (en)English
© 2025 sebthom.de | Powered by Minimalist Blog WordPress Theme
Menu
  • Home
  • Über mich
  • Impressum
    • Datenschutzerklärung
  • Gästebuch