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

Things you didn't know about Python

Things you didn't know about Python

PyCon ZA 2012 presentation, not actually what the title says.

Armin Ronacher

October 05, 2012
Tweet

More Decks by Armin Ronacher

Other Decks in Programming

Transcript

  1. Things you didn't know about Python a presentation by Armin

    Ronacher for PyCon South Africa 2012 @mitsuhiko http://lucumr.pocoo.org/
  2. Things you didn't know about Python a presentation by Armin

    Ronacher for PyCon South Africa 2012 @mitsuhiko http://lucumr.pocoo.org/ might already know computers
  3. Things you didn't know about Python a presentation by Armin

    Ronacher for PyCon South Africa 2012 @mitsuhiko http://lucumr.pocoo.org/ might already know computers and the world!!!11
  4. We're using Python And not just us. Python has been

    popular in parts of in the gaming industry
  5. “First World Problems” Most of our problems with Python are

    not stopping us from using the language. It just might make it less pleasant.
  6. But really everybody Python is one of the things that

    just shows up. If for nothing else, then build scripts.
  7. Trivia: Dropbox uses Python Not just on the server, the

    client is also implemented in Python!
  8. Trivia: what did this do until 2.5? raise ((a, b),

    c), d answer: raises exception a with value d
  9. 1995: The Huge Jump to 1.5 • Regular Expressions •

    Exceptions as classes • Built-in package support • Embeddable
  10. Trivia: did you know re is 50% python? the regular

    expression compiler is written in Python You notice that when you python -mtrace
  11. Trivia: why are builtin types lowercase? because they used to

    be functions the types where in types.py types.StringType was the type of a string (camelcase)
  12. 2000: Modern Python: Python 2.0 • Unicode Support • Augmented

    assignments (+= etc.) • Garbage Collector • PEPs
  13. 2004: Python as you know it • File encoding cookies

    • Boolean types • sets • reverse iteration • generator expressions
  14. Trivia: 2.2.1 introduced True and False … but no boolean

    type. 2.2.0: no true/false 2.3.0: real boolean type
  15. Really Early Adopters Math and Scientific Community Python's operator overloading

    and simple syntax was very convenient for scientific uses.
  16. Other Factors Python was easy to extend with C extensions

    and starting with distutils it was possible to distribute them
  17. Windows! Python has had excellent Windows support in the past

    unlike many other programming languages that were created in the POSIX environment
  18. Trivia: Battlefield 2 used Python And since the engine is

    still used today there are free to play versions of Battlefield still using Python for scripting
  19. Web Development We slowly and steadily became a proven platform

    for the web Python is not the final answer there but an amazing platform to start
  20. Trivia: Early XMPP Transports Most of the XMPP to X

    transports were written in Python with Twisted
  21. I have yet to see a Wifi Hotspot Portal Page

    that is written in Python and sucks
  22. Disclaimer: I understand that this statement is very optimistic and

    bad Python code exists in practice, that there are frameworks in the Python community that advocate for sloppy code, that there are widely used modules with security problems or bad general design, that there are indeed Wifi hotspot login pages that are horrible and indeed written in Python, that there are well written Wifi login pages in PHP (I don't actually believe that), that I am hugely biased and that my sample size is waaaaaaaaaaaaaaaaay too small.
  23. What are Descriptors? • __get__ • __set__ • __delete__ •

    Common descriptors: functions, properties
  24. Example: Basic Descriptor Lookup >>> class Foo(object): ... def my_function(self):

    ... pass ... >>> Foo.my_function <unbound method Foo.my_function> >>> Foo.__dict__['my_function'] <function my_function at 0x1002e1410> >>> Foo.__dict__['my_function'].__get__(None, Foo) <unbound method Foo.my_function> >>> >>> Foo().my_function <bound method Foo.my_function of <__main__.Foo object at 0x1002e2710>> >>> Foo.__dict__['my_function'].__get__(Foo(), Foo) <bound method Foo.my_function of <__main__.Foo object at 0x1002e2750>>
  25. Example: Everyday Decorators >>> class Foo(object): ... @property ... def

    foo(self): ... return 'hello pycon' ... >>> Foo().foo 'hello pycon'
  26. Cached Properties missing = object() class cached_property(object): def __init__(self, func):

    self.func = func self.__name__ = func.__name__ self.__doc__ = func.__doc__ self.__module__ = func.__module__ def __get__(self, obj, type=None): if obj is None: return self value = obj.__dict__.get(self.__name__, missing) if value is missing: value = self.func(obj) obj.__dict__[self.__name__] = value return value
  27. Example: Cached Properties class Post(object): def __init__(self, text): self.text =

    text @cached_property def rendered_text(self): return markdown_to_html(self.text)
  28. Abstract Base Duck Typing • abc.ABCMeta — metaclass for abstract

    bases • collections.* — common abstract bases
  29. Abstract Base Duck Typing callable(x) -> isinstance(x, Callable) tryexcept(hash(x)) ->

    isinstance(x, Hashable) tryexcept(iter(x)) -> isinstance(x, Iterable) tryexcept(len(x)) -> isinstance(x, Sized) tryexcept(hasattr(x, ‘__contains__’)) -> isinstance(x, Container) -> isinstance(x, Mapping) isinstance(x, Set) isinstance(x, Sequence) isinstance(x, MutableMapping) isinstance(x, MutableSet) isinstance(x, MutableSequence)
  30. Example: Abstract Base Duck Typing >>> from collections import Iterator

    >>> class MyIter(object): ... def __iter__(self): ... return self ... def next(self): ... return 42 ... >>> isinstance(MyIter(), Iterator) True
  31. Custom Ducks from abc import ABCMeta, abstractmethod class Markedup(object): __metaclass__

    = ABCMeta @classmethod def __subclasshook__(cls, C): if cls is Markedup: if hasattr(C, "__html__"): return True return NotImplemented
  32. Example: Custom Ducks >>> class Markup(unicode): ... def __html__(self): ...

    return self ... >>> isinstance(Markup('test'), Markedup) True
  33. Tracking Imports import sys import __builtin__ real_import = __builtin__.__import__ def

    debug_import(name, locals=None, globals=None, fromlist=None, level=-1): glob = globals or sys._getframe(1).f_globals importer_name = glob and glob.get('__name__') or 'unknown' print '%s imports %s' % (importer_name, name) return real_import(name, locals, globals, fromlist, level) __builtin__.__import__ = debug_import
  34. Example: Tracking Imports >>> import urlparse __main__ imports urlparse urlparse

    imports collections collections imports _abcoll collections imports _collections collections imports operator collections imports keyword collections imports sys collections imports heapq heapq imports itertools heapq imports operator heapq imports bisect bisect imports _bisect heapq imports _heapq collections imports itertools
  35. Interpreter Frames def print_frame_info(frame): print 'module: %s' % frame.f_globals.get('__name__') print

    'filename: %s' % frame.f_code.co_filename print 'current line: %d' % frame.f_lineno loc = dict((k, v) for k, v in frame.f_locals.iteritems() if not k.startswith('__')) print 'local variables: %s' % loc
  36. Example: Interpreter Frames >>> import sys >>> print_frame_info(sys._getframe()) module: __main__

    filename: <stdin> current line: 1 local variables: { 'a': 2, 'b': 4, 'sys': <module 'sys' (built-in)>, 'print_frame_info': <function print_frame_info at 0x100484668> }
  37. Dumping Threads import sys import traceback def dump_threads(): for thread_id,

    frame in sys._current_frames().iteritems(): print 'Thread #%d' % thread_id print ''.join(traceback.format_stack(frame))
  38. Example: Dumping Threads >>> import time, threading >>> def foo():

    ... for x in xrange(10): ... time.sleep(1) ... >>> threading.Thread(target=foo).start() >>> dump_threads() Thread #4302381056 File "lib/python2.7/threading.py", line 483, in run self.__target(*self.__args, **self.__kwargs) File "<stdin>", line 3, in foo time.sleep(1) Thread #140735295412576 File "<stdin>", line 1, in <module> dump_threads() File "<stdin>", line 4, in dump_threads print ''.join(traceback.format_stack(frame)).rstrip()
  39. Slow Execution Monitor • Dump stacktrace if an API request

    runs longer than N seconds • Permanent background thread • Request start -> set marker • Request end -> remove marker • If marker active for more than N seconds -> log stacktrace
  40. Remote Console • All Python code has a thread that

    listens for requests on redis • Can be used to execute arbitrary Python code for debugging • Sends results back to redis
  41. Rich Logging • We log into Sentry error groups •

    all stacktraces on dev environments include local variables for all frames • Also speaks $language
  42. Memory Leak Finding • Walk over all objects the garbage

    collector can reach • Resolve weak references • Group by type • Log to top grossing to graphite every second
  43. virtualenv • it does not matter that packaging or the

    import system is broken • it could be so much worse • virtualenv makes the big chaos into many separate small chaoses
  44. Easy to Learn • Language can be picked up in

    weeks • It's a fun language to learn • It's very dynamic and leaves room for (crazy) experimentation
  45. An Amazing Community • Developers and people all around the

    world • embraces simple and truly open licenses • loves documentation • … now also loves testing