def quote_copy(s):
    """Quoting for copy command.  None is converted to \\N.

    Python implementation.
    """
    if s is None:
        return '\\N'
    s = str(s)
    s = s.replace('\\', '\\\\')
    s = s.replace('\t', '\\t')
    s = s.replace('\n', '\\n')
    s = s.replace('\r', '\\r')
    return s
