python tidbits
TRANSCRIPT
● Learn new constructs● Play with them in the interpreter
● Learn how to apply them to your own code
● Become a better Python programmer
Goals
Tuple packing
a, b = 'it will', 'consume you'a, b = b, a(a, b) = (b, a)(a, b) = ('consume you', 'it will')
With a predicatecubes = []for x in range(100): if x % 2: cubes.append(x ** 3)
cubes = map(lambda x: x ** 3,filter(lambda x: x % 2, range(100)))
cubes = [x ** 3 for x in range(100) if x % 2]
cubes = [x ** 3 for x in range(100)][1::2]
Sum of two dice
table = []for i in range(1, 7): row = [] for j in range(1, 7): row.append(i + j) table.append(row)
[[i + j for i in range(1, 7)]for j in range(1, 7)]
Named arguments
def increase(num, how_much=1, multiply=False): if multiply: return num * how_much return num + how_much
increase(1)increase(10, 10, False)increase(25, multiply=True, how_much=2)
Unpack dictionaries to arguments
options = { 'is_the_splab_door_open': True, 'is_it_hack_night_yet': False, 'students_enrolled_at_umich': 43651, 'fishythefish_status': 'savage'}
func(**options)
Variadic functions
def sum(*nums): result = 0 for num in nums: result += num return result
sum(1, 2, 3, 4)
Implementing range
class simple_range: def __init__(self, n): self.n = n self.data = [i for i in range(n)]
def __iter__(self): return iter(self.data)
Performanceimport time
class simple_range: def __init__(self, n): t0 = time.time() self.n = n self.data = [i for i in range(n)] print 'Time taken:', time.time() - t0
def __iter__(self): return iter(self.data)
simple_range(100000000)
# Time taken: 7.59687685966
Improved Performanceclass simple_range: def __init__(self, n): self.n = n self.i = 0
def __iter__(self): return self
def next(self): if self.i < self.n: result = self.i self.i += 1 return result else: raise StopIteration()
range, xrange, Python 3 range
simple_range(100000000)
# Time taken: 7.59687685966
# Time taken: 0.00002408027
A more complicated generatordef alphagen(start, end=None): s = list(reversed(start)) while end is None or s != list(reversed(end)): yield ''.join(reversed(s))
if s == list('z' * len(s)): s = list('a' * (len(s) + 1))
else: for i, ch in enumerate(s): if ch is 'z': s[i] = 'a' else: s[i] = chr(ord(ch) + 1) break
Solution
i = 0
for a in alphagen('bar', 'foo'): i += 1
print i - 1
# Time taken to create generator: .00000596046
Tracingdef fibonacci(n): t0 = time.time() print 'fib started'
if n is 0 or n is 1: print 'fib finished in', time.time() - t0, 'seconds returning', 1 return 1
else: x = fibonacci(n - 1) + fibonacci(n - 2) print 'fib finished in', time.time() - t0, 'seconds returning', x return x
Tracingdef trace(f): def f_prime(n): t0 = time.time() print f.__name__, 'started' value = f(n) print f.__name__, 'finished in', time.time() - t0, 'seconds returning', value return value return f_prime def fibonacci(n): if n is 0 or n is 1: return 1 else: return fibonacci(n - 1) + fibonacci(n - 2)
fibonacci = trace(fibonacci)
Syntactic sugar
@tracedef fibonacci(n): if n is 0 or n is 1: return 1 else: return fibonacci(n - 1) + fibonacci(n - 2)
Interested in more? Check out thesedispdb
flaskpydocpygame
itertoolsfunctoolsexec, eval
beautiful soupdict comprehensions