473,382 Members | 1,442 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

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

Jul 18 '05 #1
8 4226
On 25 Mar 2005 07:34:38 -0800, rumours say that "Keith"
<ve****@lincom-asg.com> might have written:
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


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...
Jul 18 '05 #2
On 25 Mar 2005 07:34:38 -0800, Keith <ve****@lincom-asg.com> wrote:
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


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
Jul 18 '05 #3
"Keith" wrote:
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


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
cmpver("0.1.0", "0.1.2") -1 cmpver("1.876b", "1.876c") -1 cmpver("3.2.2", "3.4")

-1

ymmv.

</F>

Jul 18 '05 #4
I recently saw this:

http://www.egenix.com/files/python/mxTools.html

mx.Tools.verscmp(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:
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


Jul 18 '05 #5
On Fri, 25 Mar 2005 17:02:31 +0100, "Fredrik Lundh" <fr*****@pythonware.com> wrote:
"Keith" wrote:
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


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


[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
Jul 18 '05 #6
On Fri, 25 Mar 2005 19:23:37 GMT, rumours say that bo**@oz.net (Bengt
Richter) might have written:
On Fri, 25 Mar 2005 17:02:31 +0100, "Fredrik Lundh" <fr*****@pythonware.com> wrote:
"Keith" wrote:
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


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


[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 ... ?


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

http://groups-beta.google.com/group/...99103bb19c7332
--
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...
Jul 18 '05 #7
Christos TZOTZIOY Georgiou wrote:

One of the previous related threads is this (long URL):
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: 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.


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

Jul 18 '05 #8
On 29 Mar 2005 00:29:06 -0800, "El Pitonero" <pi******@gmail.com> wrote:
Christos TZOTZIOY Georgiou wrote:

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

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:
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.


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

Do you have specific recommendations that might benefit python 3000?
What better "core design" features would have eliminated what "new features"?
;-)

Regards,
Bengt Richter
Jul 18 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Anthony Baxter | last post by:
To go along with the 2.4a3 release, here's an updated version of the decorator PEP. It describes the state of decorators as they are in 2.4a3. PEP: 318 Title: Decorators for Functions and...
6
by: David Cook | last post by:
The html file below gets intermittent errors 'error on page' ('number expected') when clicking on column-headings to sort. Yet, this same file works flawlessly in other browsers (i.e. Opera,...
2
by: the ram arm | last post by:
Can't seem to be able to run this array. <HTML> <HEAD></HEAD> <BODY> <script language="javascript"> function e(#####,n) { this.s = s; this.m = m; this.n = n;
122
by: Einar | last post by:
Hi, I wonder if there is a nice bit twiddling hack to compare a large number of variables? If you first store them in an array, you can do: for (i = 0; i < n; i++) { if (array != value) {...
3
by: Chandu | last post by:
Hi, I am working on awk programming which is similar to C programming and have got a doubt about time function returning a float value. Ex: 01:01:30 should return 61.5 when i have tried my way i...
1
by: Tom | last post by:
I have two version numbers - one for my application and another from a manifest, database, whatever - that isn't important. Now that I have these two version numbers (in the form...
1
by: comp.lang.php | last post by:
I have written several applications that have very specific functionality that is version-dependent, furthermore, requirements dictate my apps span multiple versions of PHP. I have written a...
5
by: B1ackwater | last post by:
We've fooled around with Access a bit, but only using the single-user store-bought version. It seems to be a good database - versatile and infinitely programmable - and can apparently be used as a...
10
by: akselo | last post by:
Hi folks, I am working on a routine that will select a sample of parcels from a table. Each parcel belongs to a census tract, and depending on which tract, a certain calculation is applied. The...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.