Skip to content

Instantly share code, notes, and snippets.

@Angeloem
Created January 22, 2022 08:48
Show Gist options
  • Save Angeloem/25fa605eb8e36550aeef7c5a7fc3e747 to your computer and use it in GitHub Desktop.
Save Angeloem/25fa605eb8e36550aeef7c5a7fc3e747 to your computer and use it in GitHub Desktop.
getting the phone number provider in Tanzania. Feel free to modify
_carriers = {
'Tigo Tanzania': ['071', '065', '067'],
'Airtel Tanzania': ['078', '068', '069'],
'Vodacom Tanzania': ['075', '076', '074'],
'TTCL': ['073'],
'Halotel': ['061', '062'],
'Zantel': ['077'],
}
def get_phone_carrier(phone: str) -> str:
# add the 255 to the front of the string
if phone.startswith('255') and not phone.startswith('0'):
phone = f'0{phone[3:]}'
#
elif phone.startswith('+255') and not phone.startswith('0'):
phone = f'0{phone[4:]}'
#
elif phone.startswith('7') or phone.startswith('6'):
phone = f'0{phone}'
# the first three values of the phone:: index 3 in nonincluded
first_phone = phone[0:3]
_selected = None
for _carrier in _carriers.keys():
# _carrier is a key
if first_phone in _carriers[_carrier]:
_selected = _carrier
break
return _selected if _selected else ''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment