def convert_csv_data_to_location(csv_data: dict) -> Optional[CenterLocation]: long = csv_data.get('long_coor1', None) lat = csv_data.get('lat_coor1', None) city = csv_data.get('com_nom', None) if not long or not lat: return None if 'address' in csv_data: city = departementUtils.get_city(csv_data.get('address')) try: return CenterLocation(float(long), float(lat), city) except: return None
def convert_csv_data_to_location(csv_data: dict) -> Optional[CenterLocation]: long = csv_data.get("long_coor1", None) lat = csv_data.get("lat_coor1", None) city = csv_data.get("com_nom", None) cp = csv_data.get("com_cp", None) if not long or not lat: return None if "address" in csv_data: if not city: city = departementUtils.get_city(csv_data.get("address")) if not cp: cp = departementUtils.get_cp(csv_data.get("address")) try: return CenterLocation(float(long), float(lat), city, cp) except: return None
def from_csv_data(cls, data: dict) -> Optional[CenterLocation]: long = data.get("long_coor1") lat = data.get("lat_coor1") city = data.get("com_nom") cp = data.get("com_cp") if long and lat: if address := data.get("address"): if not city: city = departementUtils.get_city(address) if not cp: cp = departementUtils.get_cp(address) try: return CenterLocation(long, lat, city, cp) except Exception as e: logger.warning( "Failed to parse CenterLocation from {}".format(data)) logger.warning(e)
def test_get_city(): address_1 = "2 avenue de la République, 75005 PARIS" address_2 = " 24 Rue de la Brèche, 91740 Pussay " address_3 = "Centre Cial du Bois des Roches 91240 SAINT MICHEL SUR ORGE" address_4 = " , 83700 Saint-Raphaël " address_5 = "1171 Avenue Gaston Feuillard\n97100 Basse-Terre" address_6 = "Rue de la République" assert departementUtils.get_city(address_1) == "PARIS" assert departementUtils.get_city(address_2) == "Pussay" assert departementUtils.get_city(address_3) == "SAINT MICHEL SUR ORGE" assert departementUtils.get_city(address_4) == "Saint-Raphaël" assert departementUtils.get_city(address_5) == "Basse-Terre" assert departementUtils.get_city(address_6) == None