introduction to python 3.4 as of beta 1

45
Python 3.4

Upload: toru-furukawa

Post on 08-May-2015

800 views

Category:

Technology


1 download

DESCRIPTION

Python 3.4 で新規に追加されるライブラリの一部の紹介です。

TRANSCRIPT

Page 1: Introduction to Python 3.4 as of beta 1

Python 3.4

Page 2: Introduction to Python 3.4 as of beta 1

Python 3.4

@torufurukawa

プログラマ

39

ふるかわとおる

Page 3: Introduction to Python 3.4 as of beta 1

Python 3.4

• 2013 年 8 月 3 日 : alpha 1• 2013 年 9 月 9 日 : alpha 2• 2013 年 9 月 29 日 : alpha 3• 2013 年 10 月 20 日 : alpha 4• 2013 年 11 月 23 日 : @torufurukawa 誕生日• 2013 年 11 月 24 日 : beta 1---- 新機能追加はここまで ----

Page 4: Introduction to Python 3.4 as of beta 1

Python 3.4

• 2014 年 1 月 5 日 : beta 2• 2014 年 1 月 19 日 : candidate 1• 2014 年 2 月 2 日 : candidate 2• 2014 年 2 月 23 日 : final

Page 5: Introduction to Python 3.4 as of beta 1

What's New in Python 3.4

構文 / 文法の変更なし

Page 6: Introduction to Python 3.4 as of beta 1

What's New in Python 3.4

Page 7: Introduction to Python 3.4 as of beta 1

What's New in Python 3.4

追加ライブラリ• asyncio ... 非同期 I/O• enum ... 列挙型• ensurepip ... pip インストーラの管理• pathlib ... オブジェクト指向パス• selectors ... 高レベル I/O マルチプレクス• statistics ... 統計関数• tracemalloc ... メモリ確保のトレーサ

Page 8: Introduction to Python 3.4 as of beta 1

What's New in Python 3.4

実装• file descriptor• Isolated mode• Codecs for non-text encodings

Page 9: Introduction to Python 3.4 as of beta 1

What's New in Python 3.4

ライブラリの向上• Single-dispatch generic functions in functoools• New pickle protocol 4 • SHA-3 (Keccak) support for hashlib.• TLSv1.1 and TLSv1.2 support for ssl.• multiprocessing now has option to avoid using

os.fork() on Unix

Page 10: Introduction to Python 3.4 as of beta 1

What's New in Python 3.4

CPython implementation• Safe object finalization• Configurable memory allocators• Secure and interchangeable hash algorithm• Improve finalization of Python modules to avoid setting

their globals to None, in most cases (issue 18214).• A more efficient marshal format (issue 16475).• “Argument Clinic”, an initial step towards providing

improved introspection support for builtin and standard library extension types implemented in C (PEP 436)

Page 11: Introduction to Python 3.4 as of beta 1

ライブラリ見ましょう• もう新機能の追加はないし• 内部実装のこととか難しいし

Page 12: Introduction to Python 3.4 as of beta 1

asyncio

• 「 asyncore: included batteries do not fit 」• シングルスレッドで並行処理するライブ

ラリ– コルーチン– I/O マルチプレクス– イベントループ– etc.

Page 13: Introduction to Python 3.4 as of beta 1

asyncio

ドキュメントを見ると ...

免責事項 : ドキュメントは、まだ。Beta が終了する頃にはできてるといいな。それまでは PEP 読んで。

Page 14: Introduction to Python 3.4 as of beta 1

asyncio

Page 15: Introduction to Python 3.4 as of beta 1

asyncio

• 長い• 「 tulip 」がベースらしい• ぐぐる• リンクたらい回し• PyPI• PEP へリンク

Page 16: Introduction to Python 3.4 as of beta 1

asyncio

Page 17: Introduction to Python 3.4 as of beta 1

enum

モード、選択肢、戻り値の候補が固定されていて、かつ、比較が必要。

▼定数を安全に記述できる。

Page 18: Introduction to Python 3.4 as of beta 1

enum

>>> RED = 1>>> GREEN = 2

>>> SPAM = 1>>> HAM = 2>>> RED == SPAMTrue

〜 3.3

Page 19: Introduction to Python 3.4 as of beta 1

enum

>>> RED * HAM == HAMTrue

>>> RED1 # 分かりにくい

〜 3.3

Page 20: Introduction to Python 3.4 as of beta 1

enum

>>> RED = "color:red">>> GREEN = "color:green" # だるい

〜 3.3

Page 21: Introduction to Python 3.4 as of beta 1

enum

from enum import Enum

class Color(Enum): red = 1 green = 2

3.4

Page 22: Introduction to Python 3.4 as of beta 1

enum

>>> Color.red == Food.spamFalse

>>> Color.red * 2TypeError: unsupported operand type(s) for *: 'Color' and 'int'

>>> Color.redColor.red

3.4

