本文整理汇总了Python中phonenumbers.format_number方法的典型用法代码示例。如果您正苦于以下问题:Python phonenumbers.format_number方法的具体用法?Python phonenumbers.format_number怎么用?Python phonenumbers.format_number使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类phonenumbers
的用法示例。
在下文中一共展示了phonenumbers.format_number方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_canonical_form
# 需要导入模块: import phonenumbers [as 别名]
# 或者: from phonenumbers import format_number [as 别名]
def get_canonical_form(self, input_data):
""" Validate and return a canonical form to be stored in DB
and compared against.
Returns ``None`` if input isn't a valid phone number.
"""
import phonenumbers
try:
z = phonenumbers.parse(
input_data, region=config_value("PHONE_REGION_DEFAULT")
)
if phonenumbers.is_valid_number(z):
return phonenumbers.format_number(
z, phonenumbers.PhoneNumberFormat.E164
)
return None
except phonenumbers.phonenumberutil.NumberParseException:
return None
示例2: normalize_phone
# 需要导入模块: import phonenumbers [as 别名]
# 或者: from phonenumbers import format_number [as 别名]
def normalize_phone(cls, number):
"""
Normalizes the passed in phone number
"""
# remove any invalid characters
number = regex.sub(r"[^0-9a-z\+]", "", number.lower(), regex.V0)
# add on a plus if it looks like it could be a fully qualified number
if len(number) >= 11 and number[0] not in ["+", "0"]:
number = "+" + number
try:
normalized = phonenumbers.parse(number)
if phonenumbers.is_possible_number(normalized):
return phonenumbers.format_number(normalized, phonenumbers.PhoneNumberFormat.E164)
except Exception:
pass
return number
示例3: validate_phone
# 需要导入模块: import phonenumbers [as 别名]
# 或者: from phonenumbers import format_number [as 别名]
def validate_phone(cls, number):
"""
Validates the given phone number which should be in E164 format.
"""
try:
parsed = phonenumbers.parse(number)
except phonenumbers.NumberParseException as e:
raise InvalidURN(str(e))
if number != phonenumbers.format_number(parsed, phonenumbers.PhoneNumberFormat.E164):
raise InvalidURN("Phone numbers must be in E164 format")
if not phonenumbers.is_possible_number(parsed) or not phonenumbers.is_valid_number(parsed):
raise InvalidURN("Phone numbers must be in E164 format")
return True
示例4: __eq__
# 需要导入模块: import phonenumbers [as 别名]
# 或者: from phonenumbers import format_number [as 别名]
def __eq__(self, other):
"""
Override parent equality because we store only string representation
of phone number, so we must compare only this string representation
"""
if (
isinstance(other, PhoneNumber)
or isinstance(other, phonenumbers.PhoneNumber)
or isinstance(other, str)
):
format_string = "E164"
default_region = None
fmt = self.format_map[format_string]
if isinstance(other, str):
# convert string to phonenumbers.PhoneNumber
# instance
try:
other = phonenumbers.parse(other, region=default_region)
except phonenumbers.NumberParseException:
# Conversion is not possible, thus not equal
return False
other_string = phonenumbers.format_number(other, fmt)
return self.format_as(fmt) == other_string
else:
return False
示例5: format
# 需要导入模块: import phonenumbers [as 别名]
# 或者: from phonenumbers import format_number [as 别名]
def format(
self, text, oldtag=None, phone=False, phone_format='international'):
if self.pool.get('base.phone.installed') and phone and text:
# text should already be in E164 format, so we don't have
# to give a country code to phonenumbers.parse()
phone_number = phonenumbers.parse(text)
if phone_format == 'international':
res = phonenumbers.format_number(
phone_number, phonenumbers.PhoneNumberFormat.INTERNATIONAL)
elif phone_format == 'national':
res = phonenumbers.format_number(
phone_number, phonenumbers.PhoneNumberFormat.NATIONAL)
elif phone_format == 'e164':
res = phonenumbers.format_number(
phone_number, phonenumbers.PhoneNumberFormat.E164)
else:
res = text
else:
res = format_original(self, text, oldtag=oldtag)
return res
示例6: default_get
# 需要导入模块: import phonenumbers [as 别名]
# 或者: from phonenumbers import format_number [as 别名]
def default_get(self, cr, uid, fields_list, context=None):
res = super(number_not_found, self).default_get(
cr, uid, fields_list, context=context)
if not res:
res = {}
if res.get('calling_number'):
convert = self.pool['phone.common']._generic_reformat_phonenumbers(
cr, uid, {'phone': res.get('calling_number')}, context=context)
parsed_num = phonenumbers.parse(convert.get('phone'))
res['e164_number'] = phonenumbers.format_number(
parsed_num, phonenumbers.PhoneNumberFormat.INTERNATIONAL)
number_type = phonenumbers.number_type(parsed_num)
if number_type == 1:
res['number_type'] = 'mobile'
else:
res['number_type'] = 'phone'
return res
示例7: clean_international_phonenumber
# 需要导入模块: import phonenumbers [as 别名]
# 或者: from phonenumbers import format_number [as 别名]
def clean_international_phonenumber(value):
"""
Validate phone number taken from TelPrefixInput and return in format suitable for our DB.
"""
invalid_number_message = _(u'The phone number entered is not valid.')
try:
num = phonenumbers.parse(value)
if not phonenumbers.is_valid_number(num):
raise forms.ValidationError(invalid_number_message)
except phonenumbers.NumberParseException:
raise forms.ValidationError(invalid_number_message)
return phonenumbers.format_number(num, phonenumbers.PhoneNumberFormat.E164)
# noinspection PyAbstractClass
示例8: phone_number_in_e164
# 需要导入模块: import phonenumbers [as 别名]
# 或者: from phonenumbers import format_number [as 别名]
def phone_number_in_e164(number, country=None):
"""Given a phone number and a country code, returns the number in
E.164 format. Returns None if the number could not be so formatted."""
ensure_definition(number, country)
if country is None:
country = get_country()
use_whatsapp = False
if isinstance(number, str):
m = re.search(r'^whatsapp:(.*)', number)
if m:
number = m.group(1)
use_whatsapp = True
try:
pn = phonenumbers.parse(number, country)
output = phonenumbers.format_number(pn, phonenumbers.PhoneNumberFormat.E164)
except:
return None
if use_whatsapp:
return 'whatsapp:' + output
return output
示例9: phone_number_part
# 需要导入模块: import phonenumbers [as 别名]
# 或者: from phonenumbers import format_number [as 别名]
def phone_number_part(number, part, country=None):
ensure_definition(number, part, country)
if country is None:
country = get_country()
if isinstance(number, str):
m = re.search(r'^whatsapp:(.*)', number)
if m:
number = m.group(1)
try:
pn = phonenumbers.parse(number, country)
except:
return ''
formatted_number = phonenumbers.format_number(pn, phonenumbers.PhoneNumberFormat.NATIONAL)
parts = [x for x in re.split(r'[^0-9]+', formatted_number) if re.search(r'[0-9]', x)]
if part < len(parts):
return parts[part]
else:
return ''
示例10: phone_number_formatted
# 需要导入模块: import phonenumbers [as 别名]
# 或者: from phonenumbers import format_number [as 别名]
def phone_number_formatted(number, country=None):
"""Given a phone number and a country code, returns the number in
the standard format for the country. Returns None if the number
could not be so formatted."""
ensure_definition(number, country)
if country is None:
country = get_country()
if isinstance(number, str):
m = re.search(r'^whatsapp:(.*)', number)
if m:
number = m.group(1)
try:
pn = phonenumbers.parse(number, country)
output = phonenumbers.format_number(pn, phonenumbers.PhoneNumberFormat.NATIONAL)
except:
return None
return output
示例11: aggiungi_numero_telefono
# 需要导入模块: import phonenumbers [as 别名]
# 或者: from phonenumbers import format_number [as 别名]
def aggiungi_numero_telefono(self, numero, servizio=False, paese="IT"):
"""
Aggiunge un numero di telefono per la persona.
:param numero: Il numero di telefono.
:param servizio: Vero se il numero e' di servizio. Default False.
:param paese: Suggerimento per il paese. Default "IT".
:return: True se l'inserimento funziona, False se il numero e' mal formattato.
"""
try:
n = phonenumbers.parse(numero, paese)
except phonenumbers.phonenumberutil.NumberParseException:
return False
f = phonenumbers.format_number(n, phonenumbers.PhoneNumberFormat.E164)
t = Telefono(persona=self, numero=f, servizio=servizio)
try:
t.save()
except:
return False
return True
示例12: get_standard_phone
# 需要导入模块: import phonenumbers [as 别名]
# 或者: from phonenumbers import format_number [as 别名]
def get_standard_phone(self):
n = self.get_phone()
fmt = phonenumbers.PhoneNumberFormat.E164
return phonenumbers.format_number(n, fmt)
示例13: get_international_phone
# 需要导入模块: import phonenumbers [as 别名]
# 或者: from phonenumbers import format_number [as 别名]
def get_international_phone(self):
n = self.get_phone()
fmt = phonenumbers.PhoneNumberFormat.INTERNATIONAL
return phonenumbers.format_number(n, fmt)
示例14: get_national_phone
# 需要导入模块: import phonenumbers [as 别名]
# 或者: from phonenumbers import format_number [as 别名]
def get_national_phone(self):
n = self.get_phone()
fmt = phonenumbers.PhoneNumberFormat.NATIONAL
return phonenumbers.format_number(n, fmt)
示例15: national_format
# 需要导入模块: import phonenumbers [as 别名]
# 或者: from phonenumbers import format_number [as 别名]
def national_format(value):
"""Displays a phone number in an appropriate national format"""
# Since this is a template filter, we will just pass through
# the original value if we encounter any error
try:
number = phonenumbers.parse(value)
formatted_number = phonenumbers.format_number(
number, phonenumbers.PhoneNumberFormat.NATIONAL)
except Exception:
return value
return formatted_number