Infinite Squares (imperative)
def sum_area(rects):
area = 0
for r in rects:
area += r.area
return area
Slide 25
Slide 25 text
Infinite Squares (imperative)
>>> from itertools import count
>>>
>>> squares = (
... Rectangle(x, x) for x in count(1))
>>> squares
at 0x11233ca40>
>>> next(squares)
<__main__.Rectangle at 0x1123a8400>
Slide 26
Slide 26 text
Infinite Squares (imperative)
>>> sum_area(squares)
KeyboardInterrupt
Traceback (most recent call last)
in ()
----> 1 sum_area(squares)
in
sum_area(rects)
3
4 for r in rects:
----> 5 area += r.area
Infinite Squares (functional)
def accumulate_area(rects):
accum = 0
for r in rects:
accum += get_area(r)
yield accum
Slide 34
Slide 34 text
Infinite Squares (functional)
>>> from itertools import islice
>>>
>>> squares = (
... make_rect(x, x) for x in count(1))
>>>
>>> area = accumulate_area(squares)
>>> next(islice(area, 6, 7))
140
>>> next(area)
204
Slide 35
Slide 35 text
Infinite Squares (functional)
>>> from itertools import accumulate
>>>
>>> squares = (
... make_rect(x, x) for x in count(1))
>>>
>>> area = accumulate(map(get_area, squares))
>>> next(islice(area, 6, 7))
140
>>> next(area)
204
Slide 36
Slide 36 text
Exercise #1 Image Credit: Me
Slide 37
Slide 37 text
Exercise #1 (Problem)
x
y z
Slide 38
Slide 38 text
Exercise #1 (Problem)
x ₒ factor
y h
Slide 39
Slide 39 text
Exercise #1 (Problem)
x
y z
x ₒ factor
y h
Slide 40
Slide 40 text
Exercise #1 (Problem)
(z ÷ h)
z
h
Slide 41
Slide 41 text
Exercise #1 (Problem)
x
y z
x ₒ factor
y h
ratio =
Slide 42
Slide 42 text
Exercise #1 (Problem)
z = √(x2 + y2 )
ratio = function1(x, y, factor)
hyp = function2(rectangle)
Slide 43
Slide 43 text
Exercise #1 (Problem)
z = √(x2 + y2 )
x
y z
x ₒ factor
y h
ratio = function1(x, y, factor)
hyp = function2(rectangle)
>>> get_ratio(1, 2, 2)
0.7905694150420948
Slide 44
Slide 44 text
Exercise #1 (Solution)
from math import sqrt, pow
def get_hyp(rect):
sum_s = sum(pow(r, 2) for r in rect)
return sqrt(sum_s)
def get_ratio(length, width, factor=1):
rect = make_rect(length, width)
big_rect = grow_rect(rect, factor)
return get_hyp(rect) / get_hyp(big_rect)
Exercise #2 (Problem)
• create a list of dicts with keys "factor", "length",
"width", and "ratio" (for factors 1 - 20)
• group the records by quartiles of the "ratio" value,
and aggregate each group by the median "ratio"
Slide 80
Slide 80 text
Exercise #2 (Problem)
from statistics import median
from meza.process import group
records[0]['ratio'] // .25
Slide 81
Slide 81 text
Exercise #2 (Problem)
• create a list of dicts with keys "factor", "length",
"width", and "ratio" (for factors 1 - 20)
• group the records by quartiles of the "ratio" value,
and aggregate each group by the median "ratio"
• write the records out to a csv file (1 row per group)
Slide 82
Slide 82 text
Exercise #2 (Problem)
from meza.convert import records2csv
from meza.io import write
key median
0 0.108…
1 0.343…
Slide 83
Slide 83 text
Exercise #2 (Solution)
>>> length = width = 2
>>> records = [
... {
... 'length': length,
... 'width': width,
... 'factor': f,
... 'ratio': get_ratio(length, width, f)
... }
...
... for f in range(1, 21)]
Slide 84
Slide 84 text
Exercise #2 (Solution)
>>> from statistics import median
>>> from meza import process as pr
>>>
>>> def aggregator(group):
... ratios = (g['ratio'] for g in group)
... return median(ratios)
>>>
>>> kwargs = {'aggregator': aggregator}
>>> gkeyfunc = lambda r: r['ratio'] // .25
>>> groups = pr.group(
... records, gkeyfunc, **kwargs)
Slide 85
Slide 85 text
Exercise #2 (Solution)
>>> from meza import convert as cv
>>> from meza.io import write
>>>
>>> results = [
... {'key': k, 'median': g}
... for k, g in groups]
>>>
>>> csv = cv.records2csv(results)
>>> write('results.csv', csv)
Exercise #3 (Problem)
• fetch the Python jobs rss feed
• tokenize the "summary" field by newlines ("\n")
• use "subelement" to extract the location (the first
"token")
• filter for jobs located in the U.S.
Slide 106
Slide 106 text
Exercise #3 (Problem)
from riko.collections import SyncPipe
url = 'https://www.python.org/jobs/feed/rss'
# use the 'fetch', 'tokenizer', 'subelement',
# and 'filter' pipes
Slide 107
Slide 107 text
Exercise #3 (Problem)
• write the 'link', 'location', and 'title' fields of each
record to a json file
Slide 108
Slide 108 text
Exercise #3 (Problem)
from meza.fntools import dfilter
from meza.convert import records2json
from meza.io import write