[Python-Dev] thunks (for all the fish)

ManuelM.Garcia ManuelM.Garcia
Thu, 30 Jan 2003 22:08:05 -0800


On Thu, 30 Jan 2003 19:28:33 -0800, news@manuelmgarcia.com (Manuel
Garcia) wrote:

>OK, a 'thunk' is a generalized generator created from an indented
>suite.  A 'thunk' lets us react to control statements inside the thunk
>(or the lack of control statements).
>
>   iter0 =3D thunk.make_g_iter( [global] [, local] )
(edit)
>There are some subtleties of global and local I am sure I am missing.

Oh yeah, global and local will probably come from the thunk itself.
When the thunk is created, a suitable global and local is attached.
Usually the global would be used as is, perhaps the local would be
modified with some extra pairs before passing it to
thunk.make_g_iter().

>    key, record =3D recordset_loop('rs1'):
>        if no_more_keys: return None, None
>        if key in skip_keys: continue
>        print key
>        print record
>        if key =3D=3D good_key: return key, record

Would something like this be helpful?:

    max_length =3D 25
    skip_keys =3D ['1234', '2345']
    double_keys =3D ['9876', '8765']
    g =3D rs1.recordset_to_generator(BOF=3D1, EOF=3D1):
        if list_length >=3D max_length: break
        if BOF: return 'AAA', None
        if EOF: return 'ZZZ', None
        if key in skip_keys: continue
        if key in double_keys:
            yield '%s-00' % (key,), record
            return '%s-01' % (key,), record
        else:
            return key, record

You would make a generator in this strange way, with a combination of
returns and yields.  You could use break and continue for the loop,
and booleans could be put in the local to help sneak in extra values.
Again, any 'try: finally:' logic could be hidden inside.

One problem is that all these 'extra' behaviors would be hard to
document, unless you gave a long list of examples for people who might
use a 'recordset_to_generator()' you created.