Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_null_field_returns_none(self):
model = NullablePhoneNumber()
self.assertEqual(model.phone_number, None)
model.phone_number = self.test_number_1
self.assertEqual(type(model.phone_number), PhoneNumber)
model.phone_number = phonenumberutil.parse(
self.test_number_1, keep_raw_input=True
)
self.assertEqual(type(model.phone_number), PhoneNumber)
def test_null_field_returns_none(self):
model = models.NullablePhoneNumber()
self.assertIsNone(model.phone_number)
model.phone_number = self.test_number_1
self.assertIsInstance(model.phone_number, PhoneNumber)
model.phone_number = phonenumberutil.parse(
self.test_number_1, keep_raw_input=True
)
self.assertIsInstance(model.phone_number, PhoneNumber)
def test_build_phone_number_dict(self):
numObj = phonenumberutil.parse("+1-979-549-5150")
numDict = self.pp.build_phone_number_dict(numObj, "Angleton, TX", "US")
self.assertEqual(numDict, self.testDict)
def email_to_phone(from_email):
'''Converts an email address like 14155551212@sms.example.com
into a phone number like +14155551212'''
(username, domain) = from_email.split('@')
potential_number = '+' + username
try:
ph_num = ph.parse(potential_number, 'US')
return ph.format_number(ph_num, ph.PhoneNumberFormat.E164)
except:
raise InvalidPhoneNumberInEmail(from_email)
test("Running local scan...")
FormattedPhoneNumber = "+" + formatNumber(InputNumber)
try:
PhoneNumberObject = phonenumbers.parse(FormattedPhoneNumber, None)
except Exception as e:
throw(e)
else:
if not phonenumbers.is_valid_number(PhoneNumberObject):
return False
number = phonenumbers.format_number(
PhoneNumberObject, phonenumbers.PhoneNumberFormat.E164
).replace("+", "")
numberCountryCode = phonenumbers.format_number(
PhoneNumberObject, phonenumbers.PhoneNumberFormat.INTERNATIONAL
).split(" ")[0]
numberCountry = phonenumbers.region_code_for_country_code(
int(numberCountryCode)
)
localNumber = phonenumbers.format_number(
PhoneNumberObject, phonenumbers.PhoneNumberFormat.E164
).replace(numberCountryCode, "")
internationalNumber = phonenumbers.format_number(
PhoneNumberObject, phonenumbers.PhoneNumberFormat.INTERNATIONAL
)
country = geocoder.country_name_for_number(PhoneNumberObject, "en")
location = geocoder.description_for_number(PhoneNumberObject, "en")
carrierName = carrier.name_for_number(PhoneNumberObject, "en")
prnt(__doc__, file=sys.stderr)
sys.exit(1)
elif opt in ("-v", "--var"):
varprefix = arg
elif opt in ("-f", "--flat"):
per_locale = False
elif opt in ("-s", "--sep"):
separator = arg
elif opt in ("-c", "--chunks"):
chunks = int(arg)
else:
prnt("Unknown option %s" % opt, file=sys.stderr)
prnt(__doc__, file=sys.stderr)
sys.exit(1)
if len(args) != 3:
prnt(__doc__, file=sys.stderr)
sys.exit(1)
if per_locale:
prefixdata = load_locale_prefixdata(args[0], separator=separator)
else:
prefixdata = {}
load_locale_prefixdata_file(prefixdata, args[0], separator=separator)
output_prefixdata_code(prefixdata, args[1], args[2], varprefix, per_locale, chunks)
if isinstance(pattern, list):
pat = len(pattern) > index and pattern[index] or ''
else:
lst = pattern.split('|')
pat = len(lst) > index and lst[index] or ''
if not pat:
return nb
nbl = []
for i, part in enumerate(nb.split()):
nbl.append(part)
nbl.append((pat[i:i + 1] + pat[-1:])[0])
return ''.join(nbl[:-1])
if format:
ret = format_with_pattern(phonenumbers.format_number(number, format == 'int' and 1 or 2))
elif country in phonenumbers.data._COUNTRY_CODE_TO_REGION_CODE.get(number.country_code, []):
ret = format_with_pattern(phonenumbers.format_number(number, phonenumbers.PhoneNumberFormat.NATIONAL))
else:
ret = format_with_pattern(phonenumbers.format_number(number, phonenumbers.PhoneNumberFormat.INTERNATIONAL))
return ret
# description we want to modify the data and give it another go
# with a country code added
if not check_region and not num_desc:
prefix = None
if len(self.digits) == 11 and data_string[0] == "1":
prefix = "+"
elif len(self.digits) == 10 \
and (data_string[0].isdigit() or
data_string[0] in string.punctuation):
prefix = "+1"
if prefix:
try:
# Second pass to see if we can get an actual
# geocode out of it using a hammer
second_pass = phonenumberutil.parse(prefix + data_string)
num_desc = geocoder.description_for_valid_number(
second_pass, "en"
).strip()
if num_desc:
num_obj = second_pass
# confidence hit because we had to
# modify the data to get a result
self.confidence -= 5
except (NumberParseException, Exception):
pass
# Attempting to get a valid region
num_region = phonenumberutil.region_code_for_number(num_obj)
# This is the compiled phone number data that
# we will use for the confidence decision
# description we want to modify the data and give it another go
# with a country code added
if not check_region and not num_desc:
prefix = None
if len(self.digits) == 11 and data_string[0] == "1":
prefix = "+"
elif len(self.digits) == 10 \
and (data_string[0].isdigit() or
data_string[0] in string.punctuation):
prefix = "+1"
if prefix:
try:
# Second pass to see if we can get an actual
# geocode out of it using a hammer
second_pass = phonenumberutil.parse(prefix + data_string)
num_desc = geocoder.description_for_valid_number(
second_pass, "en"
).strip()
if num_desc:
num_obj = second_pass
# confidence hit because we had to
# modify the data to get a result
self.confidence -= 5
except (NumberParseException, Exception):
pass
# Attempting to get a valid region
num_region = phonenumberutil.region_code_for_number(num_obj)
# This is the compiled phone number data that
# we will use for the confidence decision
PhoneNumberObject = phonenumbers.parse(FormattedPhoneNumber, None)
except Exception as e:
print(e)
else:
if not phonenumbers.is_valid_number(PhoneNumberObject):
return False
number = phonenumbers.format_number(PhoneNumberObject, phonenumbers.PhoneNumberFormat.E164).replace("+", "")
numberCountryCode = phonenumbers.format_number(PhoneNumberObject, phonenumbers.PhoneNumberFormat.INTERNATIONAL).split(" ")[0]
numberCountry = phonenumbers.region_code_for_country_code(int(numberCountryCode))
localNumber = phonenumbers.format_number(PhoneNumberObject, phonenumbers.PhoneNumberFormat.E164).replace(numberCountryCode, "")
internationalNumber = phonenumbers.format_number(PhoneNumberObject, phonenumbers.PhoneNumberFormat.INTERNATIONAL)
country = geocoder.country_name_for_number(PhoneNumberObject, "en")
location = geocoder.description_for_number(PhoneNumberObject, "en")
carrierName = carrier.name_for_number(PhoneNumberObject, "en")
if print_results:
print("International format: {}".format(internationalNumber))
print("Local format: {}".format(localNumber))
print("Country found: {} ({})".format(country, numberCountryCode))
print("City/Area: {}".format(location))
print("Carrier: {}".format(carrierName))
for timezoneResult in timezone.time_zones_for_number(PhoneNumberObject):
print("Timezone: {}".format(timezoneResult))
if phonenumbers.is_possible_number(PhoneNumberObject):
print("The number is valid and possible.")
else:
print("The number is valid but might not be possible.")