-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdadata_service.py
More file actions
26 lines (21 loc) · 1.01 KB
/
dadata_service.py
File metadata and controls
26 lines (21 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
from dadata import Dadata
from typing import Optional, Dict
from constants import DADATA_TOKEN, DADATA_SECRET
dadata = Dadata(DADATA_TOKEN, DADATA_SECRET)
def normalize_address_with_dadata(address: str) -> Optional[Dict]:
try:
return dadata.clean("address", address)
except Exception as e:
print(f"[Ошибка Dadata] Не удалось очистить адрес: {e}")
return None
def construct_standard_address(cleaned_data: Dict) -> Optional[str]:
try:
if not isinstance(cleaned_data, dict):
raise TypeError("cleaned_data должен быть словарём")
fields_order = ['street', 'house', 'city', 'region', 'country']
parts = [cleaned_data[field]
for field in fields_order if cleaned_data.get(field)]
return ' '.join(parts) if parts else None
except (KeyError, TypeError) as e:
print(f"[Ошибка сборки адреса] Неверная структура данных: {e}")
return None