import string
def normalize_phone(phone):
if not phone:
return None
phone = ''.join(n for n in phone if n in set(string.digits + '+'))
phone_len = len(phone)
if phone_len == 12:
if phone.startswith('380'):
phone = '+' + phone
elif phone_len == 11:
if phone.startswith('8'):
phone = '+7' + phone.lstrip('8')
elif phone.startswith('7'):
phone = '+' + phone
elif phone_len == 10:
if phone.startswith('0'):
phone = '+38' + phone
elif phone.startswith('9'):
phone = '+7' + phone
if len(phone) < 12 or len(phone) > 13 or not phone.startswith('+'):
return None
return phone