skeleton of advanced iterators chapter

This commit is contained in:
Mark Pilgrim
2009-03-29 18:35:36 -05:00
parent 62b848996c
commit 30574286e9
5 changed files with 52 additions and 45 deletions
+27
View File
@@ -0,0 +1,27 @@
<!DOCTYPE html>
<head>
<meta charset=utf-8>
<title>Advanced Iterators - Dive into Python 3</title>
<link rel=stylesheet type=text/css href=dip3.css>
<style>
body{counter-reset:h1 6}
</style>
</head>
<form action=http://www.google.com/cse><div><input type=hidden name=cx value=014021643941856155761:l5eihuescdw><input type=hidden name=ie value=UTF-8>&nbsp;<input name=q size=31>&nbsp;<input type=submit name=sa value=Search></div></form>
<p>You are here: <a href=index.html>Home</a> <span>&#8227;</span> <a href=table-of-contents.html#advanced-iterators>Dive Into Python 3</a> <span>&#8227;</span>
<h1>Iterators <i class=baa>&amp;</i> Generators</h1>
<blockquote class=q>
<p><span>&#x275D;</span> FIXME <span>&#x275E;</span><br>&mdash; FIXME
</blockquote>
<p id=toc>&nbsp;
<h2 id=divingin>Diving In</h2>
<p class=f>FIXME
<h2 id=furtherreading>Further Reading</h2>
<ul>
</ul>
<p class=c>&copy; 2001&ndash;9 <a href=about.html><span>&#x2133;</span>ark Pilgrim</a>
<script src=jquery.js></script>
<script src=dip3.js></script>
+2 -2
View File
@@ -27,13 +27,13 @@ h1:before{content:""}
<li><a href=native-datatypes.html>Native Datatypes</a>
<li><a href=strings.html>Strings</a>
<li><a href=regular-expressions.html>Regular Expressions</a>
<li class=todo>The power of introspection
<li><a href=iterators-and-generators.html>Iterators <i class=baa>&amp;</i> Generators</a>
<li><a href=advanced-iterators.html>Advanced Iterators</a>
<li class=todo>Objects and object-orientation
<li><a href=unit-testing.html>Unit Testing</a>
<li class=todo>Test-first programming
<li class=todo>Refactoring your code
<li class=todo>Files
<li><a href=iterators-and-generators.html>Iterators <i class=baa>&amp;</i> Generators</a>
<li class=todo>HTML processing
<li class=todo>XML processing
<li class=todo>Web services
+4 -4
View File
@@ -4,7 +4,7 @@
<title>Iterators &amp; generators - Dive into Python 3</title>
<link rel=stylesheet type=text/css href=dip3.css>
<style>
body{counter-reset:h1 11}
body{counter-reset:h1 5}
</style>
</head>
<form action=http://www.google.com/cse><div><input type=hidden name=cx value=014021643941856155761:l5eihuescdw><input type=hidden name=ie value=UTF-8>&nbsp;<input name=q size=31>&nbsp;<input type=submit name=sa value=Search></div></form>
@@ -15,9 +15,8 @@ body{counter-reset:h1 11}
</blockquote>
<p id=toc>&nbsp;
<h2 id=divingin>Diving In</h2>
<p class=f>Let&#8217;s talk about plural nouns. Also, functions that return other functions, advanced regular expressions, iterators, and generators. But first, let&#8217;s talk about how to make plural nouns. (If you haven&#8217;t read <a href=regular-expressions.html>the chapter on regular expressions</a>, now would be a good time. This chapter assumes you understand the basics of regular expressions, and quickly descends into more advanced uses.)
<p>English is a schizophrenic language that borrows from a lot of other languages, and the rules for making singular nouns into plural nouns are varied and complex. There are rules, and then there are exceptions to those rules, and then there are exceptions to the exceptions.
<aside>English is a schizophrenic language.</aside>
<p class=f>English is a schizophrenic language that borrows words from many other languages. The most basic linguistic operations, like taking a singular noun and turning it into a plural noun, are complicated by the language's mixed heritage. There are rules, and then there are exceptions to those rules, and then there are exceptions to the exceptions.
<p>In this chapter, you&#8217;re going to learn about about plural nouns. Also, functions that return other functions, advanced regular expressions, iterators, and generators. But first, let&#8217;s talk about how to make plural nouns. (If you haven&#8217;t read <a href=regular-expressions.html>the chapter on regular expressions</a>, now would be a good time. This chapter assumes you understand the basics of regular expressions, and it quickly descends into more advanced uses.)
<p>If you grew up in an English-speaking country or learned English in a formal school setting, you&#8217;re probably familiar with the basic rules:
<ul>
<li>If a word ends in S, X, or Z, add ES. <i>Bass</i> becomes <i>basses</i>, <i>fax</i> becomes <i>faxes</i>, and <i>waltz</i> becomes <i>waltzes</i>.
@@ -250,6 +249,7 @@ $ $ s</code></pre>
<p>Now let&#8217;s see how you can use this rules file.
<p>[FIXME: now that this chapter comes before the I/O chapter, need to at least mention what open() does]
<p class=d>[<a href=examples/plural4.py>download <code>plural4.py</code></a>]
<pre><code>import re
+17 -36
View File
@@ -118,28 +118,28 @@ ul li ol{margin:0;padding:0 0 0 2.5em}
<li><a href=regular-expressions.html#phonenumbers>Case study: parsing phone numbers</a>
<li><a href=regular-expressions.html#summary>Summary</a>
</ol>
<li>The power of introspection
<li id=iterators-and-generators><a href=iterators-and-generators.html>Iterators <i class=baa>&amp;</i> generators</a>
<ol>
<li>Diving in
<li>Using optional and named arguments
<li><a href=iterators-and-generators.html#divingin>Diving in</a>
<li><a href=iterators-and-generators.html#i-know>I know, let&#8217;s use regular expressions!</a>
<li><a href=iterators-and-generators.html#a-list-of-functions>A list of functions</a>
<li><a href=iterators-and-generators.html#a-list-of-patterns>A list of patterns</a>
<li><a href=iterators-and-generators.html#a-file-of-patterns>A file of patterns</a>
<li><a href=iterators-and-generators.html#generators>Generators</a>
<ol>
<li>Keyword-only arguments
<li><a href=iterators-and-generators.html#a-fibonacci-generator>A Fibonacci generator</a>
<li><a href=iterators-and-generators.html#a-plural-rule-generator>A plural rule generator</a>
</ol>
<li>Using type, str, dir, and other built-in functions
<li><a href=iterators-and-generators.html#iterators>Iterators</a>
<ol>
<li>The type function
<li>The str function
<li>Built-in functions
<li><a href=iterators-and-generators.html#a-fibonacci-iterator>A Fibonacci iterator</a>
<li><a href=iterators-and-generators.html#a-plural-rule-iterator>A plural rule iterator</a>
</ol>
<li>Getting object references with getattr
<ol>
<li>getattr with modules
<li>getattr as a dispatcher
</ol>
<li>Filtering lists
<li>Using lambda functions
<li>Putting it all together
<li>Summary
<li><a href=iterators-and-generators.html#furtherreading>Further reading</a>
</ol>
<li><a href=advanced-iterators.html>Advanced Iterators</a>
<ol>
<li><a href=advanced-iterators.html#divingin>Diving in</a>
</ol>
<li>Objects and object-orientation
<ol>
@@ -178,25 +178,6 @@ ul li ol{margin:0;padding:0 0 0 2.5em}
<li>Handling errors (exceptions)
<li>Writing to files
</ol>
<li id=iterators-and-generators><a href=iterators-and-generators.html>Iterators <i class=baa>&amp;</i> generators</a>
<ol>
<li><a href=iterators-and-generators.html#divingin>Diving in</a>
<li><a href=iterators-and-generators.html#i-know>I know, let&#8217;s use regular expressions!</a>
<li><a href=iterators-and-generators.html#a-list-of-functions>A list of functions</a>
<li><a href=iterators-and-generators.html#a-list-of-patterns>A list of patterns</a>
<li><a href=iterators-and-generators.html#a-file-of-patterns>A file of patterns</a>
<li><a href=iterators-and-generators.html#generators>Generators</a>
<ol>
<li><a href=iterators-and-generators.html#a-fibonacci-generator>A Fibonacci generator</a>
<li><a href=iterators-and-generators.html#a-plural-rule-generator>A plural rule generator</a>
</ol>
<li><a href=iterators-and-generators.html#iterators>Iterators</a>
<ol>
<li><a href=iterators-and-generators.html#a-fibonacci-iterator>A Fibonacci iterator</a>
<li><a href=iterators-and-generators.html#a-plural-rule-iterator>A plural rule iterator</a>
</ol>
<li><a href=iterators-and-generators.html#furtherreading>Further reading</a>
</ol>
<li>HTML processing
<ol>
<li>Diving in
+2 -3
View File
@@ -4,7 +4,7 @@
<title>Unit testing - Dive into Python 3</title>
<link rel=stylesheet type=text/css href=dip3.css>
<style>
body{counter-reset:h1 7}
body{counter-reset:h1 8}
</style>
</head>
<form action=http://www.google.com/cse><div><input type=hidden name=cx value=014021643941856155761:l5eihuescdw><input type=hidden name=ie value=UTF-8>&nbsp;<input name=q size=31>&nbsp;<input type=submit name=root value=Search></div></form>
@@ -15,8 +15,7 @@ body{counter-reset:h1 7}
</blockquote>
<p id=toc>&nbsp;
<h2 id=divingin>(Not) Diving In</h2>
<p class=f>How do you know that the code you wrote yesterday still works after the changes you made today? Every seasoned programmer has war stories of an &#8220;innocent&#8221; change that couldn't <em>possibly</em> have affected that other &#8220;unrelated&#8221; module&hellip; If this sounds familiar, this chapter is for you.
<p>In this chapter, you're going to write and debug a set of utility functions to convert to and from Roman numerals. You saw the mechanics of constructing and validating Roman numerals in <a href="regular-expressions.html#romannumerals">&#8220;Case study: roman numerals&#8221;</a>. Now step back and consider what it would take to expand that into a two-way utility.
<p class=f>In this chapter, you're going to write and debug a set of utility functions to convert to and from Roman numerals. You saw the mechanics of constructing and validating Roman numerals in <a href="regular-expressions.html#romannumerals">&#8220;Case study: roman numerals&#8221;</a>. Now step back and consider what it would take to expand that into a two-way utility.
<p><a href="regular-expressions.html#romannumerals">The rules for Roman numerals</a> lead to a number of interesting observations:
<ol>
<li>There is only one correct way to represent a particular number as Roman numerals.