<s> """<STR_LIT>""" <EOL> from ldaptor . protocols import pureldap <EOL> class freeNumberGuesser : <EOL> def __init__ ( self , makeAGuess , min = None , max = None ) : <EOL> self . makeAGuess = makeAGuess <EOL> self . min = min <EOL> if self . min is None : <EOL> self . min = <NUM_LIT:0> <EOL> self . max = max <EOL> def startGuessing ( self ) : <EOL> d = self . makeAGuess ( self . min ) <EOL> d . addCallback ( self . _nextGuess , self . min ) <EOL> return d <EOL> def _nextGuess ( self , found , lastGuess ) : <EOL> if found : <EOL> self . min = lastGuess <EOL> else : <EOL> self . max = lastGuess <EOL> if self . max == self . min or self . max == self . min + <NUM_LIT:1> : <EOL> return self . max <EOL> max = self . max <EOL> if max is None : <EOL> max = self . min + <NUM_LIT:1000> <EOL> guess = ( max + self . min ) / <NUM_LIT:2> <EOL> d = self . makeAGuess ( guess ) <EOL> d . addCallback ( self . _nextGuess , guess ) <EOL> return d <EOL> class ldapGuesser : <EOL> def __init__ ( self , ldapObject , numberType ) : <EOL> self . numberType = numberType <EOL> self . ldapObject = ldapObject <EOL> def guess ( self , num ) : <EOL> d = self . ldapObject . search ( <EOL> filterObject = pureldap . LDAPFilter_equalityMatch ( <EOL> attributeDesc = pureldap . LDAPAttributeDescription ( value = self . numberType ) , <EOL> assertionValue = pureldap . LDAPAssertionValue ( value = str ( num ) ) ) , <EOL> sizeLimit = <NUM_LIT:1> ) <EOL> d . addCallback ( lambda results : len ( results ) ) <EOL> return d <EOL> def getFreeNumber ( ldapObject , numberType , min = None , max = None ) : <EOL> g = freeNumberGuesser ( ldapGuesser ( ldapObject , numberType ) . guess , <EOL> min = min , max = max ) <EOL> return g . startGuessing ( ) </s>