Page 23: Introduction to Python 3.4 as of beta 1

enum

>>> class Color(Enum):... red = "#FF0000"... green = "#00FF00"

>>> Color.red.name"red">>> Color.red.value"#FF0000"

3.4

Page 24: Introduction to Python 3.4 as of beta 1

ensurepip

サードパーティのライブラリを使う。▼

パッケージインストーラが事実上はいってくる。

Page 25: Introduction to Python 3.4 as of beta 1

ensurepip

$ wget https://bitbucket.org/pypa/setuptools/src/1.4.1/ez_setup.py$ wget https://raw.github.com/pypa/pip/develop/contrib/get-pip.py$ ez_setup.py$ get-pip.py$ pip install virtualenv$ virtualenv myenv

〜 3.3

http://pelican.aodag.jp/python34noensurepipsoretopyvenvnogeng-xin.html

Page 26: Introduction to Python 3.4 as of beta 1

ensurepip

• pip がインストールされた状態にするためのライブラリ

• make install したら呼び出されてる• 本家の OS X 用インストーラでも呼ばれて

いる

Page 27: Introduction to Python 3.4 as of beta 1

ensurepip

Page 28: Introduction to Python 3.4 as of beta 1

ensurepip

$ pip ipython

$ pyvenv-3.4 venv$ venv/bin/pip install kaaedit

3.4

Page 29: Introduction to Python 3.4 as of beta 1

pathlib

パス、ディレクトリ、ファイルを操作する。▼

パスをオブジェクトっぽく操作できる。

Page 30: Introduction to Python 3.4 as of beta 1

pathlib

import os.path

parent_dir = ( os.path.abspath( os.path.dirname( os.path.dirname(__file__) ) ))

〜 3.3

Page 31: Introduction to Python 3.4 as of beta 1

pathlib

from os.path import dirname, abspath

parent_dir = ( abspath(dirname(dirname(__file__)))

〜 3.3

Page 32: Introduction to Python 3.4 as of beta 1

pathlib

from os.path import (dirname, abspath, join)

target_dir = abspath( join( dirname(dirname(__file__)), 'target' ))

〜 3.3

Page 33: Introduction to Python 3.4 as of beta 1

pathlib

from glob import globfor p in glob(os.path.join(t, '*.py')): # do something ...

fin = open(p)

〜 3.3

Page 34: Introduction to Python 3.4 as of beta 1

pathlib

from pathlib import Path

p = Path(__file__).parent.resolve()t = p.joinpath('target')t = p / 'target'

3.4

Page 35: Introduction to Python 3.4 as of beta 1

pathlib

for p in target_dir.glob('*.py'): pass

fin = p.open()

3.4

Page 36: Introduction to Python 3.4 as of beta 1

selectors

I/O をイベント駆動で書きたいんだけど、select/poll/epoll の薄いラッパだとだるい

▼I/O マルチプレクスを手軽に。

Page 37: Introduction to Python 3.4 as of beta 1

selectors

• select モジュールが低レベルすぎる• 環境ごとに自力で選ぶ– /dev/poll ... Solaris– epoll ... Linux– poll ... どこでも– kqueue ... BSD– select ... どこでも

• イベントの種類も個別に違うし

Page 38: Introduction to Python 3.4 as of beta 1

selectors

s = selectors.DefaultSelector()s.register(mysocket, selectors.EVENT_READ)

for key, events in s.select(): if events & selectors.EVENT_READ: print(key.fileobj.recv(1024))

3.4

Page 39: Introduction to Python 3.4 as of beta 1

statistics

ちょっとした統計が必要▼

平均、標準偏差なんかを正確に計算する。

Page 40: Introduction to Python 3.4 as of beta 1

statistics

>>> def var(data):... n = len(data)... ss = sum(x**2 for x in data) –... (sum(data)**2)/n... return ss/(n-1)

>>> var([1,2,3,4]))1.6666666666666667>>> var([x+1e12 for x in [1,2,3,4]]*100))-1377834120.02005

〜 3.3

Page 41: Introduction to Python 3.4 as of beta 1

statistics

import numpya = numpy.array(data, float)a.var()

〜 3.3

Page 42: Introduction to Python 3.4 as of beta 1

statistics

import statistics

statistics.variance([1,2,3,4]))statistics.variance([x+1e12 for x in [1,2,3,4]]*100))

3.4

Page 43: Introduction to Python 3.4 as of beta 1

tracemalloc

import tracemalloctracemalloc.start()

x = [str(v) for v in range(1000)]

snapshot = tracemalloc.take_snapshot()for stat in snapshot.statistics('lineno'): print(stat)

Page 44: Introduction to Python 3.4 as of beta 1

tracemalloc

foo.py:7: size=59.4 KiB, count=1001, average=61 Bfoo.py:6: size=28 B, count=1, average=28 B

Page 45: Introduction to Python 3.4 as of beta 1

おしまい