python interactive - threaded console (run -i)
castironpi at gmail.com
castironpi at gmail.com
Tue Mar 11 17:53:47 EDT 2008
> self._doneevents= {}
is extraneous.
> id, fun, ar, kw= self.get()
done, fun, ar, kw= self.get()
suffices.
> def _draintilclose( conn, understandfun= None ):
> if None is not understandfun:
> understandfun( _curmsg )
yield _curmsg
is an interesting synonym: the static interface. Compare signatures:
def _draingen( conn ):
...
yield _curmsg
def _draintilclose( sck, th ):
for x in _draingen( sck ):
th._print( x )
or in place of th._print, whatever function you passed to
understandfun.
The general case, pseudocode, consumer:
def _draintilclose( conn, visitee ):
for x in _draingen( conn ):
x.visit( visitee )
class Tool1Visitee:
def typeA( self, arg ):
tool1.listbox.add( arg )
def typeB( self, arg ):
tool1.combobox.add( arg )
class Tool2Visitee:
def typeA( self, arg ):
tool2.icons.add( arg )
def typeB( self, arg ):
tool2.listbox.add( arg )
and:
class VisitorX:
def visit( self, v ):
v.typeA( self._attr ):
class VisitorY:
def visit( self, v ):
v.typeB( self._attr ):
Producer:
def _draingen( conn ):
...
if:
_curvisitee= VisitorX( _curmsg )
else:
_curvisitee= VisitorY( _curmsg )
yield _curvisitee
and:
th1Visitee= Tool1Visitee()
new_thread( _draintilclose, conn1, th1Visitee )
th2Visitee= Tool2Visitee()
new_thread( _draintilclose, conn2, th2Visitee )
Tool1Visitor.typeA still just receives a byte array, but type
information comes with. By that point, the 'message knows' that it's
in a Tool1Visitor, and that it's a typeA message, and so adds it to
tool1.listbox. That puts just the right information in just the right
place... if it's the right time. Or pass the visitee directly to
_draingen as fit, without the visitor or yields, as a callback
collection.
def _draingen( conn, visitee ):
...
if:
visitee.typeA( _curmsg )
else:
visitee.typeB( _curmsg )
As a callback collection:
tool1_ft= dict( typeA= tool1.listbox.add, typeB=
tool1.combobox.add )
tool2_ft= dict( typeA= tool2.icons.add, typeB= too2.listbox.add )
@new_threads( conn1, tool1_ft )
@new_threads( conn2, tool2_ft )
def _( conn, tool_ft ):
for tp, msg in _draingen( conn, tool_ft ):
tool_ft[ tp ]( msg )
using a generator, or directly without:
tool1_ft= dict( typeA= tool1.listbox.add, typeB=
tool1.combobox.add )
new_thread( _draintilclose, conn1, tool1_ft )
tool2_ft= dict( typeA= tool2.icons.add, typeB= too2.listbox.add )
new_thread( _draintilclose, conn2, tool2_ft )
More information about the Python-list
mailing list