-
Notifications
You must be signed in to change notification settings - Fork 21
/
slides.html
1034 lines (705 loc) · 19.3 KB
/
slides.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html>
<head>
<title>10 awesome features of Python that you can't use because you refuse to upgrade to Python 3</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<style type="text/css">
/* Slideshow styles */
</style>
</head>
<body>
<textarea id="source">
class: center, middle
# 10 awesome features of Python that you can't use because you refuse to upgrade to Python 3
.footnote[[There is also a pdf version of these slides](http://asmeurer.github.io/python3-presentation/python3-presentation.pdf)]
---
class: center, middle
# 10 awesome features of Python that you can't use because you refuse to upgrade to Python 3
# or
# Turning it up to 13!
---
# Prelude
- Last month (March) APUG: only three people use Python 3 (including me)
- Lots of new features of Python 3.
- Some have been backported to Python 2.7. (like dictionary/set comprehensions
or set literals, `__future__.print_function`)
- But there's more than that.
- New features that you *can't* use unless you are in Python 3.
- New syntax. New interpreter behavior. Standard library fixes.
- And it's more than bytes/unicode...
---
layout: true
# Feature 1: Advanced unpacking
---
- You can already do this:
```py
>>> a, b = range(2)
>>> a
0
>>> b
1
```
--
- Now you can do this:
```py
>>> a, b, *rest = range(10)
>>> a
0
>>> b
1
>>> rest
[2, 3, 4, 5, 6, 7, 8, 9]
```
--
- `*rest` can go anywhere:
```py
>>> a, *rest, b = range(10)
>>> a
0
>>> b
9
>>> rest
[1, 2, 3, 4, 5, 6, 7, 8]
```
```py
>>> *rest, b = range(10)
>>> rest
[0, 1, 2, 3, 4, 5, 6, 7, 8]
>>> b
9
```
---
## Get the first and last lines of a file
```py
>>> with open("using_python_to_profit") as f:
... first, *_, last = f.readlines() # Warning: this puts the whole file contents in memory!
>>> first
'Step 1: Use Python 3\n'
>>> last
'Step 10: Profit!\n'
```
---
layout: true
# Feature 2: Keyword only arguments
---
```py
def f(a, b, *args, option=True):
...
```
--
- `option` comes *after* `*args`.
--
- The only way to access it is to explicitly call `f(a, b, option=True)`
--
- You can write just a `*` if you don't want to collect `*args`.
```py
def f(a, b, *, option=True):
...
```
---
- No more, "Oops, I accidentally passed too many arguments to the function, and
one of them was swallowed by a keyword argument".
```py
def sum(a, b, biteme=False):
if biteme:
shutil.rmtree('/')
else:
return a + b
```
```py
>>> sum(1, 2)
3
```
```py
>>> sum(1, 2, 3)
```
--
.center[<img src="bomb.jpg" width="500">]
---
- Instead write
```py
def sum(a, b, *, biteme=False):
if biteme:
shutil.rmtree('/')
else:
return a + b
```
```py
>>> sum(1, 2, 3)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: sum() takes 2 positional arguments but 3 were given
```
--
.center[![](borat.jpg)]
---
- Or, "I reordered the keyword arguments of a function, but something was
implicitly passing in arguments expecting the order"
- Example:
```py
def maxall(iterable, key=None):
"""
A list of all max items from the iterable
"""
key = key or (lambda x: x)
m = max(iterable, key=key)
return [i for i in iterable if key(i) == key(m)]
```
```py
>>> maxall(['a', 'ab', 'bc'], len)
['ab', 'bc']
```
---
- The `max` builtin supports `max(a, b, c)`. We should allow that too.
```py
def maxall(*args, key=None):
"""
A list of all max items from the iterable
"""
if len(args) == 1:
iterable = args[0]
else:
iterable = args
key = key or (lambda x: x)
m = max(iterable, key=key)
return [i for i in iterable if key(i) == key(m)]
```
- We just broke any code that passed in the key as a second argument without
using the keyword.
```py
>>> maxall(['a', 'ab', 'ac'], len)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 10, in maxall
TypeError: unorderable types: builtin_function_or_method() > list()
```
- (Actually in Python 2 it would just return `['a', 'ab', 'ac']`, see feature 6).
- By the way, `max` shows that this is already possible in Python 2, but only
if you write your function in C.
- Obviously, we should have used `maxall(iterable, *, key=None)` to begin
with.
---
- You can make your APIs "future change proof".
- Stupid example:
```py
def extendto(value, shorter, longer):
"""
Extend list `shorter` to the length of list `longer` with `value`
"""
if len(shorter) > len(longer):
raise ValueError('The `shorter` list is longer than the `longer` list')
shorter.extend([value]*(len(longer) - len(shorter)))
```
```py
>>> a = [1, 2]
>>> b = [1, 2, 3, 4, 5]
>>> extendto(10, a, b)
>>> a
[1, 2, 10, 10, 10]
```
--
- Hmm, maybe it makes more sense for `longer` to come before `shorter`...
- Too bad, you'll break the code.
---
- In Python 3, you can use
```py
def extendto(value, *, shorter=None, longer=None):
"""
Extend list `shorter` to the length of list `longer` with `value`
"""
if shorter is None or longer is None:
raise TypeError('`shorter` and `longer` must be specified')
if len(shorter) > len(longer):
raise ValueError('The `shorter` list is longer than the `longer` list')
shorter.extend([value]*(len(longer) - len(shorter)))
```
- Now, `a` and `b` *have* to be passed in as `extendto(10, shorter=a,
longer=b)`.
--
- Or if you prefer, `extendto(10, longer=b, shorter=a)`.
---
- Add new keyword arguments without breaking API.
- Python 3 did this in the standard library.
--
- For example, functions in `os` have `follow_symlinks` option.
--
- So you can just use `os.stat(file, follow_symlinks=False)` instead of `os.lstat`.
--
- In case that sounds more verbose, it lets you do
```py
s = os.stat(file, follow_symlinks=some_condition)
```
instead of
```py
if some_condition:
s = os.stat(file)
else:
s = os.lstat(file)
```
--
- But `os.stat(file, some_condition)` doesn't work.
- Keeps you from thinking it's a two-argument function.
---
- In Python 2, you have to use `**kwargs` and do the handling yourself.
--
- Lots of ugly `option = kwargs.pop(True)` at the top of your functions.
--
- No longer self documenting.
--
- If you somehow are writing for a Python 3 only codebase, I highly recommend
making all your keyword arguments keyword only, especially keyword arguments
that represent "options".
---
layout: true
# Feature 3: Chained exceptions
---
- **Situation:** you catch an exception with `except`, do something, and then
raise a different exception.
```py
def mycopy(source, dest):
try:
shutil.copy2(source, dest)
except OSError: # We don't have permissions. More on this later
raise NotImplementedError("automatic sudo injection")
```
- **Problem:** You lose the original traceback
```py
>>> mycopy('noway', 'noway2')
>>> mycopy(1, 2)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 5, in mycopy
NotImplementedError: automatic sudo injection
```
--
- What happened with the `OSError`?
---
- Python 3 shows you the whole chain of exceptions:
```py
mycopy('noway', 'noway2')
Traceback (most recent call last):
File "<stdin>", line 3, in mycopy
File "/Users/aaronmeurer/anaconda3/lib/python3.3/shutil.py", line 243, in copy2
copyfile(src, dst, follow_symlinks=follow_symlinks)
File "/Users/aaronmeurer/anaconda3/lib/python3.3/shutil.py", line 109, in copyfile
with open(src, 'rb') as fsrc:
PermissionError: [Errno 13] Permission denied: 'noway'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 5, in mycopy
NotImplementedError: automatic sudo injection
```
--
- You can also do this manually using `raise from`
```py
raise exception from e
```
```py
>>> raise NotImplementedError from OSError
OSError
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NotImplementedError
```
---
layout: true
# Feature 4: Fine grained `OSError` subclasses
---
- The code I just showed you is wrong.
- It catches `OSError` and assumes it is a permission error.
- But `OSError` can be a lot of things (file not found, is a directory, is not a
directory, broken pipe, ...)
- You really have to do
```py
import errno
def mycopy(source, dest):
try:
shutil.copy2(source, dest)
except OSError as e:
if e.errno in [errno.EPERM, errno.EACCES]:
raise NotImplementedError("automatic sudo injection")
else:
raise
```
--
- Wow. That sucks.
--
.center[<img src="rage.png" width="200">]
---
- Python 3 fixes this by adding a ton of [new exceptions](https://docs.python.org/3.4/library/exceptions.html#os-exceptions).
- You can just do
```py
def mycopy(source, dest):
try:
shutil.copy2(source, dest)
except PermissionError:
raise NotImplementedError("automatic sudo injection")
```
- (Don't worry, `PermissionError` subclasses from `OSError` and still has
`.errno`. Old code will still work).
---
layout: true
# Feature 5: Everything is an iterator
---
- This is the hardest one to sell.
- Iterators exist in Python 2 as well.
- But you have to use them. Don't write `range` or `zip` or `dict.values` or
....
---
- If you do...
--
```py
def naivesum(N):
"""
Naively sum the first N integers
"""
A = 0
for i in range(N + 1):
A += i
return A
```
--
```py
In [3]: timeit naivesum(1000000)
10 loops, best of 3: 61.4 ms per loop
```
--
```py
In [4]: timeit naivesum(10000000)
1 loops, best of 3: 622 ms per loop
```
--
```py
In [5]: timeit naivesum(100000000)
```
--
.center[<img src="startupdisk.png" width="500">]
---
.center[![](badtime.jpg)]
---
- Instead write some variant (`xrange`, `itertools.izip`,
`dict.itervalues`, ...).
- Inconsistent API anyone?
---
- In Python 3, `range`, `zip`, `map`, `dict.values`, etc. all return memory-efficient iterables.
- If you want a list, just wrap the result with `list`.
- Explicit is better than implicit.
- Harder to write code that accidentally uses too much memory, because the
input was bigger than you expected.
---
layout: true
# Feature 6: No more comparison of everything to everything
---
- In Python 2, you can do
```py
>>> max(['one', 2]) # One *is* the loneliest number
'one'
```
--
- Hurray. I just disproved math!
.center[![](disprovemath.jpg)]
---
- It's because in Python 2, you can `<` compare anything to anything.
```py
>>> 'abc' > 123
True
>>> None > all
False
```
--
- In Python 3, you can't do this:
```py
>>> 'one' > 2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unorderable types: str() > int()
```
- This avoids subtle bugs, e.g., from not coercing all types from int to str
or visa versa.
- Especially when you use `>` implicitly, like with `max` or `sorted`.
- In Python 2:
```py
>>> sorted(['1', 2, '3'])
[2, '1', '3']
```
---
layout: true
# Feature 7: yield from
---
- Pretty great if you use generators
- Instead of writing
```py
for i in gen():
yield i
```
Just write
```py
yield from gen()
```
- Easily refactor generators into subgenerators.
---
- Makes it easier to turn everything into a generator. See "Feature 5:
Everything is an iterator" above for why you should do this.
- Instead of accumulating a list, just `yield` or `yield from`.
- **Bad**
```py
def dup(n):
A = []
for i in range(n):
A.extend([i, i])
return A
```
**Good**
```py
def dup(n):
for i in range(n):
yield i
yield i
```
**Better**
```py
def dup(n):
for i in range(n):
yield from [i, i]
```
---
In case you don't know, generators are awesome because:
- Only one value is computed at a time. Low memory impact (see `range` example
above).
- Can break in the middle. Don't have to compute everything just to find out
you needed none of it. Compute just what you need. If you often *don't* need
it all, you can gain a lot of performance here.
- If you need a list, just call `list()` on the generator iterator. If you need slicing, use the efficient `itertools.islice()`.
- Function state is "saved" between yields.
- This leads to interesting possibilities, à la coroutines...
---
layout: true
# Feature 8: asyncio
---
- Uses new coroutine features and saved state of generators to do
asynchronous IO.
```py
# Taken from Guido's slides from “Tulip: Async I/O for Python 3” by Guido
# van Rossum, at LinkedIn, Mountain View, Jan 23, 2014
@coroutine
def fetch(host, port):
r,w = yield from open_connection(host,port)
w.write(b'GET /HTTP/1.0\r\n\r\n ')
while (yield from r.readline()).decode('latin-1').strip():
pass
body=yield from r.read()
return body
@coroutine
def start():
data = yield from fetch('python.org', 80)
print(data.decode('utf-8'))
```
--
- Not going to lie to you. I still don't get this.
--
- It's OK, though. Even David Beazley had a hard time with it:
.center[<img src="dabeaz.png" width="500">]
---
layout: true
# Feature 9: Standard library additions
---
## `faulthandler`
- Display (limited) tracebacks, even when Python dies the hard way.
- Won't work with `kill -9`, but does work with, e.g., segfaults.
```py
import faulthandler
faulthandler.enable()
def killme():
# Taken from http://nbviewer.ipython.org/github/ipython/ipython/blob/1.x/examples/notebooks/Part%201%20-%20Running%20Code.ipynb
import sys
from ctypes import CDLL
# This will crash a Linux or Mac system; equivalent calls can be made on
# Windows
dll = 'dylib' if sys.platform == 'darwin' else 'so.6'
libc = CDLL("libc.%s" % dll)
libc.time(-1) # BOOM!!
killme()
```
```bash
$python test.py
Fatal Python error: Segmentation fault
Current thread 0x00007fff781b6310:
File "test.py", line 11 in killme
File "test.py", line 13 in <module>
Segmentation fault: 11
```
- Or `kill -6` (`SIGABRT`)
- Can also enable with `python -X faulthandler`
---
## `ipaddress`
- Exactly that. IP addresses.
```py
>>> ipaddress.ip_address('192.168.0.1')
IPv4Address('192.168.0.1')
>>> ipaddress.ip_address('2001:db8::')
IPv6Address('2001:db8::')
```
- Just another thing you don't want to roll yourself.
---
## `functools.lru_cache`
- A LRU cache decorator for your functions.
- From [docs](https://docs.python.org/3/whatsnew/3.2.html#functools).
```py
@lru_cache(maxsize=32)
def get_pep(num):
'Retrieve text of a Python Enhancement Proposal'
resource = 'http://www.python.org/dev/peps/pep-%04d/' % num
try:
with urllib.request.urlopen(resource) as s:
return s.read()
except urllib.error.HTTPError:
return 'Not Found'
>>> for n in 8, 290, 308, 320, 8, 218, 320, 279, 289, 320, 9991:
... pep = get_pep(n)
... print(n, len(pep))
>>> get_pep.cache_info()
CacheInfo(hits=3, misses=8, maxsize=32, currsize=8)
```
---
## `enum`
- Finally, an enumerated type in the standard library.
- Python 3.4 only.
```py
>>> from enum import Enum
>>> class Color(Enum):
... red = 1
... green = 2
... blue = 3
...
```
- Uses some magic that is only possible in Python 3 (due to metaclass
changes):
```py
>>> class Shape(Enum):
... square = 2
... square = 3
...
Traceback (most recent call last):
...
TypeError: Attempted to reuse key: 'square'
```
---
layout: true
# Feature 10: Fun
---
## Unicode variable names
```py
>>> résumé = "knows Python"
>>> π = math.pi
```
--
- Sorry, letter-like characters only.
- `🍺 = "beer"` does not work.
--
## Function annotations
```py
def f(a: stuff, b: stuff = 2) -> result:
...
```
- Annotations can be arbitrary Python objects.
- Python doesn't do anything with the annotations other than put them in an
`__annotations__` dictionary.
```py
>>> def f(x: int) -> float:
... pass
...
>>> f.__annotations__
{'return': <class 'float'>, 'x': <class 'int'>}
```
- But it leaves open the possibility for library authors to do fun things.
- Example, IPython 2.0 widgets.
- Run IPython notebook (in Python 3) from IPython git checkout and open http://127.0.0.1:8888/notebooks/examples/Interactive%20Widgets/Image%20Processing.ipynb
---
layout: true
# Feature 11: Unicode and bytes
---
- In Python 2, `str` acts like bytes of data.
- There is also `unicode` type to represent Unicode strings.
--
- In Python 3, `str` is a *string*.
- `bytes` are bytes.
- There is no `unicode`. `str` strings are Unicode.
---
layout: true
# Feature 12: Matrix Multiplication
---
In Python 3.5, you are able to replace
```py
>>> a = np.array([[1, 0], [0, 1]])
>>> b = np.array([[4, 1], [2, 2]])
>>> np.dot(a, b)
array([[4, 1],
[2, 2]])
```
with
```py
>>> a = np.array([[1, 0], [0, 1]])
>>> b = np.array([[4, 1], [2, 2]])
>>> a @ b
array([[4, 1],
[2, 2]])
```
- Any object can override `__matmul__` to use `@`.
---
layout: true
# Feature 13: Pathlib
---
- In Python 2, path handling is verbose
```py
import os
directory = "/etc"
filepath = os.path.join(directory, "test_file.txt")
if os.path.exists(filepath):
stuff
```
- In Python 3, it is much more simpler
```py
from pathlib import Path
directory = Path("/etc")
filepath = directory / "test_file.txt"
if filepath.exists():
stuff
```
---
layout: true
class: center, middle
---
# Discuss
.center[![](homer.gif)]
---
Slides were made with http://remarkjs.com/
All images have been blatantly stolen from the internet.