<s> from . disposable import Disposable <EOL> class CompositeDisposable ( Disposable ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * args ) : <EOL> if args and isinstance ( args [ <NUM_LIT:0> ] , list ) : <EOL> self . disposables = args [ <NUM_LIT:0> ] <EOL> else : <EOL> self . disposables = list ( args ) <EOL> super ( CompositeDisposable , self ) . __init__ ( ) <EOL> def add ( self , item ) : <EOL> """<STR_LIT>""" <EOL> should_dispose = False <EOL> with self . lock : <EOL> if self . is_disposed : <EOL> should_dispose = True <EOL> else : <EOL> self . disposables . append ( item ) <EOL> if should_dispose : <EOL> item . dispose ( ) <EOL> def remove ( self , item ) : <EOL> """<STR_LIT>""" <EOL> if self . is_disposed : <EOL> return <EOL> should_dispose = False <EOL> with self . lock : <EOL> if item in self . disposables : <EOL> self . disposables . remove ( item ) <EOL> should_dispose = True <EOL> if should_dispose : <EOL> item . dispose ( ) <EOL> return should_dispose <EOL> def dispose ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . is_disposed : <EOL> return <EOL> with self . lock : <EOL> self . is_disposed = True <EOL> current_disposables = self . disposables [ : ] <EOL> self . disposables = [ ] <EOL> for disposable in current_disposables : <EOL> disposable . dispose ( ) <EOL> def clear ( self ) : <EOL> """<STR_LIT>""" <EOL> with self . lock : <EOL> current_disposables = self . disposables [ : ] <EOL> self . disposables = [ ] <EOL> for disposable in current_disposables : <EOL> disposable . dispose ( ) <EOL> def contains ( self , item ) : <EOL> """<STR_LIT>""" <EOL> return item in self . disposables <EOL> def to_list ( self ) : <EOL> return self . disposables [ : ] <EOL> def __len__ ( self ) : <EOL> return len ( self . disposables ) <EOL> @ property <EOL> def length ( self ) : <EOL> return len ( self . disposables ) </s>
