<commit_before>from django.utils.encoding import force_str
from django_countries.data import COUNTRIES


class PhoneNumberParseException(Exception):
    pass


class PhoneNumber(object):

    def __init__(self, country_code, national_number):
        self.country_code = country_code
        self.national_number = national_number


def parse_phone_number(number, region=None, failhard=True):
    from phonenumbers.phonenumberutil import parse as phonenumbers_parse, NumberParseException
    try:
        phone = phonenumbers_parse(number, region)
    except NumberParseException:
        if failhard:
            raise PhoneNumberParseException()
        else:
            return None

    return PhoneNumber(phone.country_code, phone.national_number)


def strip_plus(number):
    return number[1:] if number.startswith('+') else number


def get_country_code_and_national_number(number, failhard=False):
    """
    :param number: str representing phone number
    :param failhard: if True, raise exception upon failure, otherwise return None
    :return: country_code: int, national_number: str
    """
    parsed = parse_phone_number(number, failhard=failhard)
    if parsed:
        country_code = parsed.country_code
        national_number = strip_plus(number)[len(str(country_code)):]
        return country_code, national_number
    return None, None


def country_code_for_region(region):
    """
    Simple wrapper around phonenumbers country_code_for_region method<commit_msg>take advantage of already parsed PhoneNumber<commit_after>from django.utils.encoding import force_str
from django_countries.data import COUNTRIES


class PhoneNumberParseException(Exception):
    pass


class PhoneNumber(object):

    def __init__(self, country_code, national_number):
        self.country_code = country_code
        self.national_number = national_number


def parse_phone_number(number, region=None, failhard=True):
    from phonenumbers.phonenumberutil import parse as phonenumbers_parse, NumberParseException
    try:
        phone = phonenumbers_parse(number, region)
    except NumberParseException:
        if failhard:
            raise PhoneNumberParseException()
        else:
            return None

    return PhoneNumber(phone.country_code, phone.national_number)


def strip_plus(number):
    return number[1:] if number.startswith('+') else number


def get_country_code_and_national_number(number):
    """
    :param number: str representing phone number
    :return: country_code: int, national_number: str
    """
    parsed = parse_phone_number(number, failhard=False)
    return (parsed.country_code, str(parsed.national_number)) if parsed else (None, None)


def country_code_for_region(region):
    """
    Simple wrapper around phonenumbers country_code_for_region method
{'fraction_non_alphanumeric': 0.07099536211202283, 'fraction_numerical': 0.0007135212272565109, 'mean_word_length': 4.182994454713493, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1352069', 'n_tokens_mistral': 789, 'n_tokens_neox': 757, 'n_words': 196}