Job Security (in Python) (Christopher Neugebauer)

Job Security (in Python) (Christopher Neugebauer)

3b085ba94fee217d7656971b0cb4cf00?s=128

PyCon Canada

August 25, 2013
Tweet

Transcript

  1. Hello Canadia!

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

  3. Hello!

  4. Why do people code Python?

  5. “Readability” -- Raymond Hettinger, PyCon US 2013

  6. “Readability Counts” -- Tim Peters, “The Zen of Python” (import

    this)
  7. PEP 0008 “Style Guide for Python Code”

  8. None
  9. Readability Sucks.

  10. 1. People can comprehend your code.

  11. 2. You can maintain your own code.

  12. 3. Your code will be more readily reusable.

  13. THIS IS ALL BAD.

  14. How do I write unmaintainable code?

  15. 1) Obvious variable names

  16. 1) Obvious (to you) variable names

  17. • Callables: Superheroes • Classes: Musical characters • Strings: Famous

    actors • Numbers: Movie characters
  18. MerryPoppins.captain_america(matt_damon, james_bond)

  19. Spiderman.spiderman(spiderman)

  20. 2) Metaprogramming

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

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

  23. >>> c = Collection() >>> c.set_spam(“eggs”) Traceback (most recent call

    last): File "<stdin>", line 1, in <module> File "<stdin>", line 10, in __getattribute__ TypeError: instancemethod expected at least 2 arguments, got 0
  24. 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)
  25. 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))
  26. c.ihasa_spam("foo") -> c.set_spam(“foo”) c.canhas_spam() -> c.get_spam()

  27. 3) Monkey patching

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

  29. Roll your own standard library

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

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

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

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

  34. - End -

  35. This talk was a joke.

  36. Don’t do this.

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