Last active
December 11, 2015 22:28
-
-
Save TMcManus/4669579 to your computer and use it in GitHub Desktop.
Example of a function to determine the country code of a phone number.
Based on the python port of Google's libphonenumber project:
https://github.com/daviddrysdale/python-phonenumbers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Example of a function to determine the country code of a phone number. | |
Based on the python port of Google's libphonenumber project: | |
https://github.com/daviddrysdale/python-phonenumbers | |
""" | |
import phonenumbers as ph | |
def get_country_code(phone_number): | |
"""Returns the ISO 3166-1 alpha-2 code for a phone number | |
May also return None or ZZ for invalid regions | |
Arguments: | |
phone_number -- A phone number in any format as a string | |
>>> get_country_code("+18085551234") | |
'US' | |
>>> get_country_code("+12505551234") | |
'CA' | |
>>> get_country_code("(707) 555-1234") | |
'US' | |
>>> get_country_code("1 514-555-1234") | |
'CA' | |
>>> get_country_code("+81355555555") | |
'JP' | |
""" | |
number_object = ph.parse(phone_number, region="US") | |
return ph.region_code_for_number(number_object) | |
# To run the tests, uncomment this section and run | |
#if __name__ == "__main__": | |
# import doctest | |
# doctest.testmod() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment