[Twisted-Python] Re: feedback requested -- flow.py goes beta

Clark C. Evans cce at clarkevans.com
Fri Apr 18 11:02:31 EDT 2003


I've got a user interface question to ask... there are several user
interface improvements possible in flow's operation, here are the 
options:

currently:

  producer = flow.wrap([1,2,3])
  while 1:
      yield producer
      res = producer.getResult() 
      // handle success

  # or, if you wish to handle failures...

  producer = flow.wrap([1,2,3])
  while 1:
      yield producer
      if producer.stop: break
      if producer.isFailure:
          fail = producer.result
          // handle failure
      else:
          res = producer.result
          // handle success
      
We can reduce one line of code by making wrap return
an iterator... which always returns iself.   Thus, the
creation+while loop can be collapsed into a single 
for line.  Unfortunately, this isn't all that intutive
since the item being looped over isn't the result

   for producer in flow.wrap([1,2,3]):
        # same as above, use either form

Another option, which uses for construct,

   producer = flow.wrap([1,2,3])
   yield producer
   for result in producer:
       // handle success
       yield producer

   producer = flow.wrap([1,2,3], raiseFailures = 0)
   yield producer
   for result in producer:
       if producer.isFailure:
           // handle failure
       else:
           // handle success
       yield producer

I'm not sure if this is better... but it does
use the for loop correctly.   There is a variant
which could save one line of code in the yield...

   # done once at the top of the file
   import flow
   flow = flow.Flow()   # create a 'global' flow object


   # done elsewhere...

       yield flow.wrap([1,2,3])
       for result in flow:
           // handle success
           yield flow

This example keeps the 'producer' in a sort-of global
variable so it can be reused.   If you had more than
one producer, you'd have to fall-back on the previous
mechanism.  But for items with a single flow, this
perhaps could be useful.  I don't know, I haven't thought
it through very much.

Another advantage of this second form, is that producer
could sprout a 'peek' method to look at the next result...
but a problem of this method is that each wrapped producer
won't release the current value till the next one is
acquired (or the end of the iteration).   This could be
a problem.. I don't know.

Best,

Clark




More information about the Twisted-Python mailing list