8000 Merge pull request #119 from eah13/console-merge · StudyCourse/pythonlearn@ac64580 · GitHub
[go: up one dir, main page]

Skip to content

Commit ac64580

Browse files
committed
Merge pull request csev#119 from eah13/console-merge
Trinket and Zip build scripts
2 parents fb2c413 + fb86bc7 commit ac64580

File tree

183 files changed

+214366
-284
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

183 files changed

+214366
-284
lines changed

book/01-intro.mkd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
% Python for Everybody
2-
% Exploring Information using Python 3.0
2+
% Exploring Data using Python 3
33
% Charles R. Severance
44

55
Why should you learn to write programs?

book/03-conditional.mkd

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,12 @@ true or false. The following examples use the operator `==`,
1414
which compares two operands and produces `True` if they are
1515
equal and `False` otherwise:
1616

17-
~~~~ {.python}
17+
~~~~ {.python .trinket}
1818
>>> 5 == 5
1919
True
2020
>>> 5 == 6
2121
False
22+
{}
2223
~~~~
2324

2425
`True` and `False` are special values that belong
@@ -110,7 +111,7 @@ Conditional execution
110111
\index{statement!conditional}
111112
\index{if statement}
112113
\index{statement!if}
113-
\index{conditional execution}
114+
\index{conditional executions}
114115

115116
In order to write useful programs, we almost always need the ability to
116117
check conditions and change the behavior of the program accordingly.

book/06-strings.mkd

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ A string is a sequence
1212
A string is a *sequence* of characters. You can access
1313
the characters one at a time with the bracket operator:
1414

15-
>>> fruit = 'banana'
16-
>>> letter = fruit[1]
15+
~~~~ {.python}
16+
>>> fruit = 'banana'
17+
>>> letter = fruit[1]
18+
~~~~
1719

1820
\index{index}
1921
\index{}

book/09-dictionaries.mkd

Lines changed: 69 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -29,34 +29,39 @@ avoid using it as a variable name.
2929
\index{dict function}
3030
\index{function!dict}
3131

32-
>>> eng2sp = dict()
33-
>>> print(eng2sp)
34-
{}
32+
~~~~ {.python .trinket}
33+
>>> eng2sp = dict()
34+
>>> print(eng2sp)
35+
{}
36+
~~~~
3537

3638
The curly brackets, `{}`, represent an empty dictionary. To add items to
3739
the dictionary, you can use square brackets:
3840

3941
\index{squiggly bracket}
4042
\index{bracket!squiggly}
4143

42-
>>> eng2sp['one'] = 'uno'
44+
~~~~ {.python}
45+
>>> eng2sp['one'] = 'uno'
46+
~~~~
4347

4448
This line creates an item that maps from the key `'one'` to
4549
the value "uno". If we print the dictionary again, we see a key-value
4650
pair with a colon between the key and value:
4751

48-
>>> print(eng2sp)
49-
{'one': 'uno'}
52+
~~~~ {.python}
53+
>>> print(eng2sp)
54+
{'one': 'uno'}
55+
~~~~
5056

5157
This output format is also an input format. For example, you can create
52-
a new dictionary with three items:
53-
54-
>>> eng2sp = {'one': 'uno', 'two': 'dos', 'three': 'tres'}
55-
56-
But if you print `eng2sp`, you might be surprised:
58+
a new dictionary with three items. But if you print `eng2sp`, you might be surprised:
5759

58-
>>> print(eng2sp)
59-
{'one': 'uno', 'three': 'tres', 'two': 'dos'}
60+
~~~~ {.python}
61+
>>> eng2sp = {'one': 'uno', 'two': 'dos', 'three': 'tres'}
62+
>>> print(eng2sp)
63+
{'one': 'uno', 'three': 'tres', 'two': 'dos'}
64+
~~~~
6065

6166
The order of the key-value pairs is not the same. In fact, if you type
6267
the same example on your computer, you might get a different result. In
@@ -66,8 +71,10 @@ But that's not a problem because the elements of a dictionary are never
6671
indexed with integer indices. Instead, you use the keys to look up the
6772
corresponding values:
6873

69-
>>> print(eng2sp['two'])
70-
'dos'
74+
~~~~ {.python}
75+
>>> print(eng2sp['two'])
76+
'dos'
77+
~~~~
7178

7279
The key `'two'` always maps to the value "dos" so the order
7380
of the items doesn't matter.
@@ -77,17 +84,21 @@ If the key isn't in the dictionary, you get an exception:
7784
\index{exception!KeyError}
7885
\index{KeyError}
7986

80-
>>> print(eng2sp['four'])
81-
KeyError: 'four'
87+
~~~~ {.python}
88+
>>> print(eng2sp['four'])
89+
KeyError: 'four'
90+
~~~~
8291

8392
The `len` function works on dictionaries; it returns the
8493
number of key-value pairs:
8594

8695
\index{len function}
8796
\index{function!len}
8897

89-
>>> len(eng2sp)
90-
3
98+
~~~~ {.python}
99+
>>> len(eng2sp)
100+
3
101+
~~~~
91102

