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

Stdlib Safari - Exotic Animal Edition

Baptiste Mispelon
June 26, 2013
150

Stdlib Safari - Exotic Animal Edition

Little-known Features and Modules of Python's Standard Library

Baptiste Mispelon

June 26, 2013
Tweet

Transcript

  1. About me Baptiste Mispelon (bmispelon) I've been using python for

    over 5 years. Currently doing web development with Django at M2BPO.
  2. Part 1: Builtins Python2 has 80 builtin functions. Python3 has

    69. That's not a lot, especially compared to a certain scriPting language whose name sHall not be sPoken.
  3. Builtins Table (python2) abs complex getattr locals range super all

    delattr globals long raw_input tuple any dict hasattr map reduce type dir hash max reload unichr basestring divmod help memoryview repr unicode bin enumerate hex min reversed vars bool eval id next round xrange bytearray input object set zip execfile int oct setattr __import__ callable file isinstance open slice apply chr filter issubclass ord sorted buffer classmethod float iter pow staticmethod coerce cmp format len print str intern compile frozenset list property sum
  4. Builtins Table (python3) abs complex getattr locals range super all

    delattr globals tuple any dict hasattr map type ascii dir hash max unichr divmod help memoryview repr bin enumerate hex min reversed vars bool eval id next round bytearray exec input object set zip bytes int oct setattr __import__ callable isinstance open slice chr filter issubclass ord sorted classmethod float iter pow staticmethod
  5. Example What are all the public attributes of this object?

    obj = 'hello world' [attr for attr in dir(obj) if not attr.startswith('_')]
  6. Example What are all the public attributes of this object?

    obj = 'hello world' [attr for attr in dir(obj) if not attr.startswith('_')] # ['capitalize', 'casefold', 'center', # 'count', 'encode', 'endswith', ...]
  7. Example a, b = 42, 5 q, r = a

    // b, a % b q, r # ?
  8. Example a, b = 42, 5 q, r = a

    // b, a % b q, r # 8, 2
  9. With slice s = 'abcdefghi' s[ :8] == s[slice(8)] s[1:8]

    == s[slice(1, 8)] s[1:8:2] == s[slice(1, 8, 2)]
  10. Restrictions Elements must be hashable (like keys of a dict)

    Not indexable: some_set[0] does not work. No order (like a dict)
  11. Set comprehensions Since python 2.7 (too): set(w.lower() for w in

    wordlist) # Is the same as doing {w.lower() for w in wordlist}
  12. Operators intersection: s1 & s2 union: s1 | s2 difference:

    s1 - s2 symmetric difference: s1 ^ s2 Subset/Superset: s1 < s2
  13. Examples set('abababc') # {'a', 'c', 'b'} {1, 2} & {2,

    3} {1, 2} | {2, 3} {1, 2} - {2, 3} {1, 2} ^ {2, 3} {1, 2} < {2, 3} {1, 2} < {3, 2, 1}
  14. Examples set('abababc') # {'a', 'c', 'b'} {1, 2} & {2,

    3} # {2} {1, 2} | {2, 3} {1, 2} - {2, 3} {1, 2} ^ {2, 3} {1, 2} < {2, 3} {1, 2} < {3, 2, 1}
  15. Examples set('abababc') # {'a', 'c', 'b'} {1, 2} & {2,

    3} # {2} {1, 2} | {2, 3} # {1, 2, 3} {1, 2} - {2, 3} {1, 2} ^ {2, 3} {1, 2} < {2, 3} {1, 2} < {3, 2, 1}
  16. Examples set('abababc') # {'a', 'c', 'b'} {1, 2} & {2,

    3} # {2} {1, 2} | {2, 3} # {1, 2, 3} {1, 2} - {2, 3} # {1} {1, 2} ^ {2, 3} {1, 2} < {2, 3} {1, 2} < {3, 2, 1}
  17. Examples set('abababc') # {'a', 'c', 'b'} {1, 2} & {2,

    3} # {2} {1, 2} | {2, 3} # {1, 2, 3} {1, 2} - {2, 3} # {1} {1, 2} ^ {2, 3} # {1, 3} {1, 2} < {2, 3} {1, 2} < {3, 2, 1}
  18. Examples set('abababc') # {'a', 'c', 'b'} {1, 2} & {2,

    3} # {2} {1, 2} | {2, 3} # {1, 2, 3} {1, 2} - {2, 3} # {1} {1, 2} ^ {2, 3} # {1, 3} {1, 2} < {2, 3} # False {1, 2} < {3, 2, 1}
  19. Examples set('abababc') # {'a', 'c', 'b'} {1, 2} & {2,

    3} # {2} {1, 2} | {2, 3} # {1, 2, 3} {1, 2} - {2, 3} # {1} {1, 2} ^ {2, 3} # {1, 3} {1, 2} < {2, 3} # False {1, 2} < {3, 2, 1} # True
  20. Why? Making it immutable makes it hashable. This means we

    can use it as the key of a dictionnary. Or put it in another set/frozenset.
  21. Why? Making it immutable makes it hashable. This means we

    can use it as the key of a dictionnary. Or put it in another set/frozenset. (Yo Dawg, I heard you like sets...)
  22. iter Make an iterator out of an iterable. Iterable An

    object that can be iterated over (for x in y) Iterator A type of iterable that can only go forward.
  23. Example l = list('abc') ''.join(c.upper() for c in l) #

    'ABC' ''.join(c.upper() for c in l) # 'ABC'
  24. Example (2) i = iter('abc') ''.join(c.upper() for c in i)

    # 'ABC' ''.join(c.upper() for c in i) # ?
  25. Example (2) i = iter('abc') ''.join(c.upper() for c in i)

    # 'ABC' ''.join(c.upper() for c in i) # ''
  26. The Twist If you pass a second argument, iter behaves

    quite differently. iter(callable, sentinel)
  27. iter(callable, sentinel) Call the first argument yielding the return value

    and stopping when the sentinel value is reached (or when the call raises StopIteration).
  28. Example (2) from random import choice roll = lambda: choice(range(1,

    7)) list(iter(roll, 6)) # [] list(iter(roll, 6)) # [4, 4, 1]
  29. Example i = iter('abc') next(i) # 'a' next(i) # 'b'

    next(i) # 'c' next(i) # StopIteration
  30. The Twist next takes a second argument. Similar to dict.get,

    it's a value to return if the iterator is empty (instead of raising StopIteration).
  31. Example (part 2) i = iter('abc') next(i, 'x') # 'a'

    next(i, 'x') # 'b' next(i, 'x') # 'c' next(i, 'x') # ?
  32. Example (part 2) i = iter('abc') next(i, 'x') # 'a'

    next(i, 'x') # 'b' next(i, 'x') # 'c' next(i, 'x') # 'x'
  33. Imaginary friends Did you know that python has first class

    support for complex numbers? a = complex(real=4, imag=1) b = complex(real=1, imag=2) a + b # ?
  34. Imaginary friends Did you know that python has first class

    support for complex numbers? a = complex(real=4, imag=1) b = complex(real=1, imag=2) a + b # complex(5, 3)
  35. Imaginary friends Did you know that python has first class

    support for complex numbers? a = complex(real=4, imag=1) b = complex(real=1, imag=2) a + b # complex(5, 3) a + b + 1 # ?
  36. Imaginary friends Did you know that python has first class

    support for complex numbers? a = complex(real=4, imag=1) b = complex(real=1, imag=2) a + b # complex(5, 3) a + b + 1 # complex(6, 3)
  37. The Twist It also has imaginary number litterals: 1 +

    2j # complex(1, 2) 3j # complex(0, 3)
  38. The Twist It also has imaginary number litterals: 1 +

    2j # complex(1, 2) 3j # complex(0, 3) 1j ** 2 # ?
  39. The Twist It also has imaginary number litterals: 1 +

    2j # complex(1, 2) 3j # complex(0, 3) 1j ** 2 # -1
  40. Part 3: Exotic Stdlib Modules Python ships with more than

    300 modules in its standard library. Here's a quick tour of some of its less known corners.
  41. python -m FOO Basically the same as doing python FOO.py

    where FOO is on the python path. Executes the content of the module.
  42. Utilities Some standard modules are written in a way that

    they can be used as standalone programs (not just imported). Uses the if __name__ == '__main__' trick.
  43. Share a directory over the network? python2 -m SimpleHTTPServer python3

    -m http.server Browse and dowload files from the current directory (on port 8000).
  44. List of functions in a file? python -m pyclbr your_file.py

    python -m pyclbr modulename Safe to run on arbitrary files.
  45. Leap Years def is_leap(year): return (year % 4) == 0

    \ and (year % 100) != 0 # Aah, that's better
  46. Leap Years def is_leap(year): return (year % 4) == 0

    \ and (year % 100) != 0 # ... or is it?
  47. Temporary Files from random import choice as ch S =

    'abcdefghjklmnopqrstuvwxyz' name = ''.join(ch(S) for _ in range(8)) temp = open('/tmp/%s' % name, 'w')
  48. Passing files to a program import sys for fname in

    sys.argv[1:]: f = open(fname) for line in f: process(line)
  49. import unicodedata from unicodedata import name, lookup name(u'á') # LATIN

    SMALL LETTER A WITH ACUTE lookup('LATIN SMALL LETTER A WITH ACUTE') # á