Upload
bleemeo
View
65
Download
1
Embed Size (px)
Citation preview
10 Reasons to Adopt Python 3
About Me
Pierre Fersing – [email protected]
CTO & co-founder @BleemeoPython & Django dev for 10 years
Monitoring as a Service solution
API in Django + User Interface in ReactJS
① Comparison in Python
>>> ["user1", 20] < ["user2", 10]True>>> ["user1", 20] < ["user1", 30]True
① Comparison in Python
Py2 : False !Py3 : TypeError: unorderable types: tuple() < list()
>>> ("user1", 20) < ["user2", 99]
>>> max("one", 2)"one"
② Iterators, Iterators Everywhere
Consume about 1,5 GB of memory with Python 2 !
range vs xrange, keys vs iterkeys, values vs itervalues, items vs iteritems
zip, map, filter
>>> for _ in range(50000000):... the_answer = 42
③ Keywords Only Arguments
>>> def sortwords(... words, reverse, case_sensitive):
>>> sortwords(... ["one", "two", "three"],... reverse=True, case_sensitive=False)
③ Keywords Only Arguments
>>> def sortwords(... words, reverse, case_sensitive):
>>> sortwords(... ["one", "two", "three"], True, False)
>>> def sortwords(... words, case_sensitive, reverse):
>>> def sortwords(... words, *, case_sensitive, reverse):
③ Keyword Arguments
>>> sortwords(... "one", "two", "three",... case_sensitive=False... )
>>> def sortwords(*words, **kwargs):... case_sensitive = \... kwargs.pop("case_sensitive")
③ Keyword Arguments
>>> sortwords(... "one", "two", "three",... case_sensitive=False... )
>>> def sortwords(... *words, case_sensitive=True):
④ Improved Except Syntax
>>> try:... 0 / 0... except OSError, ZeroDivisionError:... print("Error")
④ Improved Except Syntax
>>> try:... 0 / 0... except (OSError, ZeroDivisionError) as err:... print("Error")
⑤ Chained Exceptions
>>> try:... connection = do_connect()... # do stuff... finally:... connection.close()
Traceback (most recent call last): File "code.py", line 5, in <module> connection.close()NameError: name 'connection' is not defined
⑤ Chained Exceptions
Traceback (most recent call last): File "plop.py", line 2, in <module> connection = do_connect()ConnectionRefusedError: [Errno 111] Connection refused
During handling of the above exception, another exception occurred:
Traceback (most recent call last): File "plop.py", line 5, in <module> connection.close()NameError: name 'connection' is not defined
⑥ Reworked OS/IO Exception
>>> try:... fd = open("/etc/shadow")... except IOError:... print("Error")
>>> try:... os.stat("/etc/shadow")... except OSError:... print("Error")
⑥ Reworked OS/IO Exception
>>> try:... fd = open("/etc/shadow")... except IOError as err:... if err.errno in (EACCES, EPERM):... print("Error")... else:... raise
⑥ Reworked OS/IO Exception
>>> try:... fd = open("/etc/shadow")... except PermissionError:... print("Error")
⑦ Reworked Stdlib Names
Which module ? urllib, urllib2, urlparse ?– Parsing an url : urlparse
– Quoting an URL : urllib
– Do a requests : urllib2
No more cPickle, cProfile, cStringIO
⑧ Stdlib Additions
>>> etc = pathlib.Path("/etc")... file = etc / "passwd"... file.read_text()
>>> @functools.lru_cache(max_size=32)... def expansive_function(params):
⑧ Stdlib Additions
>>> subprocess.run(... ["ls", "-l"], timeout=10)
>>> datetime.now().timestamp()
>>> secrets.token_urlsafe()
⑧ Stdlib Additions
Lots more :– lzma
– enum
– ipaddress
– faulthandler
– statistics
– ...
⑨ asyncio and async/await
>>> reader,writer = await asyncio.open_connection(... "www.python.org", 80)... writer.write(b'GET / […]')... async for line in reader:... # do something with the line
⑩ Bonus
>>> 1 / 20.5
>>> for x in iterable:... yield x>>> yield from iterable
⑩ Bonus
Tab-completion in interpreter
>>> class Children(Parent):... def method(self):... super().method()
⑩ Bonus
>>> round(1.5)2>>> round(2.5)2
Question ?