92103
The `in` operator works on dictionaries; it tells you whether
93104
something appears as a *key* in the dictionary (appearing as a value is
@@ -97,10 +108,12 @@ not good enough).
97108
\index{in operator}
98109
\index{operator!in}
99110

100-
>>> 'one' in eng2sp
101-
True
102-
>>> 'uno' in eng2sp
103-
False
111+
~~~~ {.python}
112+
>>> 'one' in eng2sp
113+
True
114+
>>> 'uno' in eng2sp
115+
False
116+
~~~~
104117

105118
To see whether something appears as a value in a dictionary, you can use
106119
the method `values`, which returns the values as a list, and
@@ -109,9 +122,11 @@ then use the `in` operator:
109122
\index{values method}
110123
\index{method!values}
111124

112-
>>> vals = list(eng2sp.values())
113-
>>> 'uno' in vals
114-
True
125+
~~~~ {.python}
126+
>>> vals = list(eng2sp.values())
127+
>>> 'uno' in vals
128+
True
129+
~~~~
115130

116131
The `in` operator uses different algorithms for lists and
117132
dictionaries. For lists, it uses a linear search algorithm. As the list
@@ -171,14 +186,16 @@ for the letters that do appear.
171186

172187
Here is what the code might look like:
173188

174-
word = 'brontosaurus'
175-
d = dict()
176-
for c in word:
177-
if c not in d:
178-
d[c] = 1
179-
else:
180-
d[c] = d[c] + 1
181-
print(d)
189+
~~~~ {.python .trinket}
190+
word = 'brontosaurus'
191+
d = dict()
192+
for c in word:
193+
if c not in d:
194+
d[c] = 1
195+
else:
196+
d[c] = d[c] + 1
197+
print(d)
198+
~~~~
182199

183200
We are effectively computing a *histogram*, which is a
184201
statistical term for a set of counters (or frequencies).
@@ -197,7 +214,9 @@ dictionary we increment `d[c]`.
197214

198215
Here's the output of the program:
199216

200-
{'a': 1, 'b': 1, 'o': 2, 'n': 1, 's': 2, 'r': 2, 'u': 2, 't': 1}
217+
~~~~ {.python}
218+
{'a': 1, 'b': 1, 'o': 2, 'n': 1, 's': 2, 'r': 2, 'u': 2, 't': 1}
219+
~~~~
201220

202221
The histogram indicates that the letters `'a'` and "b"
203222
appear once; "o" appears twice, and so on.
@@ -210,22 +229,26 @@ a default value. If the key appears in the dictionary, `get`
210229
returns the corresponding value; otherwise it returns the default value.
211230
For example:
212231

213-
>>> counts = { 'chuck' : 1 , 'annie' : 42, 'jan': 100}
214-
>>> print(counts.get('jan', 0))
215-
100
216-
>>> print(counts.get('tim', 0))
217-
0
232+
~~~~ {.python .trinket}
233+
>>> counts = { 'chuck' : 1 , 'annie' : 42, 'jan': 100}
234+
>>> print(counts.get('jan', 0))
235+
100
236+
>>> print(counts.get('tim', 0))
237+
0
238+
~~~~
218239

219240
We can use `get` to write our histogram loop more concisely.
220241
Because the `get` method automatically handles the case where
221242
a key is not in a dictionary, we can reduce four lines down to one and
222243
eliminate the `if` statement.
223244

224-
word = 'brontosaurus'
225-
d = dict()
226-
for c in word:
227-
d[c] = d.get(c,0) + 1
228-
print(d)
245+
~~~~ {.python}
246+
word = 'brontosaurus'
247+
d = dict()
248+
for c in word:
249+
d[c] = d.get(c,0) + 1
250+
print(d)
251+
~~~~
229252

230253
The use of the `get` method to simplify this counting loop
231254
ends up being a very commonly used "idiom" in Python and we will use it
@@ -424,7 +447,7 @@ We will not specify the `table` but we will use the
424447
will even let Python tell us the list of characters that it considers
425448
"punctuation":
426449

427-
~~~~ {.python trinket}
450+
~~~~ {.python}
428451
>>> import string
429452
>>> string.punctuation
430453
'!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'

book/13-web.mkd

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,9 @@ particular Twitter user and returns it to us in JSON format in a string.
415415
We simply print the first 250 characters of the string:
416416

417417
\VerbatimInput{../code3/twitter1.py}
418+
\begin{trinketfiles}
419+
../code3/twurl.py
420+
\end{trinketfiles}
418421

419422
When the program runs it produces the following output:
420423

@@ -454,6 +457,9 @@ an indent of four characters to allow us to pore through the data when
454457
we want to extract more fields.
455458

456459
\VerbatimInput{../code3/twitter2.py}
460+
\begin{trinketfiles}
461+
../code3/twurl.py
462+
\end{trinketfiles}
457463

458464
Since the JSON becomes a set of nested Python lists and dictionaries, we
459465
can use a combination of the index operation and `for` loops

