What's New
What's Touchy
What's Coming
self.assertRaises(IndexError, foo, [1, 2, 3], bar=12)
with self.assertRaises(IndexError):
[1, 2, 3][42]
self.assertRaises(ValidationError, getattr, thing, "attr")
with self.assertRaises(ValidationError):
thing.attr
str.format
enhancements"{} {} {}".format("hello", "world", "!")
subprocess.check_output
>>> subprocess.check_output(["echo", "What a pretty kitty!"])
'What a pretty kitty!'
bz2.BZ2File
zipfile.ZipFile
>>>{i for i in xrange(10) if i % 2}
set([1, 3, 5, 7, 9])
>>> d = {1 : 2, 3 : 4}
>>> d.viewkeys() & {1}
set([1])
No we don't need a special function that does:
def input():
eval(sys.stdin.readline())
Wat
> 1 + "0"
'10'
Wat
>>> 1 < "0"
True
A million names to be cruel
url...wat?
Wat
>>> d = {u"foo" : 12, b"bar" : "monkeys"}
>>> d[b"foo"], d[u"bar"]
(12, "monkeys")
Wat
def add(s1, s2):
return " ".join(s1, s2)
>>> print add(u"hello", "world")
"hello world"
>>> print add(u"hello", "w ⓞ rld")
Traceback (most recent call last):
...
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 3: ordinal not in range(128)
Walk the Line
Python 2 only
Python 3 only
Support everything
Something in between
So you want to write 2.x+3.x code
Figure out how!
-
2to3
(python-modernize
)
-
3to2
-
unified codebase
So you want to write 2.x+3.x code
Ignore <2.6 and <(=)3.1
try:
int("Arg!")
except ValueError, e:
pass
So you want to write 2.x+3.x code
Be clever.
from __future__ import unicode_literals, division
if PY3:
from urllib import parse as urlparse
from urllib.parse import unquote
from urllib.request import urlopen
basestring = unicode = str
iteritems = operator.methodcaller("items")
else:
from itertools import izip as zip
from urllib import unquote
from urllib2 import urlopen
import urlparse
iteritems = operator.methodcaller("iteritems")
Lighten Up
The Stragglers
-
alternate implementations
-
networking
-
large frameworks
-
backward compatible conscious
The Tragic Tale of twisted.internet.test.test_tcp
$ bin/trial twisted.test.test_tcp
...
Ran 36 tests in 0.875s
$ admin/run-python3-tests twisted.test.test_tcp
...
Ran 36 tests in 38.698s
>>> bytes(14)
'14'
>>> bytes(14)
b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
def process(file=None):
if file is None:
file = open("a/default/file")
cleanup_needed = True
try:
result = perform_operation()
if result:
cleanup_needed = False
finally:
if cleanup_needed:
cleanup_resources()
from contextlib import ExitStack
def process(file=None):
with ExitStack() as stack:
if file is None:
file = open("a/default/file")
stack.enter_context(file)
process(file)
try:
with open("document.txt") as f:
content = f.read()
except IOError as err:
if err.errno == ENOENT:
pass
raise
try:
with open("document.txt") as f:
content = f.read()
except FileNotFoundError:
pass