Upgrade to Pro — share decks privately, control downloads, hide ads and more …

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

Petr Viktorin

July 23, 2014
Tweet

More Decks by Petr Viktorin

Other Decks in Programming

Transcript

  1. Python 3.4
    (party!)
    Petr Viktorin
    [email protected]
    Brněnské Pyvo, 2014-03-27

    View Slide

  2. Python 3.3
    u'string'

    View Slide

  3. Python 3.3
    u'string'
    yield from

    View Slide

  4. Python 3.3
    u'string'
    yield from
    venv

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  8. Python 3.4
    2014-03-16

    View Slide

  9. yield from →
    asyncio

    View Slide

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

    View Slide

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

    View Slide

  12. yield from →
    asyncio
    "WSGI for event loops"
    twisted, tornado, gevent
    event loop, tasks, calbacks,
    futures/deferreds
    networking IO

    View Slide

  13. venv →
    venv + ensurepip

    View Slide

  14. Commonly reinvented wheels

    View Slide

  15. Commonly reinvented wheels
    belong in the standard library

    View Slide

  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

    View Slide

  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'

    View Slide

  18. statistics
    >>> data = [1e30, 1, 3, -1e30]
    >>> sum(data) / len(data)
    0.0
    >>> from statistics import mean
    >>> mean(data)
    1.0

    View Slide

  19. __qualname__ →
    Pickle format 4

    View Slide

  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)

    View Slide

  21. re.fullmatch
    re.search('^abc$', string)
    re.match('abc$', string)
    re.fullmatch('abc', string)

    View Slide

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

    View Slide

  23. with

    View Slide

  24. with
    with what?

    View Slide

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

    View Slide

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

    View Slide

  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(...):

    View Slide

  28. signature →
    inspect, pydoc & help()
    improvements

    View Slide

  29. signature →
    inspect, pydoc & help()
    improvements
    unittest.mock →
    Mock objects now check
    signatures

    View Slide

  30. functools.partialmethod

    View Slide

  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!

    View Slide

  32. functools.singledispatch

    View Slide

  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']

    View Slide

  34. weakref.WeakMethod

    View Slide

  35. importlib simplification

    View Slide

  36. CPython improvements

    View Slide

  37. CPython improvements
    __del__ + ref loops?
    no problemo

    View Slide

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

    View Slide

  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!

    View Slide