def format_by_pattern(numobj, number_format, user_defined_formats):
 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:
   national_prefix = metadata.national_prefix
   if national_prefix:
    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, metadata, number_format, formatted_number)
 formatted_number = _prefix_number_with_country_calling_code(country_code, number_format, formatted_number)
 return formatted_number

def format_by_pattern ( numobj , number_format , user_defined_formats ) : 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 : national_prefix = metadata . national_prefix if national_prefix : 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 , metadata , number_format , formatted_number ) formatted_number = _prefix_number_with_country_calling_code ( country_code , number_format , formatted_number ) return formatted_number
formats a phone number using client-defined formatting rules .