Python Forum
example code for a coroutine generator
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
example code for a coroutine generator
#1
i was looking for a simple example of a python coroutine generator.  i did not find one.  so i wrote my own, calling it gener.py.  this is not a good example of what a coroutine generator is appropriate for.  i just picked doing a running summation because it would need very little code.  thus, it would be very minimal in obscuring the coroutine generator construct.

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, print_function, unicode_literals
"""Show what happens and doesn't happen with a simple coroutine generator.

file          gener.py
purpose       show what happens and doesn't happen with a
              simple coroutine generator that calculates a
              running total of ints.
note          the last total is not yielded to the caller.
              can you expain why not?
note          the use of False to indicate the end of the
              sequence is my choice.  None cannot be used
              for that; it is used to inidicate nothing is
              being sent in that send() call.
note          the interesting output is to stderr
              the boring output is to stdout
email         10054452614123394844460370234029112340408691

The intent is that this command works correctly under both Python 2 and
Python 3.  Please report failures or code improvement to the author.
"""

__license__ = """
Copyright (C) 2017, by Phil D. Howard - all other rights reserved

Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA, OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE, OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

The author may be contacted by decoding the number
10054452614123394844460370234029112340408691
(provu igi la numeron al duuma)
"""

import sys

def sum( init_total ):
    try:
        print('+ entering sum(',repr(init_total),')',file=sys.stderr)
        total = init_total
        print('+ total =',repr(total),'# initial value',file=sys.stderr)
        num = True
        while num != False:
            print('+ calling yield(',repr(total),')',file=sys.stderr)
            num = yield( total )
            print('+ yield(',repr(total),') returned (got sent) num =',repr(num),file=sys.stderr)
            if not isinstance( num, int ):
                continue
            ready = num + total
            print('+ adding num =',repr(num),'to total =',repr(total),'to get ready =',repr(ready),file=sys.stderr)
            total = ready
            print('+ total =',repr(total),file=sys.stderr)

    except:
        print('+ in sum() an exception happened',file=sys.stderr)

    print('+ return',file=sys.stderr)
    return

def main( args ):
    alist = args if len(args) > 1 else [ 'not used', 1, 2, 3, 4 ]

    try:
        init_sum = int( alist[1] )
        print('  calling sum(',repr(init_sum),') # nothing should happen in sum(), yet',file=sys.stderr)
        iter = sum( init_sum )
        print('  calling iter.send( None ) # now things will happen in sum()',file=sys.stderr)
        t = iter.send( None )
        print('  iter.send( None ) returned t =',repr(t),file=sys.stderr)
        for a in alist[2:]:
            n = int( a )
            print('  calling iter.send(',repr(n),')',file=sys.stderr)
            s = iter.send( n )
            print('  iter.send(',repr(n),') returned s =',repr(s),file=sys.stderr)
            print('total so far is',repr(s))
            sys.stdout.flush()
        print('  calling iter.send( False )',file=sys.stderr)
        s = iter.send( False )
        print('  iter.send( False ) returned s =',repr(s),file=sys.stderr)
        print('  calling iter.close()',file=sys.stderr)
        x = iter.close()
        print('  iter.close() returned x =',repr(x),file=sys.stderr)
    except StopIteration:
        print('  in main() StopIteration happened',file=sys.stderr)

    return

if __name__ == '__main__':
    try:
        result = main( sys.argv )
        sys.stdout.flush()
    except KeyboardInterrupt:
        result = 141
        print( '' )
    except IOError:
        result = 142
    try:
        exit( int( result ) )
    except ValueError:
        print( str( result ), file=sys.stderr )
        exit( 1 )
    except TypeError:
        if result == None:
            exit( 0 )
        exit( 255 )

# EOF
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
Does that actually use coroutines anywhere, though? It just looks like generators to me.
Reply
#3
(Feb-06-2017, 05:18 AM)nilamo Wrote: Does that actually use coroutines anywhere, though?  It just looks like generators to me.

numbers are sent to it, one at a time.  numbers are received from it, one at a time.  send it a None and it does nothing with it (yeah, not much different than send a 0 value).  but this is using the send method.  i personally don't like the asymmetry.  but i don't see an easy way to make it symmetrical in python.

i do wonder about how python figures it to be a coroutine and blocks the initial call.  i can envision code that can dynamically decide whether to do yield, or not, and accept sent data, or not.  there is also the issue of using a proxy generator (a function to do the yield() and/or send() call for you).  this stuff was complex enough in C.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#4
You may see asyncio.coroutine decorator's code. It's a coroutine generator as the documentation says. 
This liquefies my brain  Confused
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020