<s> """<STR_LIT>""" <EOL> from geopy . geocoders . base import Geocoder , DEFAULT_TIMEOUT , DEFAULT_SCHEME <EOL> from geopy . compat import urlencode <EOL> from geopy . location import Location <EOL> from geopy . exc import ConfigurationError , GeocoderQuotaExceeded <EOL> from geopy . util import logger <EOL> __all__ = ( "<STR_LIT>" , ) <EOL> class LiveAddress ( Geocoder ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( <EOL> self , <EOL> auth_id , <EOL> auth_token , <EOL> candidates = <NUM_LIT:1> , <EOL> scheme = DEFAULT_SCHEME , <EOL> timeout = DEFAULT_TIMEOUT , <EOL> proxies = None , <EOL> user_agent = None , <EOL> ) : <EOL> """<STR_LIT>""" <EOL> super ( LiveAddress , self ) . __init__ ( <EOL> timeout = timeout , proxies = proxies , user_agent = user_agent <EOL> ) <EOL> if scheme == "<STR_LIT:http>" : <EOL> raise ConfigurationError ( "<STR_LIT>" ) <EOL> self . scheme = scheme <EOL> self . auth_id = auth_id <EOL> self . auth_token = auth_token <EOL> if candidates : <EOL> if not <NUM_LIT:1> <= candidates <= <NUM_LIT:10> : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> self . candidates = candidates <EOL> self . api = '<STR_LIT>' % self . scheme <EOL> def geocode ( self , query , exactly_one = True , timeout = None ) : <EOL> """<STR_LIT>""" <EOL> url = self . _compose_url ( query ) <EOL> logger . debug ( "<STR_LIT>" , self . __class__ . __name__ , url ) <EOL> return self . _parse_json ( self . _call_geocoder ( url , timeout = timeout ) , <EOL> exactly_one ) <EOL> def _geocoder_exception_handler ( self , error , message ) : <EOL> """<STR_LIT>""" <EOL> if "<STR_LIT>" in message . lower ( ) : <EOL> raise GeocoderQuotaExceeded ( message ) <EOL> def _compose_url ( self , location ) : <EOL> """<STR_LIT>""" <EOL> query = { <EOL> '<STR_LIT>' : self . auth_id , <EOL> '<STR_LIT>' : self . auth_token , <EOL> '<STR_LIT>' : location , <EOL> '<STR_LIT>' : self . candidates <EOL> } <EOL> return '<STR_LIT>' . format ( url = self . api , query = urlencode ( query ) ) <EOL> def _parse_json ( self , response , exactly_one = True ) : <EOL> """<STR_LIT>""" <EOL> if not len ( response ) : <EOL> return None <EOL> if exactly_one is True : <EOL> return self . _format_structured_address ( response [ <NUM_LIT:0> ] ) <EOL> else : <EOL> return [ self . _format_structured_address ( c ) for c in response ] <EOL> @ staticmethod <EOL> def _format_structured_address ( address ) : <EOL> """<STR_LIT>""" <EOL> latitude = address [ '<STR_LIT>' ] . get ( '<STR_LIT>' ) <EOL> longitude = address [ '<STR_LIT>' ] . get ( '<STR_LIT>' ) <EOL> return Location ( <EOL> "<STR_LIT:U+002CU+0020>" . join ( ( address [ '<STR_LIT>' ] , address [ '<STR_LIT>' ] ) ) , <EOL> ( latitude , longitude ) if latitude and longitude else None , <EOL> address <EOL> ) </s>
