Skip to content

More coffee anyone?

November 1, 2009

Hey, thanks so much for all the great discussion and engagement.  It’s a huge encouragement to have experienced Pythonistas (and fellow n00bs!) join in the conversation.

Just a quick update on what I’ve been doing this weekend as I finish up Chapter 3 (which I started Monday night) and move through Chapter 4.

I mentioned in my last post that Chapter 3 continues to look at the coffee bean pricing example, and that it has us interacting with the Twitter API.  That piece of code was given to us as:

def send_to_twitter(msg):
    password_manager = urllib.request.HTTPPasswordMgr()
    password_manager.add_password(“Twitter API”,         “”, username, password)
    http_handler =         urllib.request.HTTPBasicAuthHandler(password_manager)
    page_opener = urllib.request.build_opener(http_handler)
    params = urllib.parse.urlencode( {‘status’: msg} )
    resp = urllib.request.urlopen(“
        update.json”, params)

I guess I basically understand what this does, but it wasn’t presented in detail.  One question it raised for me concerns the functions in the urllib.request library (or module, as I’m told is a Pythonic synonym).  You may remember an earlier post in which I consulted a Pythonista friend of mine who suggested the use of:

from urllib.request import urlopen

Well, looking at the send_to_twitter() function, I of course notice that we’re calling half a dozen different functions from the same module.  Surely it’s easier to import the whole module (in one line of code) than to import the functions individually (in six lines of code), yeah?  Where would the line be drawn on this Pythonic technique?  It’s okay to import two functions individually?  Three?  Opinions, please.

All that aside, we did learn to define our own functions (and to use these to avoid code duplication) as well as how to pass parameters to functions.  We also covered stack frames and local variables.  So far, so good.

Chapter 4 covers files and arrays.  Hooray!  To explore data structures, we left behind coffee beans for a more recreational example: Codeville’s annual “Surf-A-Thon”.  First we learned to read a text file stored on disk, and to do so line-by-line using a for loop.  Each line contained a name and a score, so we used split() to divide the strings and learned to create an array with names and scores.  Finally, we learned to use sort() and reverse() so that we could print the top three scores.  Chapter 5 will apparently show us how to retrieve and display the corresponding names.

Coincidentally, I’m currently editing a book on PHP and happened to read the chapter on arrays earlier this week.  So as I was working through Chapter 4 I had fresh in my mind certain details from the PHP chapter, and these structures seem to work in similar ways with Python.  One thing I wonder about: PHP distinguishes between indexed arrays (with numerical keys) and associative arrays (which allow strings as keys).  With the Surf-A-Thon example, we didn’t specify our keys but let Python auto-populate them for us by using append(), and Python chose numerical keys beginning with 0 (which PHP would do as well).  But I wonder whether Python also allows strings as keys, and whether it distinguishes arrays by key type.  I could go look it up (and might soon), but conversation is much more fun, so feel free to jump in on that.  🙂

Our group’s next meeting is tomorrow night (Monday again), and hopefully the rest of the group has also gotten through Chapter 4 so we can all start Chapter 5 together.  More updates soon!

8 Comments leave one →
  1. November 2, 2009 8:39 am

    “But I wonder whether Python also allows strings as keys, and whether it distinguishes arrays by key type.” … all will be revealed in Chapter 5. Keep up the great work, Julie! –Paul.

    • November 3, 2009 7:19 pm

      Thanks Paul! I really do appreciate the way the book moves through these ideas. It’s been a great introduction, and very literally thought-provoking. 🙂

      • November 4, 2009 5:32 am

        Thanks, Julie, for another great comment and complement… I’m being a little careful not to answer all of your questions as I’m interested in seeing how the group responds. I see Doug has you covered, though. [Psssst: Doug was a tech editor on hfprog]. Looking forward to your comments on Chapter 5 and beyond. Your blog will be a great resource for other readers of our book. –Paul.

  2. November 3, 2009 12:38 pm

    I usually stop importing individual functions or classes from a module when I need 3 or more. With the standard library I usually don’t import individual names at all because the names used there tend to be short and using the fully qualified name distinguishes my own local names from the ones in other modules.

  3. November 15, 2009 2:12 pm

    Julie – this is a terrific idea! Please count me in as a friend to help as required. I learned at last week’s NYC Python meetup that you have been very supportive to that group (and I suppose, vice versa). Hope to see you there on one of my NY trips.

  4. January 24, 2010 5:29 pm

    Hi there. I’m currently going through the same book, and I’m at the Twitter example you displayed above. However, when I try to run it, I get an error message telling me that http_handler is “invalid syntax”. I’ve checked the code several times and I’ve entered exactly what was given in the text. Have you run across this problem?

Leave a Reply

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

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

Google photo

You are commenting using your Google 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 )

Connecting to %s

%d bloggers like this: