<s> from twisted . internet import reactor , error <EOL> from twisted . internet . defer import Deferred <EOL> class CallLaterOnce ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , func , * a , ** kw ) : <EOL> self . _func = func <EOL> self . _a = a <EOL> self . _kw = kw <EOL> self . _call = None <EOL> self . _errfunc = None <EOL> self . _erra = None <EOL> self . _errkw = None <EOL> def setErrback ( self , func , * a , ** kw ) : <EOL> self . _errfunc = func <EOL> self . _erra = a <EOL> self . _errkw = kw <EOL> def schedule ( self , delay = <NUM_LIT:0.0> ) : <EOL> if self . _call is None : <EOL> d = Deferred ( ) <EOL> d . addCallback ( self ) <EOL> if self . _errfunc : <EOL> d . addErrback ( self . error ) <EOL> self . _call = reactor . callLater ( delay , d . callback , None ) <EOL> def cancel ( self ) : <EOL> if self . _call : <EOL> self . _call . cancel ( ) <EOL> def __call__ ( self , * args , ** kwargs ) : <EOL> self . _call = None <EOL> return self . _func ( * self . _a , ** self . _kw ) <EOL> def error ( self , f ) : <EOL> self . _call = None <EOL> if self . _errfunc : <EOL> return self . _errfunc ( f , * self . _erra , ** self . _errkw ) <EOL> return f <EOL> def listen_tcp ( portrange , host , factory ) : <EOL> """<STR_LIT>""" <EOL> assert len ( portrange ) <= <NUM_LIT:2> , "<STR_LIT>" % portrange <EOL> if not hasattr ( portrange , '<STR_LIT>' ) : <EOL> return reactor . listenTCP ( portrange , factory , interface = host ) <EOL> if not portrange : <EOL> return reactor . listenTCP ( <NUM_LIT:0> , factory , interface = host ) <EOL> if len ( portrange ) == <NUM_LIT:1> : <EOL> return reactor . listenTCP ( portrange [ <NUM_LIT:0> ] , factory , interface = host ) <EOL> for x in range ( portrange [ <NUM_LIT:0> ] , portrange [ <NUM_LIT:1> ] + <NUM_LIT:1> ) : <EOL> try : <EOL> return reactor . listenTCP ( x , factory , interface = host ) <EOL> except error . CannotListenError : <EOL> if x == portrange [ <NUM_LIT:1> ] : <EOL> raise <EOL> def chunks ( l , n ) : <EOL> for i in xrange ( <NUM_LIT:0> , len ( l ) , n ) : <EOL> yield l [ i : i + n ] </s>
