Slide 1

Slide 1 text

Effective Modern Python 2018 Ryo Takahashi 1 / 19

Slide 2

Slide 2 text

Contents Type Annotation Data Classes Packaging and Dependency Management Effective Modern Python 2018 2 / 19

Slide 3

Slide 3 text

Type Annotation python python 3.5 3.5 3 / 19

Slide 4

Slide 4 text

Benefits of type annotations Static code analysis prior to runtime PyCharm (IntelliJ) mypy Self­documenting Code itself is a document Code completion Effective Modern Python 2018 4 / 19

Slide 5

Slide 5 text

Chainer RNNLM Example train, val, test = chainer.datasets.get_ptb_words() Type of train, val, test? What kind of operations can be done on these variables? Effective Modern Python 2018 5 / 19

Slide 6

Slide 6 text

Effective Modern Python 2018 6 / 19

Slide 7

Slide 7 text

Go to Declaration def get_ptb_words(): """Gets the Penn Tree Bank dataset as long word sequences. [...] Returns: tuple of numpy.ndarray: Int32 vectors of word IDs. """ train = _retrieve_ptb_words('train.npz', _train_url) valid = _retrieve_ptb_words('valid.npz', _valid_url) test = _retrieve_ptb_words('test.npz', _test_url) return train, valid, test docstring IDEs can't parse complicated types Effective Modern Python 2018 7 / 19

Slide 8

Slide 8 text

Add Type Annotations from typing import Dict def load_vocab(vocabfile: str) -> Dict[int, str]: [...] # typed! vocab = load_vocab(vocabfile) Effective Modern Python 2018 8 / 19

Slide 9

Slide 9 text

Add Type Annotations If you can't modify function declaration... # function of other libraries def load_vocab(vocabfile): [...] from typing import Dict # typed! vocab: Dict[int, str] = load_vocab(vocabfile) Effective Modern Python 2018 9 / 19

Slide 10

Slide 10 text

Effective Modern Python 2018 10 / 19

Slide 11

Slide 11 text

Data Classes python python 3.7 3.7 11 / 19

Slide 12

Slide 12 text

Easier Way to Write Classes from dataclasses import dataclass from typing import Dict, Optional @dataclass class Vocabulary: padding_token: str oov_token: str max_vocab_size: int pretrained_files: Optional[Dict[str, str]] = None Similar to Scala's Case Classes Generate __init__, __eq__, and __repr__ automatically Effective Modern Python 2018 12 / 19

Slide 13

Slide 13 text

Conventional Class class Vocabulary: def __init__( self, padding_token: str, oov_token: str, max_vocab_size: int, pretrained_files: Optional[Dict[str, str]] = None, ): self.padding_token = padding_token self.oov_token = oov_token self.max_vocab_size = max_vocab_size self.pretrained_files = pretrained_files def __eq__(self, other): if isinstance(self, other.__class__): return self.__dict__ == other.__dict__ return False Effective Modern Python 2018 13 / 19

Slide 14

Slide 14 text

Packaging and Dependency Management 14 / 19

Slide 15

Slide 15 text

How to make your program pip installable? Other languages have great tools Scala: sbt JavaScript: npm, yarn Rust: cargo Go: dep Python: ... Effective Modern Python 2018 15 / 19

Slide 16

Slide 16 text

Ancient Way Dump all installed packages and force to download them all: $ pip freeze > requirements.txt $ pip install -r requirements.txt Need to manage virtual environment at your own responsibility Don't you have unnecessary dependency? How is version compatibility of Python and libraries? Metadata management? Effective Modern Python 2018 16 / 19

Slide 17

Slide 17 text

Medieval Way Use standard Setuptools Messy documentation Need to write messy setup.py Effective Modern Python 2018 17 / 19

Slide 18

Slide 18 text

Modern Way Use Pipenv, Hatch, Flit, or Poetry Work as another pip Clean documentation I personally recommend Poetry $ poetry init $ poetry add chainer # equivalent to `pip install chainer` $ poetry build # make wheels $ poetry publish # upload to PyPI # pip installable after this command Effective Modern Python 2018 18 / 19

Slide 19

Slide 19 text

Further Reading Type Annotation The other (great) benefit of Python type annotations Data Classes The Ultimate Guide to Data Classes in Python 3.7 – Real Python Packaging and Dependency Management Python's New Package Landscape How to Publish an Open­Source Python Package to PyPI – Real Python Effective Modern Python 2018 19 / 19