Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Job Security (in Python) (Christopher Neugebauer)

Job Security (in Python) (Christopher Neugebauer)

PyCon Canada

August 25, 2013
Tweet

More Decks by PyCon Canada

Other Decks in Education

Transcript

  1. Hello Canadia!

    View Slide

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

    View Slide

  3. Hello!

    View Slide

  4. Why do people
    code Python?

    View Slide

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

    View Slide

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

    View Slide

  7. PEP 0008
    “Style Guide for Python Code”

    View Slide

  8. View Slide

  9. Readability Sucks.

    View Slide

  10. 1. People can
    comprehend your code.

    View Slide

  11. 2. You can maintain
    your own code.

    View Slide

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

    View Slide

  13. THIS IS ALL BAD.

    View Slide

  14. How do I write
    unmaintainable code?

    View Slide

  15. 1) Obvious
    variable names

    View Slide

  16. 1) Obvious
    (to you)
    variable names

    View Slide

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

    View Slide

  18. MerryPoppins.captain_america(matt_damon, james_bond)

    View Slide

  19. Spiderman.spiderman(spiderman)

    View Slide

  20. 2) Metaprogramming

    View Slide

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

    View Slide

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

    View Slide

  23. >>> 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

    View Slide

  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)

    View Slide

  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))

    View Slide

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

    View Slide

  27. 3) Monkey patching

    View Slide

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

    View Slide

  29. Roll your own
    standard library

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  34. - End -

    View Slide

  35. This talk was a joke.

    View Slide

  36. Don’t do this.

    View Slide

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

    View Slide