Python library for parsing, formatting, storing and validating international phone numbers with reduced memory footprint
npx @tessl/cli install tessl/pypi-phonenumberslite@9.0.0A memory-optimized Python library for parsing, formatting, storing and validating international phone numbers. This is the "lite" version of Google's libphonenumber library that excludes geocoder, carrier, and timezone metadata to reduce memory footprint while maintaining all core phone number processing functionality.
pip install phonenumbersliteimport phonenumbersImport specific functions and classes:
from phonenumbers import parse, format_number, PhoneNumber, PhoneNumberFormatImport validation functions:
from phonenumbers import is_valid_number, is_possible_numberimport phonenumbers
from phonenumbers import PhoneNumberFormat
# Parse a phone number
phone_number = phonenumbers.parse("+442083661177", None)
print(phone_number) # Country Code: 44 National Number: 2083661177
# Format in different ways
national = phonenumbers.format_number(phone_number, PhoneNumberFormat.NATIONAL)
print(national) # 020 8366 1177
international = phonenumbers.format_number(phone_number, PhoneNumberFormat.INTERNATIONAL)
print(international) # +44 20 8366 1177
e164 = phonenumbers.format_number(phone_number, PhoneNumberFormat.E164)
print(e164) # +442083661177
# Parse with region context
uk_number = phonenumbers.parse("020 8366 1177", "GB")
print(uk_number == phone_number) # True
# Validate numbers
is_valid = phonenumbers.is_valid_number(phone_number)
print(is_valid) # True
is_possible = phonenumbers.is_possible_number(phone_number)
print(is_possible) # Truephonenumberslite follows a clean architecture with well-separated concerns:
PhoneNumber and FrozenPhoneNumber classes represent parsed phone numbers with metadataparse() handle various input formats and regional contextsThe library is designed for high performance and accuracy, based on Google's authoritative libphonenumber data and algorithms.
Essential functions for parsing phone number strings into structured objects and formatting them for display or storage. Supports multiple input formats and international/national output formats.
def parse(number: str, region: str = None, keep_raw_input: bool = False,
numobj: PhoneNumber = None, _check_region: bool = True) -> PhoneNumber: ...
def format_number(numobj: PhoneNumber, num_format: PhoneNumberFormat) -> str: ...
def format_by_pattern(numobj: PhoneNumber, num_format: PhoneNumberFormat,
user_defined_formats: list) -> str: ...Comprehensive validation functions to check if phone numbers are valid, possible, or match specific criteria. Includes detailed validation reasons and type checking.
def is_valid_number(numobj: PhoneNumber) -> bool: ...
def is_possible_number(numobj: PhoneNumber) -> bool: ...
def is_valid_number_for_region(numobj: PhoneNumber, region_code: str) -> bool: ...Helper functions for normalizing, converting, and analyzing phone numbers. Includes string manipulation, character conversion, and number comparison utilities.
def normalize_digits_only(number: str) -> str: ...
def convert_alpha_characters_in_number(number: str) -> str: ...
def is_number_match(numobj1: PhoneNumber, numobj2: PhoneNumber) -> MatchType: ...Functions for working with country codes, region codes, and geographical information. Provides access to supported regions, calling codes, and number type information.
def country_code_for_region(region_code: str) -> int: ...
def region_code_for_number(numobj: PhoneNumber) -> str: ...
def supported_regions() -> set: ...Interactive phone number formatting for user interfaces, providing real-time formatting as users type phone numbers.
class AsYouTypeFormatter:
def __init__(self, region_code: str): ...
def input_digit(self, digit: str) -> str: ...
def clear(self) -> AsYouTypeFormatter: ...Advanced pattern matching to find and extract phone numbers from text, with configurable leniency levels and comprehensive match information.
class PhoneNumberMatcher:
def __init__(self, text: str, region: str, leniency: Leniency = None,
max_tries: int = 65536): ...
class PhoneNumberMatch:
def start(self) -> int: ...
def end(self) -> int: ...
def number(self) -> PhoneNumber: ...Support for emergency numbers, short codes, and special service numbers with cost information and regional validation.
def is_valid_short_number(numobj: PhoneNumber) -> bool: ...
def is_emergency_number(number: str, region_code: str) -> bool: ...
def expected_cost(numobj: PhoneNumber) -> ShortNumberCost: ...class PhoneNumber:
"""Represents a parsed phone number with all associated metadata."""
def __init__(self, country_code: int = None, national_number: int = None,
extension: str = None, italian_leading_zero: bool = None,
number_of_leading_zeros: int = None, raw_input: str = None,
country_code_source: CountryCodeSource = CountryCodeSource.UNSPECIFIED,
preferred_domestic_carrier_code: str = None): ...
class FrozenPhoneNumber(PhoneNumber):
"""Immutable version of PhoneNumber."""
class CountryCodeSource:
"""Source from which a country code is derived."""
UNSPECIFIED = 0
FROM_NUMBER_WITH_PLUS_SIGN = 1
FROM_NUMBER_WITH_IDD = 5
FROM_NUMBER_WITHOUT_PLUS_SIGN = 10
FROM_DEFAULT_COUNTRY = 20
class PhoneNumberFormat:
"""Phone number formatting options."""
E164 = 0 # +442083661177
INTERNATIONAL = 1 # +44 20 8366 1177
NATIONAL = 2 # 020 8366 1177
RFC3966 = 3 # tel:+44-20-8366-1177
class PhoneNumberType:
"""Types of phone numbers."""
FIXED_LINE = 0
MOBILE = 1
FIXED_LINE_OR_MOBILE = 2
TOLL_FREE = 3
PREMIUM_RATE = 4
SHARED_COST = 5
VOIP = 6
PERSONAL_NUMBER = 7
PAGER = 8
UAN = 9
VOICEMAIL = 10
UNKNOWN = 99
class ValidationResult:
"""Validation outcomes for phone number possibility checks."""
IS_POSSIBLE = 0
IS_POSSIBLE_LOCAL_ONLY = 4
INVALID_COUNTRY_CODE = 1
TOO_SHORT = 2
INVALID_LENGTH = 5
TOO_LONG = 3
class MatchType:
"""Phone number match quality levels."""
NOT_A_NUMBER = 0
NO_MATCH = 1
SHORT_NSN_MATCH = 2
NSN_MATCH = 3
EXACT_MATCH = 4class NumberFormat:
"""Number formatting pattern definition for regional phone number formatting."""
def __init__(self, pattern: str = None, format: str = None,
leading_digits_pattern: list = None, national_prefix_formatting_rule: str = None,
national_prefix_optional_when_formatting: bool = False,
domestic_carrier_code_formatting_rule: str = None): ...
class PhoneNumberDesc:
"""Phone number description with validation patterns and example numbers."""
def __init__(self, national_number_pattern: str = None, possible_number_pattern: str = None,
example_number: str = None): ...
class PhoneMetadata:
"""Complete metadata for phone numbers in a geographic region."""
def __init__(self, id: str = None, country_code: int = None, international_prefix: str = None,
national_prefix: str = None, preferred_extn_prefix: str = None,
national_prefix_for_parsing: str = None): ...class NumberParseException(Exception):
"""Exception thrown when phone number parsing fails."""
# Error types
INVALID_COUNTRY_CODE = 0
NOT_A_NUMBER = 1
TOO_SHORT_NSN = 2
TOO_SHORT_AFTER_IDD = 3
TOO_LONG = 4The phonenumberslite package excludes the following modules to reduce memory footprint:
phonenumbers.geocoder module for location informationphonenumbers.carrier module for carrier identificationphonenumbers.timezone module for timezone datageodata, carrierdata, tzdata directories are excludedAll core parsing, formatting, validation, and utility functions remain fully available.