Skip to content

Loops and lemonade

October 26, 2009

In chapter 2 of the book, one of the exercises involved refining our coffee-bean-price-fetching program with a loop and a time delay so that we don’t overload the bean vendor’s site with price requests. We’re told the vendor updates the price every 15 minutes (900 seconds) and therefore we only need to check the price that often. Okay, no sweat. We also learn that we can use time.sleep() to do this.

My question was: where should this go?

Okay, here’s what I had at that point:

import urllib.request
import time
price = 99.99
while price > 4.74:
    page = urllib.request.urlopen(“[beansiteURL]”)
    text = page.read().decode(“utf8”)
    where = text.find(“>$”)
    price_start = where + 2
    price_end = price_start + 4
    price = float(text[price_start:price_end])
print(“Buy!”)

So, I needed to insert time.sleep(900), which would basically make my program kick its feet up and drink lemonade for 15 minutes.  But I didn’t want it to do that at the beginning of the loop — I wanted it to get the price right away.  And I didn’t want it to do that at the end of the loop — if it turned out the price of beans had dropped beneath my chosen threshold, I wanted to know about it right away, not after 15 minutes!

I asked a Python-proficient friend of mine about this, and his suggestion was to use a break.  This gives the program a way to exit the loop before pouring its iced beverage of choice if it finds a price I want to know about.  He also suggested a few other modifications to my code to make it a bit more elegant.  Instead of importing the entire urllib.request module, he said, it might be better to import just the required function.  Also, instead of arbitrarily setting price to 99.99 and depending on a garbage collector to get rid of that object later, I could use while 1 (while true) to get my loop started.

My resulting code looks like this:

from urllib.request import urlopen
import time
while 1:
    page = urlopen(“http://dl.getdropbox.com/u/194528/beans.html”)
    text = page.read().decode(“utf8”)
    where = text.find(“>$”)
    price_start = where + 2
    price_end = price_start + 4
    price = float(text[price_start:price_end])
    if price <= 4.74:
        break
    time.sleep(900)
print(“Buy!”)

Advertisements
4 Comments leave one →
  1. Yasmine permalink
    October 27, 2009 1:52 am

    I have some questions about this if you don’t mind:

    – Why are we using quotes and parenthesis for some commands and not others? We used them in Chapter 1 and now it seems like they’re appropriate in some cases and not appropriate for others.
    – Also, did it take 15 minutes for you to get the results when you ran the program? (I wasn’t patient enough to wait). I switched the sleep time to one minute just to test it and it was still running afterwards.

  2. October 27, 2009 7:38 am

    Good points on improving the code if you have a nice, friendly Python-guru to lean on.
    The “while 1:” trick is probably better written as “while True:” as I think the latter makes the code a little clearer (and we use it that way later in the book). Obviously, if you deliberately create an “infinite loop” (which “while True:” does here), you need to somehow exit the loop and the use of “break” is the classic technique (again, covered later by us).
    The adjustment to the import statement is a very “Pythonic” thing to do… and we tended to shy away from doing things in too much of a “Python Way”, concentrating instead on the core programming concepts. Also, with something this small, I wouldn’t worry too much about garbage collection…
    Now, having said all that, these are great comments and useful amendments (once you understand them). Cheers, Paul.

  3. January 10, 2010 7:57 pm

    Hello
    I just changed the 900 seconds to 10 🙂
    Nice to see someone else learning with HF Programming
    xFiona

Trackbacks

  1. More coffee anyone? « Julie Learns Python

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: