Skip to content

Commit 813ee65

Browse files
committed
커밋
1 parent 4e0297a commit 813ee65

File tree

152 files changed

+16430
-0
lines changed

Some content is hidden

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

152 files changed

+16430
-0
lines changed

Effective_Python/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
**/*.pyc

Effective_Python/Errata.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Errata
2+
3+
You can see a list of errors that have been found in [_Effective Python: Second Edition_](https://effectivepython.com) by looking at the `Confirmed` label for open issues [following this link](https://github.com/bslatkin/effectivepython/issues?utf8=✓&q=label%3A2ed+label%3Aconfirmed). If you found a mistake and don't see it listed there, [please create a new issue](https://github.com/bslatkin/effectivepython/issues/new). Thanks for your help!

Effective_Python/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: Second Edition_. To learn more about the book or contact the author, please [visit the official website](https://effectivepython.com).
4+
5+
[![Cover](./cover.jpg)](https://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` 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).

Effective_Python/VIDEO.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Example code for [Effective Python LiveLessons](http://www.informit.com/store/effective-python-livelessons-video-training-downloadable-9780134175164)
2+
3+
**This is for a previous edition of the book.**
4+
5+
[Go here to see the corresponding example code](../v1/VIDEO.md)

Effective_Python/cover.jpg

59.2 KB
Loading
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#!/usr/bin/env PYTHONHASHSEED=1234 python3
2+
3+
# Copyright 2014-2019 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+
# Reproduce book environment
18+
import random
19+
random.seed(1234)
20+
21+
import logging
22+
from pprint import pprint
23+
from sys import stdout as STDOUT
24+
25+
# Write all output to a temporary directory
26+
import atexit
27+
import gc
28+
import io
29+
import os
30+
import tempfile
31+
32+
TEST_DIR = tempfile.TemporaryDirectory()
33+
atexit.register(TEST_DIR.cleanup)
34+
35+
# Make sure Windows processes exit cleanly
36+
OLD_CWD = os.getcwd()
37+
atexit.register(lambda: os.chdir(OLD_CWD))
38+
os.chdir(TEST_DIR.name)
39+
40+
def close_open_files():
41+
everything = gc.get_objects()
42+
for obj in everything:
43+
if isinstance(obj, io.IOBase):
44+
obj.close()
45+
46+
atexit.register(close_open_files)
47+
48+
49+
# Example 1
50+
import sys
51+
print(sys.version_info)
52+
print(sys.version)
Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
#!/usr/bin/env PYTHONHASHSEED=1234 python3
2+
3+
# Copyright 2014-2019 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+
# Reproduce book environment
18+
import random
19+
random.seed(1234)
20+
21+
import logging
22+
from pprint import pprint
23+
from sys import stdout as STDOUT
24+
25+
# Write all output to a temporary directory
26+
import atexit
27+
import gc
28+
import io
29+
import os
30+
import tempfile
31+
32+
TEST_DIR = tempfile.TemporaryDirectory()
33+
atexit.register(TEST_DIR.cleanup)
34+
35+
# Make sure Windows processes exit cleanly
36+
OLD_CWD = os.getcwd()
37+
atexit.register(lambda: os.chdir(OLD_CWD))
38+
os.chdir(TEST_DIR.name)
39+
40+
def close_open_files():
41+
everything = gc.get_objects()
42+
for obj in everything:
43+
if isinstance(obj, io.IOBase):
44+
obj.close()
45+
46+
atexit.register(close_open_files)
47+
48+
49+
# Example 1
50+
a = b'h\x65llo'
51+
print(list(a))
52+
print(a)
53+
54+
55+
# Example 2
56+
a = 'a\u0300 propos'
57+
print(list(a))
58+
print(a)
59+
60+
61+
# Example 3
62+
def to_str(bytes_or_str):
63+
if isinstance(bytes_or_str, bytes):
64+
value = bytes_or_str.decode('utf-8')
65+
else:
66+
value = bytes_or_str
67+
return value # Instance of str
68+
69+
print(repr(to_str(b'foo')))
70+
print(repr(to_str('bar')))
71+
72+
73+
# Example 4
74+
def to_bytes(bytes_or_str):
75+
if isinstance(bytes_or_str, str):
76+
value = bytes_or_str.encode('utf-8')
77+
else:
78+
value = bytes_or_str
79+
return value # Instance of bytes
80+
81+
print(repr(to_bytes(b'foo')))
82+
print(repr(to_bytes('bar')))
83+
84+
85+
# Example 5
86+
print(b'one' + b'two')
87+
print('one' + 'two')
88+
89+
90+
# Example 6
91+
try:
92+
b'one' + 'two'
93+
except:
94+
logging.exception('Expected')
95+
else:
96+
assert False
97+
98+
99+
# Example 7
100+
try:
101+
'one' + b'two'
102+
except:
103+
logging.exception('Expected')
104+
else:
105+
assert False
106+
107+
108+
# Example 8
109+
assert b'red' > b'blue'
110+
assert 'red' > 'blue'
111+
112+
113+
# Example 9
114+
try:
115+
assert 'red' > b'blue'
116+
except:
117+
logging.exception('Expected')
118+
else:
119+
assert False
120+
121+
122+
# Example 10
123+
try:
124+
assert b'blue' < 'red'
125+
except:
126+
logging.exception('Expected')
127+
else:
128+
assert False
129+
130+
131+
# Example 11
132+
print(b'foo' == 'foo')
133+
134+
135+
# Example 12
136+
print(b'red %s' % b'blue')
137+
print('red %s' % 'blue')
138+
139+
140+
# Example 13
141+
try:
142+
print(b'red %s' % 'blue')
143+
except:
144+
logging.exception('Expected')
145+
else:
146+
assert False
147+
148+
149+
# Example 14
150+
print('red %s' % b'blue')
151+
152+
153+
# Example 15
154+
try:
155+
with open('data.bin', 'w') as f:
156+
f.write(b'\xf1\xf2\xf3\xf4\xf5')
157+
except:
158+
logging.exception('Expected')
159+
else:
160+
assert False
161+
162+
163+
# Example 16
164+
with open('data.bin', 'wb') as f:
165+
f.write(b'\xf1\xf2\xf3\xf4\xf5')
166+
167+
168+
# Example 17
169+
try:
170+
# Silently force UTF-8 here to make sure this test fails on
171+
# all platforms. cp1252 considers these bytes valid on Windows.
172+
real_open = open
173+
def open(*args, **kwargs):
174+
kwargs['encoding'] = 'utf-8'
175+
return real_open(*args, **kwargs)
176+
177+
with open('data.bin', 'r') as f:
178+
data = f.read()
179+
except:
180+
logging.exception('Expected')
181+
else:
182+
assert False
183+
184+
185+
# Example 18
186+
# Restore the overloaded open above.
187+
open = real_open
188+
189+
with open('data.bin', 'rb') as f:
190+
data = f.read()
191+
192+
assert data == b'\xf1\xf2\xf3\xf4\xf5'
193+
194+
195+
# Example 19
196+
with open('data.bin', 'r', encoding='cp1252') as f:
197+
data = f.read()
198+
199+
assert data == 'ñòóôõ'

0 commit comments

Comments
 (0)