Reputation: 41
I'm trying to detect if a software version is up to date and I'm doing this by using the following code in Python 3.3:
if str(version) in str(buildno):
print("Your software version is up to date. \n")
else:
print("Your software is out of date. Updating your software. \n")
However, it keeps updating the software even when it is up to date. I've also tried the code variation:
if str(version) in str(buildno) == True:
print("Your software version is up to date. \n")
else:
print("Your software is out of date. Updating your software. \n")
if os == "Windows":
subprocess.call("windowsUpgrade.sh", shell=True)
which also does not work. Is the method I'm using workable or should I be taking another approach to this problem?
>>> print(version)
4.3.0-18107
>>> print(buildno)
('4.3.0-18107', 1)
Thank you for any answers provided.
Upvotes: 1
Views: 225
Reputation: 114
Well it seems that there is a confusion about the datatypes used here
Tuple to String:
str(('4.3.0-18107', 1)) = "('4.3.0-18107', 1)"
Tuple not in String:
if "('4.3.0-18107', 1)" in '4.3.0-18107' # False
String in Tuple
if '4.3.0-18107' in "('4.3.0-18107', 1)" # True
String in (first index Tuple = String)
if '4.3.0-18107' in ('4.3.0-18107', 1)[0] # True
If order shouldn't matter you need to index the tuple str(('4.3.0-18107', 1)[0]) before converting to a string. What you did in your above code is you converted the tuple into a string and not the version. Pavel Anossov was therefore right that a swap should work here - at least it does it for me.
So this worked in the end (a whitespace missed out):
buildno=buildno[0]
version=str(version.strip())
buildno=str(buildno.strip())
if version == buildno
or shorter:
if str(version).strip() == str(buildno[0]).strip():
if str(version).strip() in str(buildno[0]).strip():
Upvotes: 1
Reputation: 20870
Since you're using Python 3, you can use the distutils.version
comparision module described in PEP386:
from distutils.version import LooseVersion as V
minimum_version = V(version)
current_version = V(buildno)
if current_version >= minimum_version:
print("Your software version is up to date. \n")
else:
print("Your software is out of date. Updating your software. \n")
There is also a StrictVersion
class, but it doesn't seem to work with your version numbering.
Upvotes: 1
Reputation: 7706
Your buildno
is a tuple. You need the first item only. That is:
if str(buildno[0]) in str(version):
Or even:
if str(buildno[0]) == str(version):
as Pavel Anossov suggested in the comments.
On a side note, your second approach:
if str(buildno) in str(version) == True:
Can be roughly translated using dis as:
if str(buildno) in str(version) and str(version) == True:
Also, take a look at DSM's comment of your question.
Using string containment like this can lead to trouble: '4.3.0-1' is in '4.3.0-11', but presumably isn't up-to-date. Depending on your version numbering strategy, you might want to make a tuple of ints to compare.
Upvotes: 1
Reputation: 62898
Your second variation will not work. The first variation should work if you swap buildno
and version
:
buildno = '4.3.0-18107'
version = ('4.3.0-18107', 1)
if str(buildno) in str(version):
print("Your software version is up to date. \n")
I assume one is a string and another one is a tuple, but they can be anything, since we only saw them printed and not how you got them.
Judging by their content, these variable names are somewhat misleading, swapped or not.
Upvotes: 1