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

D

Diez B. Roggisch

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
 
S

Stefan Rank

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
 
D

Diez B. Roggisch

Stefan said:
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
 
C

Christian Heimes

Stefan said:
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
 
P

Paul McGuire

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
 
S

Stefan Rank

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::
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
 
J

james.pye

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

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,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top