def encode(value, encoding='utf-8'):
    """Encode given Unicode value to bytes with given encoding

    :param str value: the value to encode
    :param str encoding: selected encoding
    :return: bytes; value encoded to bytes if input is a string, original value otherwise

    >>> from pyams_utils.unicode import encode
    >>> encode('Chaîne accentuée')
    b'Cha\\xc3\\xaene accentu\\xc3\\xa9e'
    >>> encode('Chaîne accentuée', 'latin1')
    b'Cha\\xeene accentu\\xe9e'
    """
    return value.encode(encoding) if isinstance(value, str) else value