views:

166

answers:

3

I am creating an app which will be used to upload images to a specified server. I have created my GUI in Qt Designer, everything works fine I just am stuck on something that I know is simple. Cant seem to wrap my head around it.

The idea is for the script to go through and see how many text fields are filed in with paths of images - from there get each path and in order upload each one to server. I can make it work with just one box just fine but when i try to create a loop for this process it falls apart. I basically need to return 'fullname' with each different path. This is just a snippit but you get the idea..

The concept seems simple enough and I have rewritten this as many ways and I could find and think of. Any help would be awesome. Should I be using lists to do this instead or something?

     # count how many images there are going to be
 if not self.imgOnePathLabel.text().isEmpty():
     totalImages = 1
     # gets the path from IMAGE 1 box
     image1 = self.imgOnePathLabel.text()
     fullname = '%s' % image1
 if not self.imgTwoPathLabel.text().isEmpty():
     totalImages = 2
     image2 = self.img2PathLabel.text()
     fullname = '%s' % image2
 if not self.imgThreePathLabel.text().isEmpty():
     totalImages = 3
     imageThreePath = self.imgThreePathLabel.text()
     fullname = '%s' % imageThreePath
    try:
     for x in range(1,totalImages,1):
      # split end file from the file path
      name = os.path.split(fullname)[1]
      f = open(fullname, "rb")
      # store our selected file
      ftp.storbinary('STOR ' + name, f)
      msg = "Sent <font color=green>" + name + "</font>"
      self.logBrowser.append(msg)
      f.close()

 finally:
     msg = "<font color=green>" "Ok" "</font>"
     self.logBrowser.append(msg)
A: 

Aside from the issue of the range needing to be +1 as so to reach to #3 (cf Remark by Vincent R),

(one of) the problem(s) is that the fullname variable is overwritten with each of the new case of non empty label.

It's hard to comment about the code to just fix it, i.e. I'd like to suggest and reorganize it a bit. For example by introducing a function to extract the name / path of a given image, given the UI object; this would avoid some of the repetition. Such a function, or the the caller of it, would then add each new fullname to a list thereof, which can then be used by the upload loop.

See Tendayi Mawushe's solution, which is respectful of the original structure but introduces a list as suggested. BTW this list then can be iterated over as the basis of the loop, instead of relying on the range() function, and this is much more pythonic (and this removes the need of fixing the problem with missing #3 with the range). While being handy on occasion, with Python, these numeric range driven loops are often an invitation to revisit the design.

mjv
Thanks a ton! I was betting the solution had to do with creating a list, I was just unsure the approach. I see what you mean about it being more pythonic for sure
Parker
+2  A: 

The issue you have is that you are assigning to the variable fullname three times overwriting it each time. So by the time you get to the for loop only have the last file name available if the last field has been set otherwise you get nothing at all. Your statement that you need a list of full names rather than one variable is correct. You need something like below:

    fullnames = []
    imageLabels = [self.imgOnePathLabel, self.imgTwoPathLabel,
            self.imgThreePathLabel]
    for imageLabel in imageLabels:
        if imageLabel.text():
            image = self.imgOnePathLabel.text()
            fullnames.append('%s' % image)
    try:
        for fullname in fullnames:
            # split end file from the file path
            name = os.path.split(fullname)[1]
            f = open(fullname, "rb")
            # store our selected file
            ftp.storbinary('STOR ' + name, f)
            msg = "Sent <font color=green>" + name + "</font>"
            self.logBrowser.append(msg)
            f.close()
    finally:
        msg = "<font color=green>" "Ok" "</font>"
        self.logBrowser.append(msg)
Tendayi Mawushe
Thank Tendayi- works like a charm, I think it feels kind of bulky with all my 'If Not' statements, but this is def a solution I was in need for!
Parker
You could definitely trim this down a bit, what I initially gave was a way to get past your immediate problem. I have made a minor update to the solution that makes it read a little better and makes it easier to add more text fields.
Tendayi Mawushe
A: 

Another issue with your original code is that if labels 1 and 3 are not blank, but label 2 is, totalImages would be set to 3 even though you only have two paths.

Also, a typo in this code ("Two" vs "2"):

if not self.imgTwoPathLabel.text().isEmpty():
    image2 = self.img2PathLabel.text()

And I believe you don't need the string substitution '%s' % image.

You could condense your code a little (and fix your issues) like this:

# this is a list of references to your PyQt4 label objects
imagePathLabels = [self.imgOnePathLabel, 
                   self.imgTwoPathLabel, 
                   self.imgThreePathLabel]

try:
    for label in imagePathLabels:
        if not label.text().isEmpty():
            image_path = label.text()
            image_name = os.path.split(image_path)[1]
            f = open(image_path, "rb")
            ftp.storbinary('STOR ' + image_name, f)
            msg = "Sent <font color=green>" + name + "</font>"
            self.logBrowser.append(msg)
            f.close()
finally:
    msg = "<font color=green>" "Ok" "</font>"
    self.logBrowser.append(msg)
tgray