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...

Fixing What to Cant

Now for the real test running the test harness against the test suite. Since the test suite is designed to cover all the possible code paths, it's a good way to test our ported code to make sure there aren't any bugs lurking anywhere. C home chardet> python test.py tests * * Traceback (most recent call last) File test.py, line 1, in < module> from chardet.universaldetector import UniversalDetector File line 51 self.done constants.False Hmm, a small snag. In Python 3, False is a reserved...

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...

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...

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....