Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > isgenerator(...) - anywhere to be found?

Reply
Thread Tools

isgenerator(...) - anywhere to be found?

 
 
Diez B. Roggisch
Guest
Posts: n/a
 
      01-22-2008
For a simple greenlet/tasklet/microthreading experiment I found myself in
the need to ask the question

isgenerator(v)

but didn't find any implementation in the usual suspects - builtins or
inspect.

I was able to help myself out with a simple (out of my head, hope its

def isgenerator(v):
def _g(): yield
return type(v) == type(_g())

But I wonder why there is no such method already available?

Diez
 
Reply With Quote
 
 
 
 
Stefan Rank
Guest
Posts: n/a
 
      01-22-2008
on 22.01.2008 14:20 Diez B. Roggisch said the following:
>
> def isgenerator(v):
> def _g(): yield
> return type(v) == type(_g())
>
> But I wonder why there is no such method already available?



This tests for generator objects, and you could also use::

return type(v) is types.GeneratorType

I think that this is pretty direct already.

I also need to test for generator functions from time to time for which
I use::

def _isaGeneratorFunction(func):
'''Check the bitmask of `func` for the magic generator flag.'''
return bool(func.func_code.co_flags & CO_GENERATOR)


cheers,
stefan

 
Reply With Quote
 
 
 
 
Diez B. Roggisch
Guest
Posts: n/a
 
      01-22-2008
Stefan Rank wrote:

> on 22.01.2008 14:20 Diez B. Roggisch said the following:
>>
>> def isgenerator(v):
>> def _g(): yield
>> return type(v) == type(_g())
>>
>> But I wonder why there is no such method already available?

>
>
> This tests for generator objects, and you could also use::
>
> return type(v) is types.GeneratorType
>
> I think that this is pretty direct already.


Not as nice as it could be, but certainly way less hackish than my approach.
Thanks!

> I also need to test for generator functions from time to time for which
> I use::
>
> def _isaGeneratorFunction(func):
> '''Check the bitmask of `func` for the magic generator flag.'''
> return bool(func.func_code.co_flags & CO_GENERATOR)


Not sure if that's not a bit too much on the dark magic side.. but good to
know that it exists.

Diez
 
Reply With Quote
 
Christian Heimes
Guest
Posts: n/a
 
      01-22-2008
Stefan Rank wrote:
> on 22.01.2008 14:20 Diez B. Roggisch said the following:
>> def isgenerator(v):
>> def _g(): yield
>> return type(v) == type(_g())
>>
>> But I wonder why there is no such method already available?

>
>
> This tests for generator objects, and you could also use::
>
> return type(v) is types.GeneratorType
>
> I think that this is pretty direct already.
>
> I also need to test for generator functions from time to time for which
> I use::
>
> def _isaGeneratorFunction(func):
> '''Check the bitmask of `func` for the magic generator flag.'''
> return bool(func.func_code.co_flags & CO_GENERATOR)


Can you please write a function for the inspect module + docs + a small
unit tests and submit a patch? The inspect module is missing the
isgenerator function.

Christian

 
Reply With Quote
 
Paul McGuire
Guest
Posts: n/a
 
      01-22-2008
On Jan 22, 7:46*am, Stefan Rank <list-e...@strank.info> wrote:
> I also need to test for generator functions from time to time for which
> I use::
>
> * *def _isaGeneratorFunction(func):
> * * * *'''Check the bitmask of `func` for the magic generator flag..'''
> * * * *return bool(func.func_code.co_flags & CO_GENERATOR)
>
> cheers,
> stefan


Might want to catch AttributeError in this routine - not all func
arguments will have a func_code attribute. See below:

class Z(object):
def __call__(*args):
for i in range(3):
yield 1

for i in Z()():
print i
# prints 1 three times

import types
print type(Z()()) == types.GeneratorType
# prints 'True'

print Z()().func_code
# raises AttributeError, doesn't have a func_code attribute

-- Paul
 
Reply With Quote
 
Stefan Rank
Guest
Posts: n/a
 
      01-22-2008
on 22.01.2008 16:09 Paul McGuire said the following:
> On Jan 22, 7:46 am, Stefan Rank <list-e...@strank.info> wrote:
>> I also need to test for generator functions from time to time for which
>> I use::
>>
>> def _isaGeneratorFunction(func):
>> '''Check the bitmask of `func` for the magic generator flag.'''
>> return bool(func.func_code.co_flags & CO_GENERATOR)
>>
>> cheers,
>> stefan

>
> Might want to catch AttributeError in this routine - not all func
> arguments will have a func_code attribute. See below:
>
> class Z(object):
> def __call__(*args):
> for i in range(3):
> yield 1
>
> for i in Z()():
> print i
> # prints 1 three times
>
> import types
> print type(Z()()) == types.GeneratorType
> # prints 'True'
>
> print Z()().func_code
> # raises AttributeError, doesn't have a func_code attribute


You are right about that for generator *objects*.
But _isaGeneratorFunction tests for generator *functions* (the ones you
call in order to get a generator object) and those must have a func_code.

So in your example::

>>> from compiler.consts import CO_GENERATOR
>>> Z().__call__.func_code.co_flags & CO_GENERATOR

32
>>> Z.__call__.func_code.co_flags & CO_GENERATOR

32

You have to use __call__ directly, you can't use the code-object-flag
test on the callable class instance Z(), but I think that's just as well
since this kind of test should not be necessary at all, except in rare
code parts (such as Diez' microthreading experiments).

cheers,
stefan

 
Reply With Quote
 
james.pye@gmail.com
Guest
Posts: n/a
 
      01-22-2008
On Jan 22, 6:20*am, "Diez B. Roggisch" <de...@nospam.web.de> wrote:
> For a simple greenlet/tasklet/microthreading experiment I found myself in
> the need to ask the question
>
> isgenerator(v)
>
> but didn't find any implementation in the usual suspects - builtins or
> inspect.


types.GeneratorType exists in newer Pythons, but I'd suggest just
checking for a send method.
That way, you can use something that emulates the interface without
being forced to use a generator.

hasattr(ob, 'send')..
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
USB config for Windows Connect Now. Did this go anywhere? glenn.woodruff@gmail.com Wireless Networking 2 09-01-2005 10:12 AM
PC Anywhere vs Wireless Paul King Wireless Networking 0 06-12-2005 11:29 PM
PC Anywhere setup Jim Wireless Networking 3 10-19-2004 02:46 PM
3620, rommon, and xmodeming an ios to it .. cannot find help for this anywhere. Volatileacid Cisco 1 07-30-2004 04:03 AM
PIX 501 and PC Anywhere the_poet Cisco 0 02-05-2004 10:38 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57