當前位置: 首頁>>代碼示例>>Python>>正文


Python phonenumbers.format_number方法代碼示例

本文整理匯總了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 
開發者ID:Flask-Middleware,項目名稱:flask-security,代碼行數:20,代碼來源:phone_util.py

示例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 
開發者ID:rapidpro,項目名稱:casepro,代碼行數:22,代碼來源:models.py

示例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 
開發者ID:rapidpro,項目名稱:casepro,代碼行數:18,代碼來源:models.py

示例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 
開發者ID:hjlarry,項目名稱:flask-shop,代碼行數:27,代碼來源:utils.py

示例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 
開發者ID:treveradams,項目名稱:openerp-freeswitch-connector,代碼行數:22,代碼來源:report_sxw_format.py

示例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 
開發者ID:treveradams,項目名稱:openerp-freeswitch-connector,代碼行數:19,代碼來源:number_not_found.py

示例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 
開發者ID:erigones,項目名稱:esdc-ce,代碼行數:19,代碼來源:widgets.py

示例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 
開發者ID:jhpyle,項目名稱:docassemble,代碼行數:22,代碼來源:functions.py

示例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 '' 
開發者ID:jhpyle,項目名稱:docassemble,代碼行數:20,代碼來源:functions.py

示例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 
開發者ID:jhpyle,項目名稱:docassemble,代碼行數:19,代碼來源:functions.py

示例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 
開發者ID:CroceRossaItaliana,項目名稱:jorvik,代碼行數:21,代碼來源:models.py

示例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) 
開發者ID:fpsw,項目名稱:Servo,代碼行數:6,代碼來源:customer.py

示例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) 
開發者ID:fpsw,項目名稱:Servo,代碼行數:6,代碼來源:customer.py

示例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) 
開發者ID:fpsw,項目名稱:Servo,代碼行數:6,代碼來源:customer.py

示例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 
開發者ID:TwilioDevEd,項目名稱:call-tracking-django,代碼行數:15,代碼來源:phone_number_filter.py


注:本文中的phonenumbers.format_number方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。