def normalize_serial_number(sn,<EOL>max_length=None, left_fill='<STR_LIT:0>', right_fill=str(), blank=str(),<EOL>valid_chars='<STR_LIT>',<EOL>invalid_chars=None,<EOL>strip_whitespace=True, join=False, na=rex.nones):
<EOL>if max_length is None:<EOL><INDENT>max_length = normalize_serial_number.max_length<EOL><DEDENT>else:<EOL><INDENT>normalize_serial_number.max_length = max_length<EOL><DEDENT>if left_fill is None:<EOL><INDENT>left_fill = normalize_serial_number.left_fill<EOL><DEDENT>else:<EOL><INDENT>normalize_serial_number.left_fill = left_fill<EOL><DEDENT>if right_fill is None:<EOL><INDENT>right_fill = normalize_serial_number.right_fill<EOL><DEDENT>else:<EOL><INDENT>normalize_serial_number.right_fill = right_fill<EOL><DEDENT>if blank is None:<EOL><INDENT>blank = normalize_serial_number.blank<EOL><DEDENT>else:<EOL><INDENT>normalize_serial_number.blank = blank<EOL><DEDENT>if valid_chars is None:<EOL><INDENT>valid_chars = normalize_serial_number.valid_chars<EOL><DEDENT>else:<EOL><INDENT>normalize_serial_number.valid_chars = valid_chars<EOL><DEDENT>if invalid_chars is None:<EOL><INDENT>invalid_chars = normalize_serial_number.invalid_chars<EOL><DEDENT>else:<EOL><INDENT>normalize_serial_number.invalid_chars = invalid_chars<EOL><DEDENT>if strip_whitespace is None:<EOL><INDENT>strip_whitespace = normalize_serial_number.strip_whitespace<EOL><DEDENT>else:<EOL><INDENT>normalize_serial_number.strip_whitespace = strip_whitespace<EOL><DEDENT>if join is None:<EOL><INDENT>join = normalize_serial_number.join<EOL><DEDENT>else:<EOL><INDENT>normalize_serial_number.join = join<EOL><DEDENT>if na is None:<EOL><INDENT>na = normalize_serial_number.na<EOL><DEDENT>else:<EOL><INDENT>normalize_serial_number.na = na<EOL><DEDENT>if invalid_chars is None:<EOL><INDENT>invalid_chars = (c for c in charlist.ascii_all if c not in valid_chars)<EOL><DEDENT>invalid_chars = '<STR_LIT>'.join(invalid_chars)<EOL>sn = str(sn).strip(invalid_chars)<EOL>if strip_whitespace:<EOL><INDENT>sn = sn.strip()<EOL><DEDENT>if invalid_chars:<EOL><INDENT>if join:<EOL><INDENT>sn = sn.translate(dict(zip(invalid_chars, ['<STR_LIT>'] * len(invalid_chars))))<EOL><DEDENT>else:<EOL><INDENT>sn = multisplit(sn, invalid_chars)[-<NUM_LIT:1>]<EOL><DEDENT><DEDENT>sn = sn[-max_length:]<EOL>if strip_whitespace:<EOL><INDENT>sn = sn.strip()<EOL><DEDENT>if na:<EOL><INDENT>if isinstance(na, (tuple, set, dict, list)) and sn in na:<EOL><INDENT>sn = '<STR_LIT>'<EOL><DEDENT>elif na.match(sn):<EOL><INDENT>sn = '<STR_LIT>'<EOL><DEDENT><DEDENT>if not sn and not (blank is False):<EOL><INDENT>return blank<EOL><DEDENT>if left_fill:<EOL><INDENT>sn = left_fill * int(max_length - len(sn) / len(left_fill)) + sn<EOL><DEDENT>if right_fill:<EOL><INDENT>sn = sn + right_fill * (max_length - len(sn) / len(right_fill))<EOL><DEDENT>return sn<EOL>
r"""Make a string compatible with typical serial number requirements

    # Default configuration strips internal and external whitespaces and retains only the last 10 characters

    >>> normalize_serial_number('1C 234567890             ')
    '0234567890'

    >>> normalize_serial_number('1C 234567890             ', max_length=20)
    '000000001C 234567890'
    >>> normalize_serial_number('Unknown', blank=None, left_fill=str())
    ''
    >>> normalize_serial_number('N/A', blank='', left_fill=str())
    'A'

    >>> normalize_serial_number('1C 234567890             ', max_length=20, left_fill='')
    '1C 234567890'

    Notice how the max_length setting (20) carries over from the previous test!
    >>> len(normalize_serial_number('Unknown', blank=False))
    20
    >>> normalize_serial_number('Unknown', blank=False)
    '00000000000000000000'
    >>> normalize_serial_number(' \t1C\t-\t234567890 \x00\x7f', max_length=14, left_fill='0',
    ...                         valid_chars='0123456789ABC', invalid_chars=None, join=True)
    '1C\t-\t234567890'

    Notice how the max_length setting carries over from the previous test!
    >>> len(normalize_serial_number('Unknown', blank=False))
    14

    Restore the default max_length setting
    >>> len(normalize_serial_number('Unknown', blank=False, max_length=10))
    10
    >>> normalize_serial_number('NO SERIAL', blank='--=--', left_fill='')  # doctest: +NORMALIZE_WHITESPACE
    'NO SERIAL'
    >>> normalize_serial_number('NO SERIAL', blank='', left_fill='')  # doctest: +NORMALIZE_WHITESPACE
    'NO SERIAL'

    >>> normalize_serial_number('1C 234567890             ', valid_chars='0123456789')
    '0234567890'