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 =“utf8”)
    where = text.find(“>$”)
    price_start = where + 2
    price_end = price_start + 4
    price = float(text[price_start:price_end])

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(“”)
    text =“utf8”)
    where = text.find(“>$”)
    price_start = where + 2
    price_end = price_start + 4
    price = float(text[price_start:price_end])
    if price <= 4.74:


Getting started

October 23, 2009

We had our first meeting last night, with 11 people gathering in person and another two or three (we think) following our Ustream channel online.

After the obligatory pizza and soda feast, during which we made sure everyone had downloaded Python 3.1.1 and successfully installed it, we started off with introductions.

It turns out that pretty much all of us have had some tangential interaction with programming at work, and are at least familiar with what code looks like.  One or two had solid programming experience but were new to Python, and several had dabbled in things like HTML/CSS, Ruby, or PHP/MySQL.  About half of us had made at least one false start at learning to code on our own (me included), and were eagerly hoping that this buddy system approach would provide the ongoing motivation necessary to attain whatever level of proficiency we’re each after — enough to write an iPhone app, to DIY instead of paying for a build, or to avoid those situations in which someone tells you something is impossible and you’re unable to prove them wrong.

Personally, my goal in learning to code is to be able to contribute to the swirling pool of innovation: not just my ideas or an introduction between people who should know each other but don’t, but something I made—something I put into action. Specifically, I’d like to be able to contribute to the work of the Sunlight Foundation, since I’ve long admired their efforts to improve data transparency and to further open government.

With these happy thoughts in mind, we dove into the book.

It worked rather well to have everyone reading through the pages and working the examples on their own, each at his or her own pace.  We tended to talk a lot, whether it was to ask each other questions or just to make jokes, and a few small groups tended to form here and there.  I found I really benefitted from having some more experienced folks in the room, since they were able to answer questions like “why isn’t there an ‘else’ statement here?” or “arg! why doesn’t it like my indentation?!”

Instead of starting with the stereotypical “Hello world” exercise, the book had us jump right into building a simple guessing game.  We started with a fixed integer and a conditional statement that would tell the player either “You win!” or “You lose!” depending on whether they input the correct number.  I had fun trying to break it (and succeeding) by guessing “bite me” (which returned a type error).

After that, we improved the game by adding a loop and some more helpful feedback for the player, such as “Too high” and “Too low”.  Finally, we learned to import a random integer so that not even we knew the answer ahead of time.  (Several of us played with different ranges of integers, but 1-20 seemed about the largest we could tolerate without getting bored by the game before we’d finished it.  I would have liked to try spicing it up a little by adding more colorful feedback for certain guesses, but  we had concluded Chapter 1 and we on to bigger and better things.)

Chapter 2 saw us retrieving the price of coffee beans from a dummy website.  The URL from the book wasn’t up yet, so it was a good thing we had some HTML jockeys in the house; Charlie was able to throw up an appropriate page for us to request and read.  We used urllib.request to read the page, then refined it by reading only a substring as defined by offset.  Of course this technique isn’t preferable, so we then further refined things by using .find() to locate the dollar sign and read the desired substring after that.  Adding a loop and employing the float() function then allowed us to monitor the price of coffee until it fell below a desired level.  In order to avoid crashing the server, we then got to learn about the time library.

At this point, most folks had to get going, so we closed down for the evening.  Not everyone ended in the same place, but it seemed clear that we were all following along well and are capable of working independently, so we agreed to be through Chapter 3 by the time we meet again on Monday evening.  Two together, one apart seems like a good chapter pace, and with weekly meetings will get us through the book in about a month.

After that, we’ll probably choose another book to help us get our hands a little deeper into the nitty-gritty of things… suggestions welcome.

Technically speaking…

October 23, 2009

So I’ve given the header on the blog a bit of a facelift. The intention is to clarify that, rather than attempting to learn the detailed ins and outs of Python just yet, the real goal of this quest is to learn to program. Python is a convenient (and fun!) language to start with. But I’m after concepts more than specific library listings, and that’s part of why I wanted to start with the Head First Programming book (as noted in the introduction, it’s intentionally not called Head First Python).

And so far, so good.  Update to come.

And we’re off…

October 22, 2009

Okay, so the October 19 meeting turned into a prep session, but now we’ve got a game plan and tonight is our first in-person meeting with the group.

That game plan includes:

  • Using Head First Programming by David Griffiths and Paul Barry as our text. We have an inside line on this, so are getting access to a pre-release version. Very cool. But if you want to follow along with us, check out the Rough Cut version in Safari or wait a few weeks for the book release.
  • Using GitHub to collaborate.  Mercurial is the official Python repository, but lots of folks are already familiar with Git so we’re going with it.
  • Using Python 3.1.1 to learn. This is the newest and spiffiest release.

Okay, more news to come later tonight!

Hello world!

October 1, 2009

The learning of the code is scheduled to commence October 19!

Until then, check out Charlie’s blog post and leave us suggestions for projects and tools.