Save 37% off PRO during our Black Friday Sale! »

Python 3.4

Python 3.4

Used for a lightning talk at Pražské Pyvo and a full talk at Brněnské Pyvo, both March 2014

870d613430249e453343efc9667ef636?s=128

Petr Viktorin

July 23, 2014
Tweet

Transcript

  1. Python 3.4 (party!) Petr Viktorin encukou@gmail.com Brněnské Pyvo, 2014-03-27

  2. Python 3.3 u'string'

  3. Python 3.3 u'string' yield from

  4. Python 3.3 u'string' yield from venv

  5. Python 3.3 u'string' yield from venv __qualname__

  6. Python 3.3 u'string' yield from venv __qualname__ unittest.mock

  7. Python 3.3 u'string' yield from venv __qualname__ unittest.mock Function signature

    objects
  8. Python 3.4 2014-03-16

  9. yield from → asyncio

  10. yield from → asyncio "WSGI for event loops"

  11. yield from → asyncio "WSGI for event loops" twisted, tornado,

    gevent
  12. yield from → asyncio "WSGI for event loops" twisted, tornado,

    gevent event loop, tasks, calbacks, futures/deferreds networking IO
  13. venv → venv + ensurepip

  14. Commonly reinvented wheels

  15. Commonly reinvented wheels belong in the standard library

  16. enum >>> from enum import Enum >>> class Color(Enum): ...

    red = 1 ... green = 2 ... blue = 3 >>> for c in Color: ... print(c) Color.red Color.green Color.blue
  17. pathlib >>> from pathlib import Path >>> p = Path('.')

    >>> p.isdir() True >>> p / 'slides.tex' PosixPath('./slides.tex') >>> (p / 'slides.tex').open().readline() '\\documentclass[20pt]{beamer}\n'
  18. statistics >>> data = [1e30, 1, 3, -1e30] >>> sum(data)

    / len(data) 0.0 >>> from statistics import mean >>> mean(data) 1.0
  19. __qualname__ → Pickle format 4

  20. inspect CLI $ python -m inspect re:search def search(pattern, string,

    flags=0): """Scan through string looking for a match to the pattern, returning a match object, or None if no match was found.""" return _compile(pattern, flags).search(string)
  21. re.fullmatch re.search('^abc$', string) re.match('abc$', string) re.fullmatch('abc', string)

  22. textwrap.shorten >>> textwrap.shorten('Hello world! You are beautiful!', 20) 'Hello world!

    [...]'
  23. with

  24. with with what?

  25. with with what? with contextlib.suppress: with contextlib.redirect_stdout:

  26. with with what? with contextlib.suppress: with contextlib.redirect_stdout: with unittest.TestCase.subTest(): with

    unittest.TestCase.assertLogs():
  27. with with what? with contextlib.suppress: with contextlib.redirect_stdout: with unittest.TestCase.subTest(): with

    unittest.TestCase.assertLogs(): with aifc.open(...): with sunau.open(...): with select.epoll(...): with dbm.open(...): with wave.open(...):
  28. signature → inspect, pydoc & help() improvements

  29. signature → inspect, pydoc & help() improvements unittest.mock → Mock

    objects now check signatures
  30. functools.partialmethod

  31. functools.partialmethod import functools class A: def greet(self, greeting, subject): print('{},

    {}!'.format(greeting, subject)) say_hello = functools.partialmethod(greet, 'Hello') A().say_hello('world') # --> Hello, world!
  32. functools.singledispatch

  33. functools.singledispatch from functools import singledispatch from collections.abc import Iterable @singledispatch

    def upper(arg): raise NotImplementedError() @upper.register(str) def _(arg): return arg.upper() @upper.register(Iterable) def _(arg): return [upper(a) for a in arg] print(upper('abc')) # --> ABC print(upper(['a', 'b', 'c'])) # --> ['A', 'B', 'C']
  34. weakref.WeakMethod

  35. importlib simplification

  36. CPython improvements

  37. CPython improvements __del__ + ref loops? no problemo

  38. CPython improvements __del__ + ref loops? no problemo 30% faster

    startup
  39. docs.python.org/3.4/whatsnew asyncio ensurepip enum pathlib statistics pickle 4 inspect CLI

    re.fullmatch textwrap.shorten with signature support functools.partialmethod functools.singledispatch weakref.WeakMethod CPython improvements ... and more!