Slide 1

Slide 1 text

Migrating from to and David Winterbottom @codeinthehole and

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

•2010: In-house PHP framework •2012: Python, Django

Slide 4

Slide 4 text

Why? •General purpose language •Elegant, readable •BDFL

Slide 5

Slide 5 text

Today •Introduction to Python •Introduction to Django

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

Zen of python

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

What’s http://www.davidshrigley.com/photo_htmpgs/lost_pidgeon.html

Slide 13

Slide 13 text

Cruft •Significant whitespace •() ; not required •Readability counts!

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

No content

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

Privacy •Nothing truly private •_name not in public API •__name mangled

Slide 18

Slide 18 text

switch () {}

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

No content

Slide 21

Slide 21 text

OO •Type-hinting •Interfaces •Duck typing

Slide 22

Slide 22 text

No content

Slide 23

Slide 23 text

Don’t check type

Slide 24

Slide 24 text

Don’t check type

Slide 25

Slide 25 text

No content

Slide 26

Slide 26 text

No content

Slide 27

Slide 27 text

No content

Slide 28

Slide 28 text

Server set-up • Easy: apache2, mod_php • Tricky: apache2, mod_wsgi, ... • ...but getting better: • Servers: gunicorn, uWSGI • Platforms: ep.io, gondor.io

Slide 29

Slide 29 text

No content

Slide 30

Slide 30 text

>>> dir() ['__builtins__', '__doc__', '__name__', '__package__'] >>> dir(__builtins__) ['ArithmeticError', 'AssertionError', 'AttributeError', 'BaseException', 'BufferError', 'BytesWarning', 'DeprecationWarning', 'EOFError', 'Ellipsis', 'EnvironmentError', 'Exception', 'False', 'FloatingPointError', 'FutureWarning', 'GeneratorExit', 'IOError', 'ImportError', 'ImportWarning', 'IndentationError', 'IndexError', 'KeyError', 'KeyboardInterrupt', 'LookupError', 'MemoryError', 'NameError', 'None', 'NotImplemented', 'NotImplementedError', 'OSError', 'OverflowError', 'PendingDeprecationWarning', 'ReferenceError', 'RuntimeError', 'RuntimeWarning', 'StandardError', 'StopIteration', 'SyntaxError', 'SyntaxWarning', 'SystemError', 'SystemExit', 'TabError', 'True', 'TypeError', 'UnboundLocalError', 'UnicodeDecodeError', 'UnicodeEncodeError', 'UnicodeError', 'UnicodeTranslateError', 'UnicodeWarning', 'UserWarning', 'ValueError', 'Warning', 'ZeroDivisionError', '_', '__debug__', '__doc__', '__import__', '__name__', '__package__', 'abs', 'all', 'any', 'apply', 'basestring', 'bin', 'bool', 'buffer', 'bytearray', 'bytes', 'callable', 'chr', 'classmethod', 'cmp', 'coerce', 'compile', 'complex', 'copyright', 'credits', 'delattr', 'dict', 'dir', 'divmod', 'enumerate', 'eval', 'execfile', 'exit', 'file', 'filter', 'float', 'format', 'frozenset', 'getattr', 'globals', 'hasattr', 'hash', 'help', 'hex', 'id', 'input', 'int', 'intern', 'isinstance', 'issubclass', 'iter', 'len', 'license', 'list', 'locals', 'long', 'map', 'max', 'memoryview', 'min', 'next', 'object', 'oct', 'open', 'ord', 'pow', 'print', 'property', 'quit', 'range', 'raw_input', 'reduce', 'reload', 'repr', 'reversed', 'round', 'set', 'setattr', 'slice', 'sorted', 'staticmethod', 'str', 'sum', 'super', 'tuple', 'type', 'unichr', 'unicode', 'vars', 'xrange', 'zip'] built-ins

Slide 31

Slide 31 text

