Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #65216 > unrolled thread

generator slides review

Started byandrea crotti <andrea.crotti.0@gmail.com>
First post2014-02-01 14:12 +0000
Last post2014-02-02 13:47 +0100
Articles 8 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  generator slides review andrea crotti <andrea.crotti.0@gmail.com> - 2014-02-01 14:12 +0000
    Re: generator slides review Miki Tebeka <miki.tebeka@gmail.com> - 2014-02-01 08:50 -0800
      Re: generator slides review andrea crotti <andrea.crotti.0@gmail.com> - 2014-02-02 10:48 +0000
        Re: generator slides review Miki Tebeka <miki.tebeka@gmail.com> - 2014-02-02 06:59 -0800
          Re: generator slides review andrea crotti <andrea.crotti.0@gmail.com> - 2014-02-02 22:55 +0000
      Re: generator slides review andrea crotti <andrea.crotti.0@gmail.com> - 2014-02-02 10:50 +0000
      Re: generator slides review andrea crotti <andrea.crotti.0@gmail.com> - 2014-02-02 10:52 +0000
      Re: generator slides review Peter Otten <__peter__@web.de> - 2014-02-02 13:47 +0100

#65216 — generator slides review

Fromandrea crotti <andrea.crotti.0@gmail.com>
Date2014-02-01 14:12 +0000
Subjectgenerator slides review
Message-ID<mailman.6278.1391263956.18130.python-list@python.org>
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

[toc] | [next] | [standalone]


#65228

FromMiki Tebeka <miki.tebeka@gmail.com>
Date2014-02-01 08:50 -0800
Message-ID<8118b17c-3352-4832-8567-089bd14c21ce@googlegroups.com>
In reply to#65216
On Saturday, February 1, 2014 6:12:28 AM UTC-8, andrea crotti wrote:
> 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.

[toc] | [prev] | [next] | [standalone]


#65257

Fromandrea crotti <andrea.crotti.0@gmail.com>
Date2014-02-02 10:48 +0000
Message-ID<mailman.6302.1391338128.18130.python-list@python.org>
In reply to#65228
2014-02-01 Miki Tebeka <miki.tebeka@gmail.com>:
>
> 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).

[toc] | [prev] | [next] | [standalone]


#65264

FromMiki Tebeka <miki.tebeka@gmail.com>
Date2014-02-02 06:59 -0800
Message-ID<ed96e4f0-5a72-48de-a00c-8f4a19ce0756@googlegroups.com>
In reply to#65257
> Thank you that's nicer, but ifiilterfalse is not in Python 3 (could
> 
> use filter of course).
It was renamed to filterfalse - http://docs.python.org/3.3/library/itertools.html#itertools.filterfalse

[toc] | [prev] | [next] | [standalone]


#65290

Fromandrea crotti <andrea.crotti.0@gmail.com>
Date2014-02-02 22:55 +0000
Message-ID<mailman.6311.1391381767.18130.python-list@python.org>
In reply to#65264
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!

[toc] | [prev] | [next] | [standalone]


#65258

Fromandrea crotti <andrea.crotti.0@gmail.com>
Date2014-02-02 10:50 +0000
Message-ID<mailman.6303.1391338252.18130.python-list@python.org>
In reply to#65228
The slides are updated now

2014-02-02 andrea crotti <andrea.crotti.0@gmail.com>:
> 2014-02-01 Miki Tebeka <miki.tebeka@gmail.com>:
>>
>> 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).

[toc] | [prev] | [next] | [standalone]


#65259

Fromandrea crotti <andrea.crotti.0@gmail.com>
Date2014-02-02 10:52 +0000
Message-ID<mailman.6304.1391338354.18130.python-list@python.org>
In reply to#65228
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 <andrea.crotti.0@gmail.com>:
> The slides are updated now
>
> 2014-02-02 andrea crotti <andrea.crotti.0@gmail.com>:
>> 2014-02-01 Miki Tebeka <miki.tebeka@gmail.com>:
>>>
>>> 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).

[toc] | [prev] | [next] | [standalone]


#65261

FromPeter Otten <__peter__@web.de>
Date2014-02-02 13:47 +0100
Message-ID<mailman.6305.1391345206.18130.python-list@python.org>
In reply to#65228
andrea crotti wrote:

> 2014-02-01 Miki Tebeka <miki.tebeka@gmail.com>:
>>
>> My 2 cents:

>> 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, 

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().

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web