

def format_by_pattern(numobj, number_format, user_defined_formats):
    'Formats a phone number using client-defined formatting rules."\n\n    Note that if the phone number has a country calling code of zero or an\n    otherwise invalid country calling code, we cannot work out things like\n    whether there should be a national prefix applied, or how to format\n    extensions, so we return the national significant number with no\n    formatting applied.\n\n    Arguments:\n    numobj -- The phone number to be formatted\n    num_format -- The format the phone number should be formatted into\n    user_defined_formats -- formatting rules specified by clients\n\n    Returns the formatted phone number.\n    '
    country_code = numobj.country_code
    nsn = national_significant_number(numobj)
    if (not _has_valid_country_calling_code(country_code)):
        return nsn
    region_code = region_code_for_country_code(country_code)
    metadata = PhoneMetadata.metadata_for_region_or_calling_code(country_code, region_code)
    formatted_number = U_EMPTY_STRING
    formatting_pattern = _choose_formatting_pattern_for_number(user_defined_formats, nsn)
    if (formatting_pattern is None):
        formatted_number = nsn
    else:
        num_format_copy = _copy_number_format(formatting_pattern)
        np_formatting_rule = formatting_pattern.national_prefix_formatting_rule
        if ((np_formatting_rule is not None) and (len(np_formatting_rule) > 0)):
            national_prefix = metadata.national_prefix
            if ((national_prefix is not None) and (len(national_prefix) > 0)):
                np_formatting_rule = re.sub(_NP_PATTERN, national_prefix, np_formatting_rule, count=1)
                np_formatting_rule = re.sub(_FG_PATTERN, unicod('\\\\1'), np_formatting_rule, count=1)
                num_format_copy.national_prefix_formatting_rule = np_formatting_rule
            else:
                num_format_copy.national_prefix_formatting_rule = None
        formatted_number = _format_nsn_using_pattern(nsn, num_format_copy, number_format)
    formatted_number = _maybe_append_formatted_extension(numobj, user_defined_formats, number_format, formatted_number)
    formatted_number = _prefix_number_with_country_calling_code(country_code, number_format, formatted_number)
    return formatted_number

dataset/ETHPy150Open daviddrysdale/python-phonenumbers/python/phonenumbers/phonenumberutil.py format_by_pattern/VarMisuse@228/643 `metadata`->`user_defined_formats`