Slide 1

Slide 1 text

Hello Canadia!

Slide 2

Slide 2 text

Job Security (in Python) Christopher Neugebauer @chrisjrn http://chris.neugebauer.id.au

Slide 3

Slide 3 text

Hello!

Slide 4

Slide 4 text

Why do people code Python?

Slide 5

Slide 5 text

“Readability” -- Raymond Hettinger, PyCon US 2013

Slide 6

Slide 6 text

“Readability Counts” -- Tim Peters, “The Zen of Python” (import this)

Slide 7

Slide 7 text

PEP 0008 “Style Guide for Python Code”

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

Readability Sucks.

Slide 10

Slide 10 text

1. People can comprehend your code.

Slide 11

Slide 11 text

2. You can maintain your own code.

Slide 12

Slide 12 text

3. Your code will be more readily reusable.

Slide 13

Slide 13 text

THIS IS ALL BAD.

Slide 14

Slide 14 text

How do I write unmaintainable code?

Slide 15

Slide 15 text

1) Obvious variable names

Slide 16

Slide 16 text

1) Obvious (to you) variable names

Slide 17

Slide 17 text

• Callables: Superheroes • Classes: Musical characters • Strings: Famous actors • Numbers: Movie characters

Slide 18

Slide 18 text

MerryPoppins.captain_america(matt_damon, james_bond)

Slide 19

Slide 19 text

Spiderman.spiderman(spiderman)

Slide 20

Slide 20 text

2) Metaprogramming

Slide 21

Slide 21 text

class Collection(Magic): def get_spam(self): return self.spam def set_spam(self, default): self.spam = default

Slide 22

Slide 22 text

>>> c = Collection() >>> c.set_spam(“eggs”)

Slide 23

Slide 23 text

>>> c = Collection() >>> c.set_spam(“eggs”) Traceback (most recent call last): File "", line 1, in File "", line 10, in __getattribute__ TypeError: instancemethod expected at least 2 arguments, got 0

Slide 24

Slide 24 text

Help on Collection in module __main__ object: class Collection(Magic) | Method resolution order: | Collection | Magic | __builtin__.object | | Methods defined here: | | get_spam(self) | | set_spam(self, default)

Slide 25

Slide 25 text

TRANSLATE = (("get","_bork_"), ("set","_bork_"), ("canhas" , "get"), ("ihasa" , "set")) class Magic(object): def __getattribute__(self, attribute): oa = attribute for (key,val) in TRANSLATE: attribute = attribute.replace(key, val) try: return object. __getattribute__(self,attribute) except AttributeError: return type(object. __getattribute__(self, oa))

Slide 26

Slide 26 text

c.ihasa_spam("foo") -> c.set_spam(“foo”) c.canhas_spam() -> c.get_spam()

Slide 27

Slide 27 text

3) Monkey patching

Slide 28

Slide 28 text

“A batteries-included standard library” -- Raymond Hettinger, PyCon US 2013

Slide 29

Slide 29 text

Roll your own standard library

Slide 30

Slide 30 text

>>> import math >>> math.cos(0)

Slide 31

Slide 31 text

>>> import math >>> math.cos(0) 0.0

Slide 32

Slide 32 text

>>> import magic >>> import math >>> math.cos(0) 0.0

Slide 33

Slide 33 text

import math math.cos, math.sin = math.sin, math.cos

Slide 34

Slide 34 text

- End -

Slide 35

Slide 35 text

This talk was a joke.

Slide 36

Slide 36 text

Don’t do this.

Slide 37

Slide 37 text

The end. Christopher Neugebauer @chrisjrn http://chris.neugebauer.id.au