from typing import AnyStr
import codecs


def auto_encode(string: AnyStr, encoding: str = "utf-8", errors: str = "strict") -> bytes:
	"""Lookup a encoder and encode the string if it is bytes, else return it
	untouched if it's already in bytes (for utf). If its an int, etc, it'll try
	to wrap it in bytes for you.

	:param string: The text to encode
	:param encoding: The encoding-type to use; default is `utf-8`
	:param errors: optional; pass `replace` or `namereplace` if you don't want
									the default `strict` for how to process errors
	:return: The encoded text
	"""
	encoder = codecs.getencoder(encoding=encoding)
	if isinstance(string, bytes):
		return string
	elif isinstance(string, str):
		return encoder(string)[0]
	else:
		return encoder(str(string))[0]