def to_bytes(value, encoding='utf8'):
    """
    cast string to bytes() like object, but for python2 support it's bytearray copy
    """
    if isinstance(value, bytes):
        return value
    if isinstance(value, str):
        return value.encode(encoding)
    elif isinstance(value, bytearray):
        return bytes(value)
    else:
        raise TypeError('Not a string or bytes like object')
