mirror of
https://github.com/kennethreitz/dive-into-python3.git
synced 2026-06-05 23:10:17 +00:00
generate in-page TOCs on demand
This commit is contained in:
@@ -18,36 +18,7 @@ mark{background:#ff8;font-weight:bold}
|
||||
<blockquote class=q>
|
||||
<p><span>❝</span> Words, words. They’re all we have to go on. <span>❞</span><br>— <cite>Rosencrantz and Guildenstern are Dead</cite>
|
||||
</blockquote>
|
||||
<ol>
|
||||
<li><a href=#divingin>Diving in</a>
|
||||
<li><a href=#faq.what>What is character encoding auto-detection?</h2>
|
||||
<ol>
|
||||
<li><a href=#faq.impossible>Isn’t that impossible?</a>
|
||||
<li><a href=#faq.who>Does such an algorithm exist?</a>
|
||||
</ol>
|
||||
<li><a href=#divingin2>Diving in</a>
|
||||
<ol>
|
||||
<li><a href=#how.bom><code>UTF-n</code> with a <abbr title="Byte Order Mark">BOM</abbr></a>
|
||||
<li><a href=#how.esc>Escaped encodings</a>
|
||||
<li><a href=#how.mb>Multi-byte encodings</a>
|
||||
<li><a href=#how.sb>Single-byte encodings</a>
|
||||
<li><a href=#how.windows1252><code>windows-1252</code></a>
|
||||
</ol>
|
||||
<li><a href=#running2to3>Running <code>2to3</code></a>
|
||||
<li><a href=#manual>Fixing what <code>2to3</code> can’t</a>
|
||||
<ol>
|
||||
<li><a href=#falseisinvalidsyntax><code>False</code> is invalid syntax</a>
|
||||
<li><a href=#nomodulenamedconstants>No module named <code>constants</code></a>
|
||||
<li><a href=#namefileisnotdefined>Name <var>'file'</var> is not defined</a>
|
||||
<li><a href=#cantuseastringpattern>Can’t use a string pattern on a bytes-like object</a>
|
||||
<li><a href=#cantconvertbytesobject>Can’t convert <code>'bytes'</code> object to <code>str</code> implicitly</a>
|
||||
<li><a href=#unsupportedoperandtypeforplus>Unsupported operand type(s) for +: <code>'int'</code> and <code>'bytes'</code></a>
|
||||
<li><a href=#ordexpectedstring><code>ord()</code> expected string of length 1, but <code>int</code> found</a>
|
||||
<li><a href=#unorderabletypes>Unorderable types: <code>int()</code> >= <code>str()</code></a>
|
||||
<li><a href=#reduceisnotdefined>Global name <code>'reduce'</code> is not defined</a>
|
||||
</ol>
|
||||
<li><a href=#summary>Summary</a>
|
||||
</ol>
|
||||
<p id=toc>
|
||||
<h2 id=divingin>Diving in</h2>
|
||||
<p class=f>Unknown or incorrect character encoding is the #1 cause of gibberish text on the web, in your inbox, and indeed across every computer system ever written. In <a href=strings.html>Chapter 3</a>, I talked about the history of character encoding and the creation of Unicode, the “one encoding to rule them all.” I’d love it if I never had to see a gibberish character on a web page again, because all authoring systems stored accurate encoding information, all transfer protocols were Unicode-aware, and every system that handled text maintained perfect fidelity when converting between encodings.
|
||||
<p>I’d also like a pony.
|
||||
|
||||
@@ -16,6 +16,8 @@ $(document).ready(function() {
|
||||
}
|
||||
*/
|
||||
|
||||
$("#toc").html('<a href="javascript:showTOC()">table of contents</a>');
|
||||
|
||||
// "hide", "open in new window", and (optionally) "download" widgets on code & screen blocks
|
||||
$("pre > code").each(function(i) {
|
||||
var pre = $(this.parentNode);
|
||||
@@ -81,3 +83,23 @@ function plainTextOnClick(id) {
|
||||
win.document.write('<pre>' + clone.html());
|
||||
win.document.close();
|
||||
}
|
||||
|
||||
function showTOC() {
|
||||
var toc = '';
|
||||
var old_level = 1;
|
||||
$('h2,h3').each(function(i, h) {
|
||||
level = parseInt(h.tagName.substring(1));
|
||||
if (level < old_level) {
|
||||
toc += '</ol>';
|
||||
} else if (level > old_level) {
|
||||
toc += '<ol>';
|
||||
}
|
||||
toc += '<li><a href=#' + h.id + '>' + h.innerHTML + '</a>';
|
||||
old_level = level;
|
||||
});
|
||||
while (level > 1) {
|
||||
toc += '</ol>';
|
||||
level -= 1;
|
||||
}
|
||||
$("#toc").html(toc);
|
||||
}
|
||||
|
||||
+1
-42
@@ -13,48 +13,7 @@ body{counter-reset:h1 2}
|
||||
<blockquote class=q>
|
||||
<p><span>❝</span> Wonder is the foundation of all philosophy, research its progress, ignorance its end. <span>❞</span><br>— <cite>Michel de Montaigne</cite>
|
||||
</blockquote>
|
||||
<ol>
|
||||
<li><a href=#divingin>Diving in</a>
|
||||
<li><a href=#booleans>Booleans</a>
|
||||
<li><a href=#numbers>Numbers</a>
|
||||
<ol>
|
||||
<li><a href=#number-coercion>Coercing integers to floats and vice-versa</a>
|
||||
<li><a href=#common-numerical-operations>Common numerical operations</a>
|
||||
<li><a href=#fractions>Fractions</a>
|
||||
<li><a href=#trig>Trigonometry</a>
|
||||
<li><a href=#numbers-in-a-boolean-context>Numbers in a boolean context</a>
|
||||
</ol>
|
||||
<li><a href=#lists>Lists</a>
|
||||
<ol>
|
||||
<li><a href=#creatinglists>Creating a list</a>
|
||||
<li><a href=#slicinglists>Slicing a list</a>
|
||||
<li><a href=#extendinglists>Adding items to a list</a>
|
||||
<li><a href=#searchinglists>Searching for values in a list</a>
|
||||
<li><a href=#lists-in-a-boolean-context>Lists in a boolean context</a>
|
||||
</ol>
|
||||
<!--
|
||||
<li><a href=#sets>Sets</a>
|
||||
<ol>
|
||||
<li>Creating a new set
|
||||
<li>Modifying a set
|
||||
<li>Deleting items from a set
|
||||
<li>Common operations on sets (union, intersection, and difference)
|
||||
<li>Frozen sets
|
||||
</ol>
|
||||
-->
|
||||
<li><a href=#dictionaries>Dictionaries</a>
|
||||
<ol>
|
||||
<li><a href=#creating-dictionaries>Creating a dictionary</a>
|
||||
<li><a href=#modifying-dictionaries>Modifying a dictionary</a>
|
||||
<li><a href=#mixed-value-dictionaries>Mixed-value dictionaries</a>
|
||||
<li><a href=#dictionaries-in-a-boolean-context>Dictionaries in a boolean context</a>
|
||||
</ol>
|
||||
<li><a href=#none><code>None</code></a>
|
||||
<ol>
|
||||
<li><a href=#none-in-a-boolean-context><code>None</code> in a boolean context</a>
|
||||
</ol>
|
||||
<li><a href=#furtherreading>Further reading</a>
|
||||
</ol>
|
||||
<p id=toc>
|
||||
<h2 id=divingin>Diving in</h2>
|
||||
<p class=f>Cast aside <a href=your-first-python-program.html>your first Python program</a> for just a minute, and let's talk about datatypes. In Python, <a href=your-first-python-program.html#datatypes>every variable has a datatype</a>, but you don't need to declare it explicitly. Based on each variable's original assignment, Python figures out what type it is and keeps tracks of that internally.
|
||||
<p>Python has many native datatypes. Here are the important ones:
|
||||
|
||||
@@ -23,64 +23,7 @@ td pre{padding:0;border:0}
|
||||
<blockquote class=q>
|
||||
<p><span>❝</span> Life is pleasant. Death is peaceful. It’s the transition that’s troublesome. <span>❞</span><br>— Isaac Asimov (attributed)
|
||||
</blockquote>
|
||||
<ol>
|
||||
<li><a href=#divingin>Diving in</a>
|
||||
<li><a href=#print><code>print</code> statement</a>
|
||||
<li><a href=#unicodeliteral>Unicode string literals</a>
|
||||
<li><a href=#unicode><code>unicode()</code> global function</a>
|
||||
<li><a href=#long><code>long</code> data type</a>
|
||||
<li><a href=#ne><> comparison</a>
|
||||
<li><a href=#has_key><code>has_key()</code> dictionary method</a>
|
||||
<li><a href=#dict>Dictionary methods that return lists</a>
|
||||
<li><a href=#imports>Modules that have been renamed or reorganized</a>
|
||||
<ol>
|
||||
<li><a href=#http><code>http</code></a>
|
||||
<li><a href=#urllib><code>urllib</code></a>
|
||||
<li><a href=#dbm><code>dbm</code></a>
|
||||
<li><a href=#xmlrpc><code>xmlrpc</code></a>
|
||||
<li><a href=#othermodules>Other modules</a>
|
||||
</ol>
|
||||
<li><a href=#import>Relative imports within a package</a>
|
||||
<li><a href=#next><code>next()</code> iterator method</a>
|
||||
<li><a href=#filter><code>filter()</code> global function</a>
|
||||
<li><a href=#map><code>map()</code> global function</a>
|
||||
<li><a href=#reduce><code>reduce()</code> global function</a> (3.1+)
|
||||
<li><a href=#apply><code>apply()</code> global function</a>
|
||||
<li><a href=#intern><code>intern()</code> global function</a>
|
||||
<li><a href=#exec><code>exec</code> statement</a>
|
||||
<li><a href=#execfile><code>execfile</code> statement</a> (3.1+)
|
||||
<li><a href=#repr><code>repr</code> literals (backticks)</a>
|
||||
<li><a href=#except><code>try...except</code> statement</a>
|
||||
<li><a href=#raise><code>raise</code> statement</a>
|
||||
<li><a href=#throw><code>throw</code> method on generators</a>
|
||||
<li><a href=#xrange><code>xrange()</code> global function</a>
|
||||
<li><a href=#raw_input><code>raw_input()</code> and <code>input()</code> global functions</a>
|
||||
<li><a href=#funcattrs><code>func_*</code> function attributes</a>
|
||||
<li><a href=#xreadlines><code>xreadlines()</code> I/O method</a>
|
||||
<li><a href=#tuple_params><code>lambda</code> functions with multiple parameters</a>
|
||||
<li><a href=#methodattrs>Special method attributes</a>
|
||||
<li><a href=#nonzero><code>__nonzero__</code> special class attribute</a>
|
||||
<li><a href=#numliterals>Octal literals</a>
|
||||
<li><a href=#renames><code>sys.maxint</code></a>
|
||||
<li><a href=#callable><code>callable()</code> global function</a>
|
||||
<li><a href=#zip><code>zip()</code> global function</a>
|
||||
<li><a href=#standarderror><code>StandardError()</code> exception</a>
|
||||
<li><a href=#types><code>types</code> module constants</a>
|
||||
<li><a href=#isinstance><code>isinstance()</code> global function</a> (3.1+)
|
||||
<li><a href=#basestring><code>basestring</code> datatype</a>
|
||||
<li><a href=#itertools><code>itertools</code> module</a>
|
||||
<li><a href=#sys_exc><code>sys.exc_type</code>, <code>sys.exc_value</code>, <code>sys.exc_traceback</code></a>
|
||||
<li><a href=#paren>List comprehensions over tuples</a>
|
||||
<li><a href=#getcwdu><code>os.getcwdu()</code> function</a>
|
||||
<li><a href=#metaclass>Metaclasses</a>
|
||||
<li><a href=#nitpick>Matters of style</a>
|
||||
<ol>
|
||||
<li><a href=#set_literal><code>set()</code> literals</a>
|
||||
<li><a href=#buffer><code>buffer()</code> global function</a>
|
||||
<li><a href=#wscomma>Whitespace around commas</a>
|
||||
<li><a href=#idioms>Common idioms</a>
|
||||
</ol>
|
||||
</ol>
|
||||
<p id=toc>
|
||||
<h2 id=divingin>Diving in</h2>
|
||||
<p class=f>Virtually all Python 2 programs will need at least some tweaking to run properly under Python 3. To help with this transition, Python 3 comes with a utility script called <code>2to3</code>, which takes your actual Python 2 source code as input and auto-converts as much as it can to Python 3. <a href=case-study-porting-chardet-to-python-3.html#running2to3>Case study: porting <code>chardet</code> to Python 3</a> describes how to run the <code>2to3</code> script, then shows some things it can't fix automatically. This appendix documents what it <em>can</em> fix automatically.
|
||||
<h2 id=print><code>print</code> statement</h2>
|
||||
|
||||
@@ -13,22 +13,7 @@ body{counter-reset:h1 4}
|
||||
<blockquote class=q>
|
||||
<p><span>❝</span> Some people, when confronted with a problem, think “I know, I’ll use regular expressions.” Now they have two problems. <span>❞</span><br>— <cite>Jamie Zawinski</cite>
|
||||
</blockquote>
|
||||
<ol>
|
||||
<li><a href=#divingin>Diving in</a>
|
||||
<li><a href=#streetaddresses>Case study: street addresses</a>
|
||||
<li><a href=#romannumerals>Case study: Roman numerals</a>
|
||||
<ol>
|
||||
<li><a href=#thousands>Checking for thousands</a>
|
||||
<li><a href=#hundreds>Checking for hundreds</a>
|
||||
</ol>
|
||||
<li><a href=#nmsyntax>Using the <code>{n,m}</code> Syntax</a>
|
||||
<ol>
|
||||
<li><a href=#tensandones>Checking for tens and ones</a>
|
||||
</ol>
|
||||
<li><a href=#verbosere>Verbose regular expressions</a>
|
||||
<li><a href=#phonenumbers>Case study: parsing phone numbers</a>
|
||||
<li><a href=#summary>Summary</a>
|
||||
</ol>
|
||||
<p id=toc>
|
||||
<h2 id=divingin>Diving in</h2>
|
||||
<p class=f>Every modern programming language has built-in functions for working with strings. In Python, strings have methods for searching and replacing: <code>index()</code>, <code>find()</code>, <code>split()</code>, <code>count()</code>, <code>replace()</code>, <i class=baa>&</i>c. But these methods are limited to the simplest of cases. For example, the <code>index()</code> method looks for a single, hard-coded substring, and the search is always case-sensitive. To do case-insensitive searches of a string <var>s</var>, you must call <code>s.lower()</code> or <code>s.upper()</code> and make sure your search strings are the appropriate case to match. The <code>replace()</code> and <code>split()</code> methods have the same limitations.
|
||||
<p>If your goal can be accomplished with string methods, you should use them. They’re fast and simple and easy to read, and there’s a lot to be said for fast, simple, readable code. But if you find yourself using a lot of different string functions with <code>if</code> statements to handle special cases, or if you’re chaining calls to <code>split()</code> and <code>join()</code> to slice-and-dice your strings, you may need to move up to regular expressions.
|
||||
|
||||
+1
-15
@@ -14,21 +14,7 @@ body{counter-reset:h1 3}
|
||||
<p><span>❝</span> I’m telling you this ’cause you’re one of my friends.<br>
|
||||
My alphabet starts where your alphabet ends! <span>❞</span><br>— <cite>Dr. Seuss, On Beyond Zebra!</cite>
|
||||
</blockquote>
|
||||
<ol>
|
||||
<li><a href=#divingin>Diving in</a>
|
||||
<li><a href=#one-ring-to-rule-them-all>Unicode</a>
|
||||
<ol>
|
||||
<li>How strings are stored in memory
|
||||
<li>Converting between different character encodings
|
||||
<li><a href=#py-encoding>Specifying character encoding in <code>.py</code> files</a>
|
||||
</ol>
|
||||
<li>Strings in Python 3
|
||||
<li>Common string operations
|
||||
<li>Formatting strings
|
||||
<li><a href=#string-module>The <code>string</code> module</a>
|
||||
<li><a href=#byte-arrays>Strings vs. bytes</a>
|
||||
<li><a href=#furtherreading>Further reading</a>
|
||||
</ol>
|
||||
<p id=toc>
|
||||
<h2 id=divingin>Diving in</h2>
|
||||
<p class=f>Chinese has thousands of characters. The <a href="http://en.wikipedia.org/wiki/Rotokas_alphabet">Rotokas alphabet</a> of <a href="http://en.wikipedia.org/wiki/Bougainville_Province">Bougainville</a> is the smallest alphabet in the world, with just 12 letters. English has 26, plus a handful of punctuation marks. Python 3 can handle all of these languages, and more.
|
||||
|
||||
|
||||
+1
-7
@@ -13,13 +13,7 @@ body{counter-reset:h1 7}
|
||||
<blockquote class=q>
|
||||
<p><span>❝</span> Certitude is not the test of certainty. We have been cocksure of many things that were not so. <span>❞</span><br>— <cite>Oliver Wendell Holmes, Jr.</cite>
|
||||
</blockquote>
|
||||
<ol>
|
||||
<li><a href=#divingin>(Not) diving in</a>
|
||||
<li><a href=#romantest1>A single question</a>
|
||||
<li><a href=#romantest2>“Halt and catch fire”</a>
|
||||
<li><a href=#romantest3>More halting, more fire</a>
|
||||
<li>...
|
||||
</ol>
|
||||
<p id=toc>
|
||||
<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 “innocent” change that couldn't <em>possibly</em> have affected that other “unrelated” module… 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">“Case study: roman numerals”</a>. Now step back and consider what it would take to expand that into a two-way utility.
|
||||
|
||||
@@ -14,27 +14,7 @@ th{font-family:inherit !important}
|
||||
<blockquote class=q>
|
||||
<p><span>❝</span> Don’t bury your burden in saintly silence. You have a problem? Great. Rejoice, dive in, and investigate. <span>❞</span><br>— <cite>Ven. Henepola Gunararatana</cite>
|
||||
</blockquote>
|
||||
<ol>
|
||||
<li><a href=#divingin>Diving in</a>
|
||||
<li><a href=#declaringfunctions>Declaring functions</a>
|
||||
<ol>
|
||||
<li><a href=#datatypes>How Python's datatypes compare to other programming languages</a>
|
||||
</ol>
|
||||
<li><a href=#readability>Writing readable code</a>
|
||||
<ol>
|
||||
<li><a href=#docstrings>Docstrings</a>
|
||||
<li><a href=#functionannotations>Function annotations</a>
|
||||
<li><a href=#styleconventions>Style conventions</a>
|
||||
</ol>
|
||||
<li><a href=#everythingisanobject>Everything is an object</a>
|
||||
<ol>
|
||||
<li><a href=#importsearchpath>The <code>import</code> search path</a>
|
||||
<li><a href=#whatsanobject>What's an object?</a>
|
||||
</ol>
|
||||
<li><a href=#indentingcode>Indenting code</a>
|
||||
<li><a href=#runningscripts>Running scripts</a>
|
||||
<li><a href=#furtherreading>Further reading</a>
|
||||
</ol>
|
||||
<p id=toc>
|
||||
<h2 id=divingin>Diving in</h2>
|
||||
<p class=f>Books about programming usually start with a bunch of boring chapters about fundamentals and eventually 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.
|
||||
<p id=noscript>[The code examples will be easier to follow if you enable Javascript, but whatever.]
|
||||
|
||||
Reference in New Issue
Block a user