>>> dir() ['__builtins__', '__doc__', '__name__', '__package__'] >>> dir(__builtins__) ['ArithmeticError', 'AssertionError', 'AttributeError', 'BaseException', 'BufferError', 'BytesWarning', 'DeprecationWarning', 'EOFError', 'Ellipsis', 'EnvironmentError', 'Exception', 'False', 'FloatingPointError', 'FutureWarning', 'GeneratorExit', 'IOError', 'ImportError', 'ImportWarning', 'IndentationError', 'IndexError', 'KeyError', 'KeyboardInterrupt', 'LookupError', 'MemoryError', 'NameError', 'None', 'NotImplemented', 'NotImplementedError', 'OSError', 'OverflowError', 'PendingDeprecationWarning', 'ReferenceError', 'RuntimeError', 'RuntimeWarning', 'StandardError', 'StopIteration', 'SyntaxError', 'SyntaxWarning', 'SystemError', 'SystemExit', 'TabError', 'True', 'TypeError', 'UnboundLocalError', 'UnicodeDecodeError', 'UnicodeEncodeError', 'UnicodeError', 'UnicodeTranslateError', 'UnicodeWarning', 'UserWarning', 'ValueError', 'Warning', 'ZeroDivisionError', '_', '__debug__', '__doc__', '__import__', '__name__', '__package__', 'abs', 'all', 'any', 'apply', 'basestring', 'bin', 'bool', 'buffer', 'bytearray', 'bytes', 'callable', 'chr', 'classmethod', 'cmp', 'coerce', 'compile', 'complex', 'copyright', 'credits', 'delattr', 'dict', 'dir', 'divmod', 'enumerate', 'eval', 'execfile', 'exit', 'file', 'filter', 'float', 'format', 'frozenset', 'getattr', 'globals', 'hasattr', 'hash', 'help', 'hex', 'id', 'input', 'int', 'intern', 'isinstance', 'issubclass', 'iter', 'len', 'license', 'list', 'locals', 'long', 'map', 'max', 'memoryview', 'min', 'next', 'object', 'oct', 'open', 'ord', 'pow', 'print', 'property', 'quit', 'range', 'raw_input', 'reduce', 'reload', 'repr', 'reversed', 'round', 'set', 'setattr', 'slice', 'sorted', 'staticmethod', 'str', 'sum', 'super', 'tuple', 'type', 'unichr', 'unicode', 'vars', 'xrange', 'zip'] built-ins

Slide 32

Slide 32 text

>>> dir() ['__builtins__', '__doc__', '__name__', '__package__'] >>> dir(__builtins__) ['ArithmeticError', 'AssertionError', 'AttributeError', 'BaseException', 'BufferError', 'BytesWarning', 'DeprecationWarning', 'EOFError', 'Ellipsis', 'EnvironmentError', 'Exception', 'False', 'FloatingPointError', 'FutureWarning', 'GeneratorExit', 'IOError', 'ImportError', 'ImportWarning', 'IndentationError', 'IndexError', 'KeyError', 'KeyboardInterrupt', 'LookupError', 'MemoryError', 'NameError', 'None', 'NotImplemented', 'NotImplementedError', 'OSError', 'OverflowError', 'PendingDeprecationWarning', 'ReferenceError', 'RuntimeError', 'RuntimeWarning', 'StandardError', 'StopIteration', 'SyntaxError', 'SyntaxWarning', 'SystemError', 'SystemExit', 'TabError', 'True', 'TypeError', 'UnboundLocalError', 'UnicodeDecodeError', 'UnicodeEncodeError', 'UnicodeError', 'UnicodeTranslateError', 'UnicodeWarning', 'UserWarning', 'ValueError', 'Warning', 'ZeroDivisionError', '_', '__debug__', '__doc__', '__import__', '__name__', '__package__', 'abs', 'all', 'any', 'apply', 'basestring', 'bin', 'bool', 'buffer', 'bytearray', 'bytes', 'callable', 'chr', 'classmethod', 'cmp', 'coerce', 'compile', 'complex', 'copyright', 'credits', 'delattr', 'dict', 'dir', 'divmod', 'enumerate', 'eval', 'execfile', 'exit', 'file', 'filter', 'float', 'format', 'frozenset', 'getattr', 'globals', 'hasattr', 'hash', 'help', 'hex', 'id', 'input', 'int', 'intern', 'isinstance', 'issubclass', 'iter', 'len', 'license', 'list', 'locals', 'long', 'map', 'max', 'memoryview', 'min', 'next', 'object', 'oct', 'open', 'ord', 'pow', 'print', 'property', 'quit', 'range', 'raw_input', 'reduce', 'reload', 'repr', 'reversed', 'round', 'set', 'setattr', 'slice', 'sorted', 'staticmethod', 'str', 'sum', 'super', 'tuple', 'type', 'unichr', 'unicode', 'vars', 'xrange', 'zip'] built-ins

Slide 33

Slide 33 text

No content

Slide 34

Slide 34 text

TypeError >>> 'hello' + 100 Traceback (most recent call last): File "", line 1, in TypeError: cannot concatenate 'str' and 'int' objects >>> duck, dynamic, strong

