Version Number Comparison Function

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Keith

    #1

    Version Number Comparison Function

    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

    Keith

  • Christos TZOTZIOY Georgiou

    #2
    Re: Version Number Comparison Function

    On 25 Mar 2005 07:34:38 -0800, rumours say that "Keith"
    <vetter@linco m-asg.com> might have written:
    [color=blue]
    >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
    >
    >Keith[/color]

    Convert your version numbers into tuples:

    (0, 1, 0) < (0, 1, 2)
    (1, 876, 'b') < (1, 876, 'c')
    (3, 2, 2) < (3, 4)

    All of the above are True.
    --
    TZOTZIOY, I speak England very best.
    "Be strict when sending and tolerant when receiving." (from RFC1958)
    I really should keep that in mind when talking with people, actually...

    Comment

    • Bill Mill

      #3
      Re: Version Number Comparison Function

      On 25 Mar 2005 07:34:38 -0800, Keith <vetter@linco m-asg.com> wrote:[color=blue]
      > 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
      >[/color]

      Not by default AFAIK. How about something like (untested):

      def test_version(v1 , v2):
      v1, v2 = v1.split('.'), v2.split('.')
      for x, y in zip(v1, v2):
      if x < y: return v1
      if y > x: return v2

      It assumes that v1 and v2 have the same amount of '.'s and that all of
      the version numbers are of the same length (i.e. 1.1000 would be <
      1.999). How general do you need to be?

      Peace
      Bill Mill
      bill.mill at gmail.com

      Comment

      • Fredrik Lundh

        #4
        Re: Version Number Comparison Function

        "Keith" wrote:
        [color=blue]
        > 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[/color]

        the following works for many common cases:

        import re

        def cmpver(a, b):
        def fixup(i):
        try:
        return int(i)
        except ValueError:
        return i
        a = map(fixup, re.findall("\d+ |\w+", a))
        b = map(fixup, re.findall("\d+ |\w+", b))
        return cmp(a, b) # -1 if a<b, 0 if a=b, 1 if a>b
        [color=blue][color=green][color=darkred]
        >>> cmpver("0.1.0", "0.1.2")[/color][/color][/color]
        -1[color=blue][color=green][color=darkred]
        >>> cmpver("1.876b" , "1.876c")[/color][/color][/color]
        -1[color=blue][color=green][color=darkred]
        >>> cmpver("3.2.2", "3.4")[/color][/color][/color]
        -1

        ymmv.

        </F>



        Comment

        • Steve M

          #5
          Re: Version Number Comparison Function

          I recently saw this:



          mx.Tools.verscm p(a,b)
          Compares two version strings and returns a cmp() function
          compatible value (<,==,> 0). The function is useful for sorting lists
          containing version strings.

          The logic used is as follows: the strings are compared at each
          level, empty levels defaulting to '0', numbers with attached strings
          (e.g. '1a1') compare less than numbers without attachement (e.g. '1a1'
          < '1).

          Keith wrote:[color=blue]
          > 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
          >
          > Keith[/color]

          Comment

          • Bengt Richter

            #6
            Re: Version Number Comparison Function

            On Fri, 25 Mar 2005 17:02:31 +0100, "Fredrik Lundh" <fredrik@python ware.com> wrote:
            [color=blue]
            >"Keith" wrote:
            >[color=green]
            >> 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[/color]
            >
            >the following works for many common cases:
            >
            >import re
            >
            >def cmpver(a, b):
            > def fixup(i):
            > try:
            > return int(i)
            > except ValueError:
            > return i
            > a = map(fixup, re.findall("\d+ |\w+", a))
            > b = map(fixup, re.findall("\d+ |\w+", b))
            > return cmp(a, b) # -1 if a<b, 0 if a=b, 1 if a>b[/color]

            [OT] Visually, I like the nested def fixup, and I realize
            that for cmpver execution overhead is not likely to be an issue,
            but in general, what do you think of not being able
            to write it that way if MAKE_FUNCTION overhead is unacceptable?

            What if we had something like

            @sticky('fixup' ) # evaluate binding only first time
            def cmpver(a , b):
            def fixup ... ?

            Regards,
            Bengt Richter

            Comment

            • Christos TZOTZIOY Georgiou

              #7
              Re: &quot;static&qu ot; variables in functions (was: Version Number Comparison Function)

              On Fri, 25 Mar 2005 19:23:37 GMT, rumours say that bokr@oz.net (Bengt
              Richter) might have written:
              [color=blue]
              >On Fri, 25 Mar 2005 17:02:31 +0100, "Fredrik Lundh" <fredrik@python ware.com> wrote:
              >[color=green]
              >>"Keith" wrote:
              >>[color=darkred]
              >>> 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[/color]
              >>
              >>the following works for many common cases:
              >>
              >>import re
              >>
              >>def cmpver(a, b):
              >> def fixup(i):
              >> try:
              >> return int(i)
              >> except ValueError:
              >> return i
              >> a = map(fixup, re.findall("\d+ |\w+", a))
              >> b = map(fixup, re.findall("\d+ |\w+", b))
              >> return cmp(a, b) # -1 if a<b, 0 if a=b, 1 if a>b[/color]
              >
              >[OT] Visually, I like the nested def fixup, and I realize
              >that for cmpver execution overhead is not likely to be an issue,
              >but in general, what do you think of not being able
              >to write it that way if MAKE_FUNCTION overhead is unacceptable?
              >
              >What if we had something like
              >
              >@sticky('fixup ') # evaluate binding only first time
              >def cmpver(a , b):
              > def fixup ... ?[/color]

              One of the previous related threads is this (long URL):


              --
              TZOTZIOY, I speak England very best.
              "Be strict when sending and tolerant when receiving." (from RFC1958)
              I really should keep that in mind when talking with people, actually...

              Comment

              • El Pitonero

                #8
                Re: &quot;static&qu ot; variables in functions (was: Version Number Comparison Function)

                Christos TZOTZIOY Georgiou wrote:[color=blue]
                >
                > One of the previous related threads is this (long URL):
                >[/color]


                Another previous message on this issue:



                Python's syntax surely is not clean enough for concise metaprogramming .
                At any rate, I'd agree with Fernando's assessment:

                Fernando wrote:[color=blue]
                > The real problem with Python is ... Python is
                > going the C++ way: piling feature upon feature, adding bells
                > and whistles while ignoring or damaging its core design.[/color]

                If the core design were better, many "new features" in Python could
                have been rendered unnecessary.

                Comment

                • Bengt Richter

                  #9
                  Re: &quot;static&qu ot; variables in functions (was: Version Number Comparison Function)

                  On 29 Mar 2005 00:29:06 -0800, "El Pitonero" <pitonero@gmail .com> wrote:
                  [color=blue]
                  >Christos TZOTZIOY Georgiou wrote:[color=green]
                  >>
                  >> One of the previous related threads is this (long URL):
                  >>[/color]
                  >http://groups-beta.google.com/group/...99103bb19c7332
                  >
                  >Another previous message on this issue:
                  >
                  >http://groups-beta.google.com/group/...15d8b83cca5b20
                  >
                  >Python's syntax surely is not clean enough for concise metaprogramming .
                  >At any rate, I'd agree with Fernando's assessment:
                  >
                  >Fernando wrote:[color=green]
                  >> The real problem with Python is ... Python is
                  >> going the C++ way: piling feature upon feature, adding bells
                  >> and whistles while ignoring or damaging its core design.[/color]
                  >
                  >If the core design were better, many "new features" in Python could
                  >have been rendered unnecessary.
                  >[/color]
                  Do you have specific recommendations that might benefit python 3000?
                  What better "core design" features would have eliminated what "new features"?
                  ;-)

                  Regards,
                  Bengt Richter

                  Comment

                  Working...