generator slides review

A

andrea crotti

I'm giving a talk tomorrow @Fosdem about generators/iterators/iterables..

The slides are here (forgive the strange Chinese characters):
https://dl.dropboxusercontent.com/u/3183120/talks/generators/index.html#3

and the code I'm using is:
https://github.com/AndreaCrotti/generators/blob/master/code/generators.py
and the tests:
https://github.com/AndreaCrotti/generators/blob/master/code/test_generators.py

If anyone has any feedback or want to point out I'm saying something
stupid I'd love to hear it before tomorrow (or also later I might give
this talk again).
Thanks
 
M

Miki Tebeka

I'm giving a talk tomorrow @Fosdem about generators/iterators/iterables..



The slides are here (forgive the strange Chinese characters):

https://dl.dropboxusercontent.com/u/3183120/talks/generators/index.html#3



and the code I'm using is:

https://github.com/AndreaCrotti/generators/blob/master/code/generators.py

and the tests:

https://github.com/AndreaCrotti/generators/blob/master/code/test_generators.py



If anyone has any feedback or want to point out I'm saying something

stupid I'd love to hear it before tomorrow (or also later I might give

this talk again).

Thanks

My 2 cents:

slide 4:
[i*2 for i in range(10)]

slide 9:
while True:
try:
it = next(g)
body(it)
except StopIteration:
break

slide 21:
from itertools import count, ifilterfalse

def divided_by(p):
return lambda n: n % p == 0

def primes():
nums = count(2)
while True:
p = next(nums)
yield p
nums = ifilterfalse(divided_by(p), nums)



Another resource you can point to is http://www.dabeaz.com/generators/

Good luck.
 
A

andrea crotti

2014-02-01 Miki Tebeka said:
My 2 cents:

slide 4:
[i*2 for i in range(10)]

Well this is not correct in theory because the end should be the max
number, not the number of elements.
So it should be
[i*2 for i in range(10/2)] which might be fine but it's not really
more clear imho..
slide 9:
while True:
try:
it = next(g)
body(it)
except StopIteration:
break

Changed it thanks
slide 21:
from itertools import count, ifilterfalse

def divided_by(p):
return lambda n: n % p == 0

def primes():
nums = count(2)
while True:
p = next(nums)
yield p
nums = ifilterfalse(divided_by(p), nums)

Thank you that's nicer, but ifiilterfalse is not in Python 3 (could
use filter of course).
 
A

andrea crotti

The slides are updated now

2014-02-02 andrea crotti said:
2014-02-01 Miki Tebeka said:
My 2 cents:

slide 4:
[i*2 for i in range(10)]

Well this is not correct in theory because the end should be the max
number, not the number of elements.
So it should be
[i*2 for i in range(10/2)] which might be fine but it's not really
more clear imho..
slide 9:
while True:
try:
it = next(g)
body(it)
except StopIteration:
break

Changed it thanks
slide 21:
from itertools import count, ifilterfalse

def divided_by(p):
return lambda n: n % p == 0

def primes():
nums = count(2)
while True:
p = next(nums)
yield p
nums = ifilterfalse(divided_by(p), nums)

Thank you that's nicer, but ifiilterfalse is not in Python 3 (could
use filter of course).
 
A

andrea crotti

Sorry left too early, the slides are updated with the fixes suggested,
thanks everyone.
https://dl.dropboxusercontent.com/u/3183120/talks/generators/index.html#1

For me the biggest problem is still:
- to find some more interesting example that is easy enough to explain
- to find a better order in which explain things, to tell a clear story in a way

2014-02-02 andrea crotti said:
The slides are updated now

2014-02-02 andrea crotti said:
2014-02-01 Miki Tebeka said:
My 2 cents:

slide 4:
[i*2 for i in range(10)]

Well this is not correct in theory because the end should be the max
number, not the number of elements.
So it should be
[i*2 for i in range(10/2)] which might be fine but it's not really
more clear imho..
slide 9:
while True:
try:
it = next(g)
body(it)
except StopIteration:
break

Changed it thanks
slide 21:
from itertools import count, ifilterfalse

def divided_by(p):
return lambda n: n % p == 0

def primes():
nums = count(2)
while True:
p = next(nums)
yield p
nums = ifilterfalse(divided_by(p), nums)

Thank you that's nicer, but ifiilterfalse is not in Python 3 (could
use filter of course).
 
P

Peter Otten

andrea said:
Thank you that's nicer,

It may be nice, but is probably less efficient because of the lambda
function calls that replace the if expression in your
def exclude_multiples(n, ints):
for i in ints:
if (i % n) != 0:
yield i
but ifiilterfalse is not in Python 3 (could
use filter of course).

ifilterfalse() isn't gone in Python3, it just was renamed to filterfalse().
 
A

andrea crotti

Thanks everyone for your feedback.
The talk I think went well, maybe I was too fast because I only used 21 minutes.
From the audience feedback, there were some questions about my "Buggy
code" example, so yes probably it's not a good example since it's too
artificial.

I'll have to find something more useful about that or just skip this maybe.
For possible generators drawbacks though I could add maintanability,
if you start passing generators around in 3-4 nested levels finding
out what is the original source of can be difficult.

I'm also still not convinced by the definitions, which I tried now to
make clear and ay something like:
- and iterator defines *how you iterate* over an object (with the
__next__ method)
- an iterable defines *if you can iterate* over an object (with the
__iter__ method)

And when I do something like this:

class GenIterable:
def __init__(self, start=0):
self.even = start if is_even(start) else start + 1

def __iter__(self):
return self

def __next__(self):
tmp = self.even
self.even += 2
return tmp


it basically means that the a GenIterable object is iterable (because
of __iter__) and the way you iterate over it is to call the next
method on the object itself (since we return self and we define
__next__).

That seems clear enough, what do you think?
I might give this talk again so feedback is still appreciated!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top