2

I have a method that is intended to grab all img elements from some html and to add a css style to ensure the image is resized if it is large. It works great until the final test : largest_size < img_size - I have tried all manner of different ways to express this simple thing, and yet it always evaluates to true - which means all images are resized regardless of their original size.

The code:

    def adjust_html(self, html_text):
    # pull image links and adjust those larger than 30k
    # to be width=100%
    html = etree.HTML(html_text)
    r = html.xpath('.//img')
    changed_text = False
    for elem in r:
        for tag, value in elem.attrib.iteritems():
            if tag == 'src':
                largest_size = 30720
                img_size = 0
                img_url = value
                if self.bad_urls.has_key(img_url):
                    break
                try:
                    usock = urllib2.urlopen(img_url)
                    img_size =  usock.info().get('Content-Length')
                except:
                    self.log.debug("***** 406 for " + img_url)
                    self.bad_urls[img_url] = True
                    break
                if img_size is None:
                    break
                else:
                    **if (largest_size < img_size):**
                        self.log.debug("*** " + img_url + " ***")
                        self.log.debug("********** img size = " + str(img_size) + " **********")
                        elem.set("style","width:100%")
                        changed_text = True
                break

    if changed_text == True:
        html_text = etree.tostring(html)

    return html_text

I know there has to be something simple wrong here - I just don't see it :)

2
  • Marking interesting parts of code with ** is generally a bad idea. Put some # comments before and after the interesting part. Commented Jun 10, 2011 at 6:49
  • is img_size actually an int? change your debugging statement to 'img_size=%d' % img_size Commented Jun 10, 2011 at 6:51

2 Answers 2

4

int is always less than str. Turn your header value into an int first. And remember, debug with repr(), not str().

Sign up to request clarification or add additional context in comments.

4 Comments

+1 first. what a netlag tonight... this didn't show up till after I posted.
Thank you guys - and so fast! It wasn't an int, lol - I'm so used to typed languages.
Python is typed. It's strongly, dynamically typed.
Yes, I meant statically typed, these are the things that hold me up sadly.
0

img_size is a string not an int:

>>> 30720 < '0'
True

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.