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

Discussion in 'Python' started by Diez B. Roggisch, Jan 22, 2008.

  1. 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
     
    Diez B. Roggisch, Jan 22, 2008
    #1
    1. Advertising

  2. Diez B. Roggisch

    Stefan Rank Guest

    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
     
    Stefan Rank, Jan 22, 2008
    #2
    1. Advertising

  3. 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
     
    Diez B. Roggisch, Jan 22, 2008
    #3
  4. 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
     
    Christian Heimes, Jan 22, 2008
    #4
  5. Diez B. Roggisch

    Paul McGuire Guest

    On Jan 22, 7:46 am, Stefan Rank <> 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
     
    Paul McGuire, Jan 22, 2008
    #5
  6. Diez B. Roggisch

    Stefan Rank Guest

    on 22.01.2008 16:09 Paul McGuire said the following:
    > On Jan 22, 7:46 am, Stefan Rank <> 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
     
    Stefan Rank, Jan 22, 2008
    #6
  7. Diez B. Roggisch

    Guest

    On Jan 22, 6:20 am, "Diez B. Roggisch" <> 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')..
     
    , Jan 22, 2008
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jason Shohet

    can't find the javascript cookie anywhere

    Jason Shohet, May 11, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    558
    Jason Shohet
    May 11, 2004
  2. =?Utf-8?B?U3RldmVuIFc=?=

    Selecting Rows by Clicking Anywhere

    =?Utf-8?B?U3RldmVuIFc=?=, Feb 15, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    365
    =?Utf-8?B?U3RldmVuIFc=?=
    Feb 15, 2005
  3. Fernando Lopes
    Replies:
    0
    Views:
    3,202
    Fernando Lopes
    Apr 28, 2005
  4. Paul W
    Replies:
    4
    Views:
    4,686
    Daniel Walzenbach
    Jul 15, 2005
  5. Jean-Paul Calderone

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

    Jean-Paul Calderone, Jan 22, 2008, in forum: Python
    Replies:
    3
    Views:
    257
    TezZ Da Sp@cE MaN
    Jan 23, 2008
Loading...

Share This Page