1

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.

11
  • 9
    You should show us what buildno and version are. This code is fine. Commented Feb 28, 2013 at 12:49
  • 3
    It simply looks like buildno is not in version. :D The first version is fine. Commented Feb 28, 2013 at 12:49
  • With a print(buildno, version) the output is "('4.3.0-18107', 1) 4.3.0-18107", so I figured I'd had them the wrong way around however the problem still persists. Commented Feb 28, 2013 at 12:58
  • 1
    @Metagen It seems you need str(buildno[0]) in str(version) Commented Feb 28, 2013 at 13:18
  • 4
    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. Commented Feb 28, 2013 at 13:20

4 Answers 4

1

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():
Sign up to request clarification or add additional context in comments.

5 Comments

Right so here's what I've done buildno=buildno[0] version=str(version) buildno=str(buildno) print(buildno) print(version) if version == buildno: however the problem still persists despite printing the exact same output :S
Then It is something about 1.) enconding or 2.) what you are parsing isn't actually what you think it is. Try to use buildno[0].strip() and/or version.strip() to make sure you're not missing out any whitespace after your string. If everything fails reinstall python :)
See you, you are the man/woman! Problem solved, thank you so much. Also, is there any chance you could upvote this question so I can finally start upvoting answers and comments?
Glad I could help... Haha... I had a lot of problems like that when I started parsing logfiles..^^ What was it? strip or reinstall? ;)
buildno=buildno[0] version=str(version.strip()) buildno=str(buildno.strip()) if version == buildno:
1

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.

2 Comments

Swapping them does not work as I mentioned in the comments above. buildno is read straight from the registry using winreg and version is received from the server (this is the client side of TCP client/server) and written to a file then read and saved from that file and is indeed a tuple. I can add all the code from the client and server if you like?
The code I posted works. That means your buildno and version are different from mine.
1

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.

4 Comments

You might want to change it back, haha. For whatever reason if str(buildno[0]) == str(version): does not work
@Metagen: instead of print(version), try print(repr(version)) and print(repr(buildno)). It's possible that you have trailing whitespace in version which is preventing the equality check from working. (I'm on the record as thinking this is the wrong approach, though.)
@Metagen something is quite messed up with your data. As DSM states trailing whitespaces can be a reason. Also, do thing about an approach with a tuple of integers as DSM suggested.
Actually after having some lunch and coming back to this, I may have been hasty as this has not fixed my problem. Rather, it has reversed the situation so that it never updates. I'll have a quick check of the suggestions here and get back to you.
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.