#NEWLINE# def normalize( vat_id ) : #NEWLINE# #INDENT# '\n    Accepts a VAT ID and normaizes it, getting rid of spaces, periods, dashes\n    etc and converting it to upper case.\n\n    :param vat_id:\n        The VAT ID to check. Allows "GR" prefix for Greece, even though it\n        should be "EL".\n\n    :raises:\n        ValueError - If the is not a string or is not in the format of two characters plus an identifier\n\n    :return:\n        None if the VAT ID is blank or not for an EU country or Norway\n        Otherwise a normalized string containing the VAT ID\n    ' #NEWLINE# if ( not vat_id ) : #NEWLINE# #INDENT# return None #NEWLINE# #UNINDENT# if ( not isinstance ( vat_id , str_cls ) ) : #NEWLINE# #INDENT# raise ValueError ( 'VAT ID is not a string' ) #NEWLINE# #UNINDENT# if ( len ( vat_id ) < 3 ) : #NEWLINE# #INDENT# raise ValueError ( 'VAT ID must be at least three character long' ) #NEWLINE# #UNINDENT# vat_id = re . sub ( '\\s+' , '' , vat_id ) #NEWLINE# vat_id = vat_id . replace ( '-' , '' ) #NEWLINE# vat_id = vat_id . replace ( '.' , '' ) #NEWLINE# vat_id = vat_id . upper ( ) #NEWLINE# country_prefix = vat_id [ 0 : 2 ] #NEWLINE# if ( country_prefix == 'GR' ) : #NEWLINE# #INDENT# vat_id = ( 'EL' + vat_id [ 2 : ] ) #NEWLINE# country_prefix = 'EL' #NEWLINE# #UNINDENT# if ( country_prefix not in ID_PATTERNS ) : #NEWLINE# #INDENT# return None #NEWLINE# #UNINDENT# return vat_id