Slide 35

Slide 35 text

TypeError >>> 'hello' + 100 Traceback (most recent call last): File "", line 1, in TypeError: cannot concatenate 'str' and 'int' objects >>> duck, dynamic, strong

Slide 36

Slide 36 text

TypeError >>> 'hello' + 100 Traceback (most recent call last): File "", line 1, in TypeError: cannot concatenate 'str' and 'int' objects >>> duck, dynamic, strong

Slide 37

Slide 37 text

TypeError >>> 'hello' + 100 Traceback (most recent call last): File "", line 1, in TypeError: cannot concatenate 'str' and 'int' objects >>> duck, dynamic, strong

Slide 38

Slide 38 text

Data-structures •PHP: array() •tuple(), dict(), set(), list(), , ... •... deque(), defaultdict() - collections

Slide 39

Slide 39 text

Inheritance multiple inheritance

Slide 40

Slide 40 text

Modules! •Multiple classes per file •Static methods not needed

Slide 41

Slide 41 text

No content

Slide 42

Slide 42 text

integer division

Slide 43

Slide 43 text

integer division

Slide 44

Slide 44 text

integer division

Slide 45

Slide 45 text

integer division

Slide 46

Slide 46 text

mutable defaults

Slide 47

Slide 47 text

mutable defaults

Slide 48

Slide 48 text

mutable defaults

Slide 49

Slide 49 text

mutable defaults

Slide 50

Slide 50 text

names and objects

Slide 51

Slide 51 text

names and objects

Slide 52

Slide 52 text

names and objects

Slide 53

Slide 53 text

names and objects

Slide 54

Slide 54 text

Demo http://people.csail.mit.edu/pgbovine/python/tutor.html

Slide 55

Slide 55 text

class attributes

Slide 56

Slide 56 text

class attributes

Slide 57

Slide 57 text

class attributes

Slide 58

Slide 58 text

No content

Slide 59

Slide 59 text

Circular references •Misleading errors messages •Environment-specific •Divide and conquer

Slide 60

Slide 60 text

The good stuff

Slide 61

Slide 61 text

Interpreter console

Slide 62

Slide 62 text

ipython

Slide 63

Slide 63 text

pdb

Slide 64

Slide 64 text

pdb

Slide 65

Slide 65 text

pdb

Slide 66

Slide 66 text

pip

Slide 67

Slide 67 text

pip

Slide 68

Slide 68 text

pip

Slide 69

Slide 69 text

virtualenn •virtualenvwrapper •workon django virtualenv

Slide 70

Slide 70 text

Testing •unittest ~ JUnit •nose, mock, coverage, pyhamcrest, twill, lettuce, tox

Slide 71

Slide 71 text

No content

Slide 72

Slide 72 text

docs

Slide 73

Slide 73 text

server runserver

Slide 74

Slide 74 text

admin

Slide 75

Slide 75 text

class-based views

Slide 76

Slide 76 text

No content

Slide 77

Slide 77 text

1. Placing the project's primary focus on the core domain and domain logic

Slide 78

Slide 78 text

1. Placing the project's primary focus on the core domain and domain logic 2. Basing complex design on a model

Slide 79

Slide 79 text

1. Placing the project's primary focus on the core domain and domain logic 2. Basing complex design on a model 3. Developers and domain experts collaborate to iteratively improve the model

Slide 80

Slide 80 text

No content

Slide 81

Slide 81 text

Models •Capture the “conceptual heart of the domain” •DB schema generated for you •Foundation for application

Slide 82

Slide 82 text

“...where powerful new features unfold as corollaries to older features.”

Slide 83

Slide 83 text

Libraries

Slide 84

Slide 84 text

django-extensions •Various useful utilities •runserver_plus (demo)

Slide 85

Slide 85 text

south •Database schema migrations •Data migrations •Not a magic bullet though

Slide 86

Slide 86 text

debug-toolbar •Inspect SQL queries •Template contexts •Signals, logging, ...

Slide 87

Slide 87 text

Tips •Do a pure python project •Read top questions on Stack Overflow •Get your IDE set-up

Slide 88

Slide 88 text

Teams •Books •Pairing •Dojos and workshops

Slide 89

Slide 89 text

Summary

Slide 90

Slide 90 text

Python :) •Clean and readable •Low learning curve •Rich eco-system of tools and libraries

Slide 91

Slide 91 text

Python :( •No-one wants to go back •Harder to hire •Low-lowel hosting

Slide 92

Slide 92 text

Thanks