def multiple_from_location_resources(resources, include_confidence=True): """ :param resources: :param include_confidence: :return: list of tuples of bing location and confidence from high = 2 to low = 0 or list of bing locations """ results = [] try: for loc in resources: bing_location = BingLocation.from_location_resource(loc) if not include_confidence: results.append(bing_location) else: confidence = loc["confidence"].lower() if confidence == "high": confidence = 2 elif confidence == "medium": confidence = 1 elif confidence == "low": confidence = 0 else: raise BingApiError("Unknown Confidence:\n{}".format(dict_to_pretty_str(loc))) results.append((bing_location, confidence)) # append the tuple except KeyError: raise BingApiError("The following location does not has the expected format:\n{}" .format(dict_to_pretty_str(loc))) return results
def from_location_resource(resource): try: point = resource["point"]["coordinates"] address = resource["address"]["formattedAddress"] except KeyError: raise BingApiError("The following location does not has the expected format:\n{}" .format(dict_to_pretty_str(resource))) return BingLocation(point=point, address=address)
def __str__(self): try: return dict_to_pretty_str(str(self.__dict__)) except Exception as e: return str(self.__dict__)