Coverage for /var/srv/projects/api.amasfac.comuna18.com/tmp/venv/lib/python3.9/site-packages/phonenumbers/__init__.py: 77%

11 statements  

« prev     ^ index     » next       coverage.py v6.4.4, created at 2023-07-17 14:22 -0600

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 

145 

146 

147# Version number is taken from the upstream libphonenumber version 

148# together with an indication of the version of the Python-specific code. 

149__version__ = "8.12.55" 

150 

151__all__ = ['PhoneNumber', 'CountryCodeSource', 'FrozenPhoneNumber', 

152 'REGION_CODE_FOR_NON_GEO_ENTITY', 'NumberFormat', 'PhoneNumberDesc', 'PhoneMetadata', 

153 'AsYouTypeFormatter', 

154 # items from phonenumberutil.py 

155 'COUNTRY_CODE_TO_REGION_CODE', 'SUPPORTED_REGIONS', 

156 'UNKNOWN_REGION', 'COUNTRY_CODES_FOR_NON_GEO_REGIONS', 

157 'NON_DIGITS_PATTERN', 

158 'MatchType', 'NumberParseException', 'PhoneNumberFormat', 

159 'PhoneNumberType', 'ValidationResult', 

160 'can_be_internationally_dialled', 

161 'convert_alpha_characters_in_number', 

162 'country_code_for_region', 

163 'country_code_for_valid_region', 

164 'country_mobile_token', 

165 'example_number', 

166 'example_number_for_type', 

167 'example_number_for_non_geo_entity', 

168 'format_by_pattern', 

169 'format_in_original_format', 

170 'format_national_number_with_carrier_code', 

171 'format_national_number_with_preferred_carrier_code', 

172 'format_number_for_mobile_dialing', 

173 'format_number', 

174 'format_out_of_country_calling_number', 

175 'format_out_of_country_keeping_alpha_chars', 

176 'invalid_example_number', 

177 'is_alpha_number', 

178 'is_nanpa_country', 

179 'is_number_geographical', 

180 'is_number_type_geographical', 

181 'is_number_match', 

182 'is_possible_number', 

183 'is_possible_number_for_type', 

184 'is_possible_number_for_type_with_reason', 

185 'is_possible_number_string', 

186 'is_possible_number_with_reason', 

187 'is_valid_number', 

188 'is_valid_number_for_region', 

189 'length_of_geographical_area_code', 

190 'length_of_national_destination_code', 

191 'national_significant_number', 

192 'ndd_prefix_for_region', 

193 'normalize_digits_only', 

194 'normalize_diallable_chars_only', 

195 'number_type', 

196 'parse', 

197 'region_code_for_country_code', 

198 'region_codes_for_country_code', 

199 'region_code_for_number', 

200 'supported_calling_codes', 

201 'supported_types_for_region', 

202 'supported_types_for_non_geo_entity', 

203 'truncate_too_long_number', 

204 'is_mobile_number_portable_region', 

205 # end of items from phonenumberutil.py 

206 # items from shortnumberinfo.py 

207 'SUPPORTED_SHORT_REGIONS', 

208 'ShortNumberCost', 

209 'is_possible_short_number_for_region', 

210 'is_possible_short_number', 

211 'is_valid_short_number_for_region', 

212 'is_valid_short_number', 

213 'expected_cost_for_region', 

214 'expected_cost', 

215 'connects_to_emergency_number', 

216 'is_emergency_number', 

217 'is_carrier_specific', 

218 'is_carrier_specific_for_region', 

219 'is_sms_service_for_region', 

220 # end of items from shortnumberinfo.py 

221 'PhoneNumberMatch', 'PhoneNumberMatcher', 'Leniency', 

222 ] 

223 

224if __name__ == '__main__': # pragma no cover 224 ↛ 225line 224 didn't jump to line 225, because the condition on line 224 was never true

225 import doctest 

226 doctest.testmod()