book/README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,32 @@ The output `bash book.sh` is in the file `x.pdf` and `x.epub`.
2929

3030
*Note that the build scripts require Python 2*
3131

32+
## Alternate Build Scripts
33+
34+
In addition to the official `book.sh1`, there are other build scripts that make
35+
alternate versions of the book.
36+
37+
* `htmlbook.sh` will make an html verion of the book, with interactive examples
38+
embedded in trinkets. These files are in `books/html` if you want to download
39+
or view them.
40+
* `zipbook.sh` will make two html versions of the book with Trinket branding,
41+
one with interactive examples (that require an internet connection to work) and one with
42+
syntax highlighted code blocks for completely offline viewing. A zip containing
43+
these is at `/book/zips/pfe.zip` if you'd just like to download it.
44+
* `trinketbook.sh` will make the nunjucks template that we use to host the book
45+
at [books.trinket.io](https://books.trinket.io). This is likely not of use to you
46+
unless you're looking for an example of how to get the book source into your own
47+
templating language. If you'd like to see the output of this script it's in
48+
`books/trinket/pfe`.
49+
50+
If you'd like to make your own build script, you can use these as examples. If
51+
your build script might have use to others, consider contributing it in a pull request.
52+
Note that each build script plays nicely with the others and the represent parallel
53+
workflows. Please don't alter any of the python scripts that are used by another
54+
script if you intend on contributing a new script.
55+
56+
## Contributing
57+
3258
If you want to contribute, feel free to fork the pythonlearn
3359
repository and send me pull requests.
3460

book/consoles.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919
whole_string = x.group(1)
2020
arguments = x.group(1).split()
2121

22-
# print x
22+
# Check for errors
23+
if x and ('trinket' in arguments):
24+
raise Exception("Error: found 'trinket'; should be '.trinket' in " + line)
2325
# If open, include script
2426
if x and ('.trinket' in arguments) and ('.python' in arguments) :
2527
with open('trinket/console-trinket-script') as ts:

book/html/01-intro.html

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
55
<meta http-equiv="Content-Style-Type" content="text/css" />
66
<meta name="generator" content="pandoc" />
7-
<meta name="author" content="Charles Severance" />
8-
<title>Python 3.0 For Informatics : Exploring Information</title>
7+
<meta name="author" content="Exploring Information using Python 3.0" />
8+
<title>Python for Everybody</title>
99
<style type="text/css">code{white-space: pre;}; pre.sourceCode {margin-left: 2em;}</style>
1010
<style type="text/css">
1111
div.sourceCode { overflow-x: auto; }
@@ -74,9 +74,9 @@
7474
<div class="col-9-12">
7575
<div class="content">
7676
<div id="header">
77-
<h1 class="title">Python 3.0 For Informatics : Exploring Information</h1>
78-
<h2 class="author">Charles Severance</h2>
79-
<h3 class="date">This book is being converted from Python 2.0 to Python 3.0. If you want to help see http://www.pythonlearn.com/book/</h3>
77+
<h1 class="title">Python for Everybody</h1>
78+
<h2 class="author">Exploring Information using Python 3.0</h2>
79+
<h3 class="date">Charles R. Severance</h3>
8080
</div>
8181

8282
<h1 id="why-should-you-learn-to-write-programs">Why should you learn to write programs?</h1>
@@ -182,7 +182,7 @@ <h2 id="conversing-with-python">Conversing with Python</h2>
182182
<span class="st"> File &quot;&lt;stdin&gt;&quot;, line 1</span>
183183
<span class="st"> print '</span>We will have a feast tonight unless you say
184184
<span class="op">^</span>
185-
<span class="pp">SyntaxError</span>: EOL <span class="cf">while</span> scanning string literal
185+
<span class="pp">SyntaxError</span>: Missing parentheses <span class="op">in</span> call to <span class="st">'print'</span>
186186
<span class="op">&gt;&gt;&gt;</span></code></pre></div>
187187
<p>The conversation was going so well for a while and then you made the tiniest mistake using the Python language and Python brought the spears back out.</p>
188188
<p>At this point, you should also realize that while Python is amazingly complex and powerful and very picky about the syntax you use to communicate with it, Python is <em>not</em> intelligent. You are really just having a conversation with yourself, but using proper syntax.</p>
@@ -240,7 +240,7 @@ <h2 id="writing-a-program">Writing a program</h2>
240240
<p></p>
241241
<p>To execute the script, you have to tell the Python interpreter the name of the file. In a Unix or Windows command window, you would type <code>python hello.py</code> as follows:</p>
242242
<div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash"> <span class="kw">csev</span>$ cat hello.py
243-
<span class="kw">print</span> <span class="st">'Hello world!'</span>
243+
<span class="kw">print</span>(<span class="st">'Hello world!'</span>)
244244
<span class="kw">csev</span>$ python hello.py
245245
<span class="kw">Hello</span> world!
246246
<span class="kw">csev</span>$</code></pre></div>

0 commit comments

Comments
 (0)
0