<s> from rx import Observable , AnonymousObservable <EOL> from rx . internal . exceptions import ArgumentOutOfRangeException <EOL> from rx . internal import extensionmethod <EOL> def _element_at_or_default ( source , index , has_default = False , <EOL> default_value = None ) : <EOL> if index < <NUM_LIT:0> : <EOL> raise ArgumentOutOfRangeException ( ) <EOL> def subscribe ( observer ) : <EOL> i = [ index ] <EOL> def on_next ( x ) : <EOL> found = False <EOL> with source . lock : <EOL> if i [ <NUM_LIT:0> ] : <EOL> i [ <NUM_LIT:0> ] -= <NUM_LIT:1> <EOL> else : <EOL> found = True <EOL> if found : <EOL> observer . on_next ( x ) <EOL> observer . on_completed ( ) <EOL> def on_completed ( ) : <EOL> if not has_default : <EOL> observer . on_error ( ArgumentOutOfRangeException ( ) ) <EOL> else : <EOL> observer . on_next ( default_value ) <EOL> observer . on_completed ( ) <EOL> return source . subscribe ( on_next , observer . on_error , on_completed ) <EOL> return AnonymousObservable ( subscribe ) <EOL> @ extensionmethod ( Observable ) <EOL> def element_at_or_default ( self , index , default_value = None ) : <EOL> """<STR_LIT>""" <EOL> return _element_at_or_default ( self , index , True , default_value ) </s>
