byteify

privex.helpers.common.byteify(data: Optional[Union[str, bytes]], encoding='utf-8', if_none=None)bytes[source]

Convert a piece of data into bytes if it isn’t already:

>>> byteify("hello world")
b"hello world"

By default, if data is None, then a TypeError will be raised by bytes().

If you’d rather convert None into a blank bytes string, use if_node="", like so:

>>> byteify(None)
TypeError: encoding without a string argument
>>> byteify(None, if_none="")
b''