1"""Python phone number parsing and formatting library
2
3Examples of use:
4
5>>> import phonenumbers
6>>> from phonenumbers.util import prnt # equivalent to Py3k print()
7>>> x = phonenumbers.parse("+442083661177", None)
8>>> prnt(x)
9Country Code: 44 National Number: 2083661177
10>>> type(x)
11<class 'phonenumbers.phonenumber.PhoneNumber'>
12>>> str(phonenumbers.format_number(x, phonenumbers.PhoneNumberFormat.NATIONAL))
13'020 8366 1177'
14>>> str(phonenumbers.format_number(x, phonenumbers.PhoneNumberFormat.INTERNATIONAL))
15'+44 20 8366 1177'
16>>> str(phonenumbers.format_number(x, phonenumbers.PhoneNumberFormat.E164))
17'+442083661177'
18>>> y = phonenumbers.parse("020 8366 1177", "GB")
19>>> prnt(y)
20Country Code: 44 National Number: 2083661177
21>>> x == y
22True
23>>>
24>>> formatter = phonenumbers.AsYouTypeFormatter("US")
25>>> prnt(formatter.input_digit("6"))
266
27>>> prnt(formatter.input_digit("5"))
2865
29>>> prnt(formatter.input_digit("0"))
30650
31>>> prnt(formatter.input_digit("2"))
32650-2
33>>> prnt(formatter.input_digit("5"))
34650-25
35>>> prnt(formatter.input_digit("3"))
36650-253
37>>> prnt(formatter.input_digit("2"))
38650-2532
39>>> prnt(formatter.input_digit("2"))
40(650) 253-22
41>>> prnt(formatter.input_digit("2"))
42(650) 253-222
43>>> prnt(formatter.input_digit("2"))
44(650) 253-2222
45>>>
46>>> text = "Call me at 510-748-8230 if it's before 9:30, or on 703-4800500 after 10am."
47>>> for match in phonenumbers.PhoneNumberMatcher(text, "US"):
48... prnt(match)
49...
50PhoneNumberMatch [11,23) 510-748-8230
51PhoneNumberMatch [51,62) 703-4800500
52>>> for match in phonenumbers.PhoneNumberMatcher(text, "US"):
53... prnt(phonenumbers.format_number(match.number, phonenumbers.PhoneNumberFormat.E164))
54...
55+15107488230
56+17034800500
57>>>
58"""
59
60# Licensed under the Apache License, Version 2.0 (the "License");
61# you may not use this file except in compliance with the License.
62# You may obtain a copy of the License at
63#
64# http://www.apache.org/licenses/LICENSE-2.0
65#
66# Unless required by applicable law or agreed to in writing, software
67# distributed under the License is distributed on an "AS IS" BASIS,
68# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
69# See the License for the specific language governing permissions and
70# limitations under the License.
71#
72# 'Some people, when confronted with a problem, think "I know,
73# I'll use regular expressions." Now they have two problems.'
74# -- jwz 1997-08-12
75
76# Data class definitions
77from .phonenumber import PhoneNumber, CountryCodeSource, FrozenPhoneNumber
78from .phonemetadata import REGION_CODE_FOR_NON_GEO_ENTITY, NumberFormat, PhoneNumberDesc, PhoneMetadata
79# Functionality
80from .asyoutypeformatter import AsYouTypeFormatter
81from .phonenumberutil import (COUNTRY_CODE_TO_REGION_CODE, SUPPORTED_REGIONS,
82 UNKNOWN_REGION, COUNTRY_CODES_FOR_NON_GEO_REGIONS,
83 NON_DIGITS_PATTERN,
84 MatchType, NumberParseException, PhoneNumberFormat,
85 PhoneNumberType, ValidationResult,
86 can_be_internationally_dialled,
87 convert_alpha_characters_in_number,
88 country_code_for_region,
89 country_code_for_valid_region,
90 country_mobile_token,
91 example_number,
92 example_number_for_type,
93 example_number_for_non_geo_entity,
94 format_by_pattern,
95 format_in_original_format,
96 format_national_number_with_carrier_code,
97 format_national_number_with_preferred_carrier_code,
98 format_number_for_mobile_dialing,
99 format_number,
100 format_out_of_country_calling_number,
101 format_out_of_country_keeping_alpha_chars,
102 invalid_example_number,
103 is_alpha_number,
104 is_nanpa_country,
105 is_number_match,
106 is_number_geographical,
107 is_number_type_geographical,
108 is_possible_number,
109 is_possible_number_for_type,
110 is_possible_number_for_type_with_reason,
111 is_possible_number_string,
112 is_possible_number_with_reason,
113 is_valid_number,
114 is_valid_number_for_region,
115 length_of_geographical_area_code,
116 length_of_national_destination_code,
117 national_significant_number,
118 ndd_prefix_for_region,
119 normalize_digits_only,
120 normalize_diallable_chars_only,
121 number_type,
122 parse,
123 region_code_for_country_code,
124 region_codes_for_country_code,
125 region_code_for_number,
126 supported_calling_codes,
127 supported_types_for_region,
128 supported_types_for_non_geo_entity,
129 truncate_too_long_number,
130 is_mobile_number_portable_region,)
131from .shortnumberinfo import (SUPPORTED_SHORT_REGIONS,
132 ShortNumberCost,
133 is_possible_short_number_for_region,
134 is_possible_short_number,
135 is_valid_short_number_for_region,
136 is_valid_short_number,
137 expected_cost_for_region,
138 expected_cost,
139 connects_to_emergency_number,
140 is_emergency_number,
141 is_carrier_specific,
142 is_carrier_specific_for_region,
143 is_sms_service_for_region)
144from .phonenumbermatcher import PhoneNumberMatch, PhoneNumberMatcher, Leniency
145from .re_util import enable_pattern_cache
146
147
148# Version number is taken from the upstream libphonenumber version
149# together with an indication of the version of the Python-specific code.
150__version__ = "9.0.43"
151
152__all__ = ['PhoneNumber', 'CountryCodeSource', 'FrozenPhoneNumber',
153 'REGION_CODE_FOR_NON_GEO_ENTITY', 'NumberFormat', 'PhoneNumberDesc', 'PhoneMetadata',
154 'AsYouTypeFormatter',
155 # items from phonenumberutil.py
156 'COUNTRY_CODE_TO_REGION_CODE', 'SUPPORTED_REGIONS',
157 'UNKNOWN_REGION', 'COUNTRY_CODES_FOR_NON_GEO_REGIONS',
158 'NON_DIGITS_PATTERN',
159 'MatchType', 'NumberParseException', 'PhoneNumberFormat',
160 'PhoneNumberType', 'ValidationResult',
161 'can_be_internationally_dialled',
162 'convert_alpha_characters_in_number',
163 'country_code_for_region',
164 'country_code_for_valid_region',
165 'country_mobile_token',
166 'example_number',
167 'example_number_for_type',
168 'example_number_for_non_geo_entity',
169 'format_by_pattern',
170 'format_in_original_format',
171 'format_national_number_with_carrier_code',
172 'format_national_number_with_preferred_carrier_code',
173 'format_number_for_mobile_dialing',
174 'format_number',
175 'format_out_of_country_calling_number',
176 'format_out_of_country_keeping_alpha_chars',
177 'invalid_example_number',
178 'is_alpha_number',
179 'is_nanpa_country',
180 'is_number_geographical',
181 'is_number_type_geographical',
182 'is_number_match',
183 'is_possible_number',
184 'is_possible_number_for_type',
185 'is_possible_number_for_type_with_reason',
186 'is_possible_number_string',
187 'is_possible_number_with_reason',
188 'is_valid_number',
189 'is_valid_number_for_region',
190 'length_of_geographical_area_code',
191 'length_of_national_destination_code',
192 'national_significant_number',
193 'ndd_prefix_for_region',
194 'normalize_digits_only',
195 'normalize_diallable_chars_only',
196 'number_type',
197 'parse',
198 'region_code_for_country_code',
199 'region_codes_for_country_code',
200 'region_code_for_number',
201 'supported_calling_codes',
202 'supported_types_for_region',
203 'supported_types_for_non_geo_entity',
204 'truncate_too_long_number',
205 'is_mobile_number_portable_region',
206 # end of items from phonenumberutil.py
207 # items from shortnumberinfo.py
208 'SUPPORTED_SHORT_REGIONS',
209 'ShortNumberCost',
210 'is_possible_short_number_for_region',
211 'is_possible_short_number',
212 'is_valid_short_number_for_region',
213 'is_valid_short_number',
214 'expected_cost_for_region',
215 'expected_cost',
216 'connects_to_emergency_number',
217 'is_emergency_number',
218 'is_carrier_specific',
219 'is_carrier_specific_for_region',
220 'is_sms_service_for_region',
221 # end of items from shortnumberinfo.py
222 'PhoneNumberMatch', 'PhoneNumberMatcher', 'Leniency',
223 'enable_pattern_cache',
224 ]
225
226if __name__ == '__main__': # pragma no cover
227 import doctest
228 doctest.testmod()