1- """Python phone number parsing and formatting library"""
1+ """Python phone number parsing and formatting library
22
3+ If you use this library, and want to be notified about important changes,
4+ please sign up to the libphonenumber mailing list at
5+ http://groups.google.com/group/libphonenumber-discuss/about.
6+
7+ NOTE: A lot of methods in this module require Region Code strings. These must
8+ be provided using ISO 3166-1 two-letter country-code format. These should be
9+ in upper-case. The list of the codes can be found here:
10+ http://www.iso.org/iso/english_country_names_and_code_elements
11+
12+ author: Shaopeng Jia (original Java version)
13+ author: Lara Rennie (original Java Version)
14+ author: David Drysdale (Python version)
15+ """
316# Based on original Java code:
417# java/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java
518# Copyright (C) 2009-2011 Google Inc.
@@ -608,7 +621,7 @@ def _is_valid_region_code(region_code):
608621 """Helper function to check region code is not unknown or None"""
609622 if region_code is None :
610623 return False
611- return (region_code . upper () in SUPPORTED_REGIONS )
624+ return (region_code in SUPPORTED_REGIONS )
612625
613626
614627def format_number (numobj , num_format ):
@@ -630,18 +643,18 @@ def format_number(numobj, num_format):
630643
631644 Returns the formatted phone number.
632645 """
633- country_code = numobj .country_code
646+ country_calling_code = numobj .country_code
634647 nsn = national_significant_number (numobj )
635648 if num_format == PhoneNumberFormat .E164 :
636649 # Early exit for E164 case since no formatting of the national number needs to be applied.
637650 # Extensions are not formatted.
638- return _format_number_by_format (country_code , num_format , nsn )
651+ return _format_number_by_format (country_calling_code , num_format , nsn )
639652
640653 # Note region_code_for_country_code() is used because formatting
641654 # information for regions which share a country calling code is contained
642655 # by only one region for performance reasons. For example, for NANPA
643656 # regions it will be contained in the metadata for US.
644- region_code = region_code_for_country_code (country_code )
657+ region_code = region_code_for_country_code (country_calling_code )
645658 if not _is_valid_region_code (region_code ):
646659 return nsn
647660
@@ -650,7 +663,7 @@ def format_number(numobj, num_format):
650663 region_code ,
651664 num_format ,
652665 formatted_number )
653- return _format_number_by_format (country_code ,
666+ return _format_number_by_format (country_calling_code ,
654667 num_format ,
655668 formatted_number )
656669
@@ -815,8 +828,7 @@ def format_out_of_country_calling_number(numobj, region_calling_from):
815828
816829 Arguments:
817830 numobj -- The phone number to be formatted
818- region_calling_from -- The ISO 3166-1 two-letter region code that denotes
819- the region where the call is being placed
831+ region_calling_from -- The region where the call is being placed
820832
821833 Returns the formatted phone number
822834 """
@@ -1143,8 +1155,7 @@ def example_number(region_code):
11431155 """Gets a valid number for the specified region.
11441156
11451157 Arguments:
1146- region_code -- The ISO 3166-1 two-letter region code that denotes
1147- the region for which an example number is needed.
1158+ region_code -- The region for which an example number is needed.
11481159
11491160 Returns a valid fixed-line number for the specified region. Returns None
11501161 when the metadata does not contain such information.
@@ -1156,12 +1167,12 @@ def example_number_for_type(region_code, num_type):
11561167 """Gets a valid number for the specified region and number type.
11571168
11581169 Arguments:
1159- region_code -- The ISO 3166-1 two-letter region code that denotes
1160- the region for which an example number is needed.
1170+ region_code -- The region for which an example number is needed.
11611171 num_type -- The type of number that is needed.
11621172
11631173 Returns a valid number for the specified region and type. Returns None
1164- when the metadata does not contain such information.
1174+ when the metadata does not contain such information or if an invalid
1175+ region was specified.
11651176 """
11661177 # Check the region code is valid.
11671178 if not _is_valid_region_code (region_code ):
@@ -1316,8 +1327,7 @@ def is_valid_number_for_region(numobj, region_code):
13161327
13171328 Arguments:
13181329 numobj -- The phone number object that we want to validate.
1319- region_code -- The ISO 3166-1 two-letter region code that denotes the
1320- region that we want to validate the phone number for.
1330+ region_code -- The region that we want to validate the phone number for.
13211331
13221332 Returns a boolean that indicates whether the number is of a valid pattern.
13231333 """
@@ -1399,8 +1409,7 @@ def country_code_for_region(region_code):
13991409 Zealand.
14001410
14011411 Arguments:
1402- region_code -- The ISO 3166-1 two-letter region code that denotes
1403- the region that we want to get the country calling code for.
1412+ region_code -- The region that we want to get the country calling code for.
14041413
14051414 Returns the country calling code for the region denoted by region_code.
14061415 """
@@ -1424,8 +1433,7 @@ def ndd_prefix_for_region(region_code, strip_non_digits):
14241433 prefix when required.
14251434
14261435 Arguments:
1427- region_code -- The ISO 3166-1 two-letter region code that denotes
1428- the region that we want to get the dialling prefix for.
1436+ region_code -- The region that we want to get the dialling prefix for.
14291437 strip_non_digits -- whether to strip non-digits from the national
14301438 dialling prefix.
14311439
@@ -1451,7 +1459,7 @@ def is_nanpa_country(region_code):
14511459 Numbering Plan Administration (NANPA).
14521460 """
14531461 return (region_code is not None and
1454- region_code . upper () in _NANPA_REGIONS )
1462+ region_code in _NANPA_REGIONS )
14551463
14561464
14571465def _is_leading_zero_possible (country_code ):
@@ -1585,17 +1593,16 @@ def is_possible_number_string(number, region_dialing_from):
15851593
15861594 Arguments:
15871595 number -- The number that needs to be checked, in the form of a string.
1588- region_dialling_from -- The ISO 3166-1 two-letter region code that denotes
1589- the region that we are expecting the number to be dialed from.
1590- Note this is different from the region where the number belongs.
1591- For example, the number +1 650 253 0000 is a number that belongs
1592- to US. When written in this form, it can be dialed from any
1593- region. When it is written as 00 1 650 253 0000, it can be
1594- dialed from any region which uses an international dialling
1595- prefix of 00. When it is written as 650 253 0000, it can only be
1596- dialed from within the US, and when written as 253 0000, it can
1597- only be dialed from within a smaller area in the US (Mountain
1598- View, CA, to be more specific).
1596+ region_dialling_from -- The region that we are expecting the number to be
1597+ dialed from. Note this is different from the region where the
1598+ number belongs. For example, the number +1 650 253 0000 is a
1599+ number that belongs to US. When written in this form, it can be
1600+ dialed from any region. When it is written as 00 1 650 253 0000,
1601+ it can be dialed from any region which uses an international
1602+ dialling prefix of 00. When it is written as 650 253 0000, it
1603+ can only be dialed from within the US, and when written as 253
1604+ 0000, it can only be dialed from within a smaller area in the US
1605+ (Mountain View, CA, to be more specific).
15991606
16001607 Returns True if the number is possible
16011608 """
@@ -1942,8 +1949,7 @@ def parse(number, region, keep_raw_input=False,
19421949 number -- The number that we are attempting to parse. This can
19431950 contain formatting such as +, ( and -, as well as a phone
19441951 number extension.
1945- region -- The ISO 3166-1 two-letter region code that denotes the
1946- region that we are expecting the number to be from. This
1952+ region -- The region that we are expecting the number to be from. This
19471953 is only used if the number being parsed is not written in
19481954 international format. The country_code for the number in
19491955 this case would be stored as that of the default region
0 commit comments