Skip to content

Commit 178f786

Browse files
committed
Adds example code, readme, and errata page
1 parent 6503be9 commit 178f786

116 files changed

Lines changed: 7232 additions & 0 deletions

File tree

Some content is hidden

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

Errata.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Errata
2+
3+
These are the errors that have been reported for [_Effective Python_](http://www.effectivepython.com). If you found a mistake but don't see it listed below, [please create a new issue here](https://github.com/bslatkin/effectivepython/issues/new). Thanks for your help!
4+
5+
_No errors found (yet)._

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Effective Python
2+
3+
Hello! You've reached the official source code repository for _Effective Python_. To learn more about the book or contact the author, please [visit the official website](http://www.effectivepython.com).
4+
5+
[![Cover](./cover.jpg)](http://www.effectivepython.com)
6+
7+
In this repository you can browse all of the source code included in the book. Each item has its own file or directory containing the example code. Each file is annotated with which example snippet it came from within each chapter.
8+
9+
To run all the code for an item, just type `./item_01.py` into your shell and see what it prints out. Alternatively you can type `python3 item_01.py` or `python2.7 item_03_example_03.py` to run a specific version of Python.
10+
11+
To report a problem with the book or view known issues, please [visit the Errata page](./Errata.md).

cover.jpg

38.9 KB
Loading

example_code/item_01.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/usr/bin/env python3
2+
3+
# Copyright 2014 Brett Slatkin, Pearson Education Inc.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
# Preamble to mimick book environment
18+
import logging
19+
from pprint import pprint
20+
from sys import stdout as STDOUT
21+
22+
23+
# Example 1
24+
import sys
25+
print(sys.version_info)
26+
print(sys.version)

example_code/item_03.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#!/usr/bin/env python3
2+
3+
# Copyright 2014 Brett Slatkin, Pearson Education Inc.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
# Preamble to mimick book environment
18+
import logging
19+
from pprint import pprint
20+
from sys import stdout as STDOUT
21+
22+
23+
# Example 1
24+
def to_str(bytes_or_str):
25+
if isinstance(bytes_or_str, bytes):
26+
value = bytes_or_str.decode('utf-8')
27+
else:
28+
value = bytes_or_str
29+
return value # Instance of str
30+
31+
print(repr(to_str(b'foo')))
32+
print(repr(to_str('foo')))
33+
34+
35+
# Example 2
36+
def to_bytes(bytes_or_str):
37+
if isinstance(bytes_or_str, str):
38+
value = bytes_or_str.encode('utf-8')
39+
else:
40+
value = bytes_or_str
41+
return value # Instance of bytes
42+
43+
print(repr(to_bytes(b'foo')))
44+
print(repr(to_bytes('foo')))
45+
46+
47+
# Example 5
48+
try:
49+
import os
50+
with open('/tmp/random.bin', 'w') as f:
51+
f.write(os.urandom(10))
52+
except:
53+
logging.exception('Expected')
54+
else:
55+
assert False
56+
57+
58+
# Example 6
59+
with open('/tmp/random.bin', 'wb') as f:
60+
f.write(os.urandom(10))

example_code/item_03_example_03.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/usr/bin/env python2.7
2+
3+
# Copyright 2014 Brett Slatkin, Pearson Education Inc.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
# Preamble to mimick book environment
18+
import logging
19+
from pprint import pprint
20+
from sys import stdout as STDOUT
21+
22+
23+
# Example 3
24+
def to_unicode(unicode_or_str):
25+
if isinstance(unicode_or_str, str):
26+
value = unicode_or_str.decode('utf-8')
27+
else:
28+
value = unicode_or_str
29+
return value # Instance of unicode
30+
31+
print(repr(to_unicode(u'foo')))
32+
print(repr(to_unicode('foo')))

example_code/item_03_example_04.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/usr/bin/env python2.7
2+
3+
# Copyright 2014 Brett Slatkin, Pearson Education Inc.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
# Preamble to mimick book environment
18+
import logging
19+
from pprint import pprint
20+
from sys import stdout as STDOUT
21+
22+
23+
# Example 4
24+
def to_str(unicode_or_str):
25+
if isinstance(unicode_or_str, unicode):
26+
value = unicode_or_str.encode('utf-8')
27+
else:
28+
value = unicode_or_str
29+
return value # Instance of str
30+
31+
print(repr(to_str(u'foo')))
32+
print(repr(to_str('foo')))

example_code/item_04.py

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#!/usr/bin/env python3
2+
3+
# Copyright 2014 Brett Slatkin, Pearson Education Inc.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
# Preamble to mimick book environment
18+
import logging
19+
from pprint import pprint
20+
from sys import stdout as STDOUT
21+
22+
23+
# Example 1
24+
from urllib.parse import parse_qs
25+
my_values = parse_qs('red=5&blue=0&green=',
26+
keep_blank_values=True)
27+
print(repr(my_values))
28+
29+
30+
# Example 2
31+
print('Red: ', my_values.get('red'))
32+
print('Green: ', my_values.get('green'))
33+
print('Opacity: ', my_values.get('opacity'))
34+
35+
36+
# Example 3
37+
# For query string 'red=5&blue=0&green='
38+
red = my_values.get('red', [''])[0] or 0
39+
green = my_values.get('green', [''])[0] or 0
40+
opacity = my_values.get('opacity', [''])[0] or 0
41+
print('Red: %r' % red)
42+
print('Green: %r' % green)
43+
print('Opacity: %r' % opacity)
44+
45+
46+
# Example 4
47+
red = int(my_values.get('red', [''])[0] or 0)
48+
green = int(my_values.get('green', [''])[0] or 0)
49+
opacity = int(my_values.get('opacity', [''])[0] or 0)
50+
print('Red: %r' % red)
51+
print('Green: %r' % green)
52+
print('Opacity: %r' % opacity)
53+
54+
55+
# Example 5
56+
red = my_values.get('red', [''])
57+
red = int(red[0]) if red[0] else 0
58+
green = my_values.get('green', [''])
59+
green = int(green[0]) if green[0] else 0
60+
opacity = my_values.get('opacity', [''])
61+
opacity = int(opacity[0]) if opacity[0] else 0
62+
print('Red: %r' % red)
63+
print('Green: %r' % green)
64+
print('Opacity: %r' % opacity)
65+
66+
67+
# Example 6
68+
green = my_values.get('green', [''])
69+
if green[0]:
70+
green = int(green[0])
71+
else:
72+
green = 0
73+
print('Green: %r' % green)
74+
75+
76+
# Example 7
77+
def get_first_int(values, key, default=0):
78+
found = values.get(key, [''])
79+
if found[0]:
80+
found = int(found[0])
81+
else:
82+
found = default
83+
return found
84+
85+
86+
# Example 8
87+
green = get_first_int(my_values, 'green')
88+
print('Green: %r' % green)

example_code/item_05.py

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
#!/usr/bin/env python3
2+
3+
# Copyright 2014 Brett Slatkin, Pearson Education Inc.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
# Preamble to mimick book environment
18+
import logging
19+
from pprint import pprint
20+
from sys import stdout as STDOUT
21+
22+
23+
# Example 1
24+
a = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
25+
print('First four:', a[:4])
26+
print('Last four: ', a[-4:])
27+
print('Middle two:', a[3:-3])
28+
29+
30+
# Example 2
31+
assert a[:5] == a[0:5]
32+
33+
34+
# Example 3
35+
assert a[5:] == a[5:len(a)]
36+
37+
38+
# Example 4
39+
print(a[:5])
40+
print(a[:-1])
41+
print(a[4:])
42+
print(a[-3:])
43+
print(a[2:5])
44+
print(a[2:-1])
45+
print(a[-3:-1])
46+
47+
48+
# Example 5
49+
a[:] # ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
50+
a[:5] # ['a', 'b', 'c', 'd', 'e']
51+
a[:-1] # ['a', 'b', 'c', 'd', 'e', 'f', 'g']
52+
a[4:] # ['e', 'f', 'g', 'h']
53+
a[-3:] # ['f', 'g', 'h']
54+
a[2:5] # ['c', 'd', 'e']
55+
a[2:-1] # ['c', 'd', 'e', 'f', 'g']
56+
a[-3:-1] # ['f', 'g']
57+
58+
59+
# Example 6
60+
first_twenty_items = a[:20]
61+
last_twenty_items = a[-20:]
62+
63+
64+
# Example 7
65+
try:
66+
a[20]
67+
except:
68+
logging.exception('Expected')
69+
else:
70+
assert False
71+
72+
73+
# Example 8
74+
b = a[4:]
75+
print('Before: ', b)
76+
b[1] = 99
77+
print('After: ', b)
78+
print('No change:', a)
79+
80+
81+
# Example 9
82+
print('Before ', a)
83+
a[2:7] = [99, 22, 14]
84+
print('After ', a)
85+
86+
87+
# Example 10
88+
b = a[:]
89+
assert b == a and b is not a
90+
91+
92+
# Example 11
93+
b = a
94+
print('Before', a)
95+
a[:] = [101, 102, 103]
96+
assert a is b # Still the same list object
97+
print('After ', a) # Now has different contents

0 commit comments

Comments
 (0)