Hi all,

class: FileCheck(object):
    def checkFiles(install_vers=None, uninstall_vers=None):
        if uninstall_vers != None or install_vers != None:
            if uninstall_vers != None:
                for ver in uninstall_vers:
                    print "checking %s" % ver
            if install_vers != None:
                for ver in install_vers:
                    pass
            print "done"

if __name__ == "__main__":
    check = FileCheck()
    check.checkFiles(uninstall_vers=["1.0","1.3"])

I'm getting the following error:

Checking 1.0 is uninstalled
Checking 1.3 is uninstalled
Traceback (most recent call last):
  File "airFileCheck.py", line 332, in <module>
    check.checkFiles(uninstall_vers=["1.0","1.3"])
  File "airFileCheck.py", line 316, in checkFiles
    for ver in install_vers:
TypeError: 'FileCheck' object is not iterable

If I remove the 'object' from the first line so it's: "class: FileCheck():" I get:

Checking 1.0 is uninstalled
Checking 1.3 is uninstalled
Traceback (most recent call last):
  File "airFileCheck.py", line 331, in <module>
    check.checkFiles(uninstall_vers=["1.0","1.3"])
  File "airFileCheck.py", line 315, in checkFiles
    for ver in install_vers:
TypeError: iteration over non-sequence

What am I doing wrong? Why isn't it detecting that my install_vers is equal to None?

Steve

Recommended Answers

All 4 Replies

class: FileCheck(object):
    def checkFiles(install_vers=None, uninstall_vers=None):
        if uninstall_vers != None or install_vers != None:
            if uninstall_vers != None:
                for ver in uninstall_vers:
                    print "checking %s" % ver
            if install_vers != None:
                for ver in install_vers:
                    pass
            print "done"

if __name__ == "__main__":
    check = FileCheck()
    check.checkFiles(uninstall_vers=["1.0","1.3"])

Hi asciiman,

Two things of mention. The first : after class causes an error when I run it on IDLE, I don't think you want that there.

The second thing of mention is I changed your 'if' statement in the line if install_vers != None: to an 'elif' and I stopped receiving the error message. Check to see if that works for you.

EDIT: Also just one more quick question, I am still new to Python and programming in general so I might be missing why...

Is there a reason for the if uninstall_vers != None or install_vers != None: line? It seems a little redundant seeing the two if statements that follow...

Two things of mention. The first : after class causes an error when I run it on IDLE, I don't think you want that there.

Yep, you're right. That was a mistake when I copied and pasted to here. It's not in my running code.

The second thing of mention is I changed your 'if' statement in the line if install_vers != None: to an 'elif' and I stopped receiving the error message. Check to see if that works for you.

That's not going to work. Both install_vers and uninstall_vers can possibly be true. I need to be able to check for both conditions.

Is there a reason for the if uninstall_vers != None or install_vers != None: line? It seems a little redundant seeing the two if statements that follow...

Yep, there's a reason. I need to execute the print "done" statement if either statement is true, so I need to have that block. The print "done" is actually several lines of code, but I simplified it for logics-sake to post in the forum.

Still looking for a viable solution for this. I am sure Python's got to be able to have this logic. What am I missing?

Steve

Ahh I see what you mean.

Would this work for you?

class FileCheck(object):
    def __init__(self, installVers=None, uninstallVers=None):
        self.installVers = installVers
        self.uninstallVers = uninstallVers
    def checkFiles(self):
        if self.installVers != None or self.uninstallVers != None:
            if self.uninstallVers != None:
                for version in self.uninstallVers:
                    print "Checking %s" % version
            else:
                print "No versions to uninstall specified."
            if self.installVers != None:
                for version in self.installVers:
                    print "Checking %s" % version
            else:
                print "No versions to install specified."
            print "Done"

if __name__ == "__main__":
    check = FileCheck(uninstallVers=['1.4', '1.5'])
    check.checkFiles()

Ah, I got it. The problem is that I wasn't including "self" as the first param of the method. Once I put that in it worked.

Thanks for your reply Dunganb.

Steve

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.