15.04.2013 Views

Core Python Programming (2nd Edition)

Core Python Programming (2nd Edition)

Core Python Programming (2nd Edition)

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

deltas = [ x[1]-x[0] for x in partition() ]<br />

print deltas<br />

print sum(deltas)<br />

[0.10271966686994982, 0.13826576491042208, 0.064146913555132801,<br />

0.11906452454467387, 0.10501198456091299, 0.011732423830768779,<br />

0.11785369256442912, 0.065927165520102249, 0.098351305878176198,<br />

0.077786747076205365, 0.099139810689226726]<br />

1.0<br />

Of course, the output will always be different since articles are always being posted. No two executions<br />

will result in the same output unless your news server has not been updated with another article since<br />

you last ran the script.<br />

Line-by-Line Explanation<br />

Lines 19<br />

This application starts with a few import statements and some constants, much like the FTP client<br />

example.<br />

Lines 1140<br />

In the first section, we attempt to connect to the NNTP host server and bail if it tails (lines 13-24). Line<br />

15 is commented out deliberately in case your server requires authentication (with login and password)if<br />

so, uncomment this line and edit it in with line 14. This is followed by trying to load up the specific<br />

newsgroup. Again, it will quit if that newsgroup does not exist, is not archived by this server, or if<br />

authentication is required (lines 26-40).<br />

Lines 4255<br />

In the next part we get some headers to display (lines 42-51). The ones that have the most meaning<br />

are the author, subject, and date. This data is retrieved and displayed to the user. Each call to the xhdr<br />

() method requires us to give the range of articles to extract the headers from. We are only interested<br />

in a single message, so the range is "X-X" where X is the last message number.<br />

xhdr() returns a 2-tuple consisting of a server response (rsp) and a list of the headers in the range we<br />

specify. Since we are only requesting this information for one message (the last one), we just take the<br />

first element of the list (hdr [0]). That data item is a 2-tuple consisting of the article number and the<br />

data string. Since we already know the article number (because we give it in our range request), we are<br />

only interested in the second item, the data string (hdr [0][1] ).<br />

The last part is to download the body of the article itself (lines 53-55). It consists of a call to the body()<br />

method, a display the first 20 or fewer meaningful lines (as defined at the beginning of this section), a<br />

logout of the server, and complete execution.<br />

Lines 5780<br />

The core piece of processing is done by the displayFirst20() function (lines 57-80). It takes the set of

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!