Features of HTTP

There are five important features which all HTTP clients should support. The most important thing to understand about any type of web service is that network access is incredibly expensive. I don't mean dollars and cents expensive (although bandwidth ain't free). I mean that it takes an extraordinary long time to open a connection, send a request, and retrieve a response from a remote server. Even on the fastest broadband connection, latency (the time it takes to send a request and start...

O Installing on Mac OS X

All modern Macintosh computers use the Intel chip (like most Windows PCs). Older Macs used PowerPC chips. You don't need to understand the difference, because there's just one Mac Python installer for all Macs. Visit python.org download and download the Mac installer. It will be called something like Python 3.1 Mac Installer Disk Image, although the version number may vary. Be sure to download version 3.x, not 2.x. Your browser should automatically mount the disk image and open a Finder window...

Callable global function

In Python 2, you could check whether an object was callable like a function with the global callable function. In Python 3, this global function has been eliminated. To check whether an object is callable, check for the existence of the_call_ special method. In Python 2, the global zip function took any number of sequences and returned a list of tuples. The first tuple contained the first item from each sequence the second tuple contained the second item from each sequence and so on. In Python...

Halt And Catch Fire

It is not enough to test that functions succeed when given good input you must also test that they fail when given bad input. And not just any sort of failure they must fail in the way you expect. gt gt gt import roman1 gt gt gt roman1.to_roman 4000 'MMMM' gt gt gt roman1.to_roman 5000 'MMMMM' gt gt gt roman1.to_roman 9000 'MMMMMMMMM' 1. That's definitely not what you wanted that's not even a valid Roman numeral In fact, each of these numbers is outside the range of acceptable input, but the...

Diving In

Onvention dictates that I should bore you with the fundamental building blocks of programming, so we can slowly work up to building something useful. Let's skip all that. Here is a complete, working Python program. It probably makes absolutely no sense to you. Don't worry about that, because you're going to dissect it line by line. But read through it first and see what, if anything, you can make of it. SUFFIXES 1000 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB' , 1024 'KiB', 'MiB', 'GiB',...

Beyond Http Get

HTTP web services are not limited to GET requests. What if you want to create something new Whenever you post a comment on a discussion forum, update your weblog, publish your status on a microblogging service like Twitter or Identi.ca, you're probably already using HTTP POST. Both Twitter and Identi.ca both offer a simple HTTP-based API for publishing and updating your status in 140 characters or less. Let's look at Identi.ca's API documentation for updating your status Identi.ca rest api...

IntroducingTHe chardet Module

Before we set off porting the code, it would help if you understood how the code worked This is a brief guide to navigating the code itself. The chardet library is too large to include inline here, but you can download it from chardet.feedparser.org. universaldetector.py, which has one class, UniversalDetector. You might think the main entry point is the detect function in chardet _init_.py, but that's really just a convenience function that creates a UniversalDetector object, calls it, and...

Serializing Datatypes Unsupported by json

Even if JSON has no built-in support for bytes, that doesn't mean you can't serialize bytes objects. The json module provides extensibility hooks for encoding and decoding unknown datatypes. By unknown, I mean not defined in JSON. Obviously the json module knows about byte arrays, but it's constrained by the limitations of the JSON specification. If you want to encode bytes or other datatypes that JSON doesn't support natively, you need to provide custom encoders and decoders for those types....

Loading Data from a json File

Like the pickle module, the json module has a load function which takes a stream object, reads JSON-encoded data from it, and creates a new Python object that mirrors the JSON data structure. File lt stdin gt , line 1, in lt module gt NameError name 'entry' is not defined gt gt gt import json gt gt gt with open 'entry.json', 'r', encoding 'utf-8' as f entry json.load f 'internal_id' '_class_' 'bytes', '_value_' 222, 213, 180, 248 , 'title' 'Dive into history, 2009 edition', 'tags'...

List Comprehensions

List Python

A list comprehension provides a compact way of mapping a list into another list by applying a function to each of the elements of the list. gt gt gt elem 2 for elem in a_list gt gt gt a_list elem 2 for elem in a_list 1. To make sense of this, look at it from right to left. a_list is the list you're mapping. The Python interpreter loops through a_list one element at a time, temporarily assigning the value of each element to the variable elem. Python then applies the function elem 2 and appends...

Introducing httpl i b

Before you can use httplib2, you'll need to install it. Visit code.google.com p httplib2 and download the latest version. httplib2 is available for Python 2.x and Python 3.x make sure you get the Python 3 version, named something like httplib2-python3-0.5.0.zip. Unzip the archive, open a terminal window, and go to the newly created httplib2 directory. On Windows, open the Start menu, select Run , type cmd.exe and press ENTER. c Users pilgrim Downloads gt dir Volume in drive C has no label....