rbtools.utils.encoding¶
Utilities for managing string types and encoding.
Functions
Force a given string to be a byte string type. |
|
Force a given string to be a Unicode string type. |
- rbtools.utils.encoding.force_bytes(string: Union[bytes, str], encoding: str = 'utf-8', *, strings_only: Literal[True] = True) bytes [source]¶
- rbtools.utils.encoding.force_bytes(string: Any, encoding: str = 'utf-8', *, strings_only: Literal[False]) bytes
Force a given string to be a byte string type.
Changed in version 5.0: Added the
strings_only
parameter.- Parameters:
string (
bytes
orstr
orobject
) – The string to enforce, or an object that can cast to a string type.encoding (
str
, optional) – The optional encoding, if encoding Unicode strings.strings_only (
bool
, optional) –Whether to only transform byte/Unicode strings.
If
True
(the default), any other type will result in an error.If
False
, the object will be cast to a string using the object’s__bytes__()
or__str__()
method.New in version 5.0.
- Returns:
The string as a byte string.
- Return type:
- Raises:
ValueError – The given string was not a supported type.
- rbtools.utils.encoding.force_unicode(string: Union[bytes, str], encoding: str = 'utf-8', *, strings_only: Literal[True] = True) str [source]¶
- rbtools.utils.encoding.force_unicode(string: Any, encoding: str = 'utf-8', *, strings_only: Literal[False]) str
Force a given string to be a Unicode string type.
Changed in version 5.0: Added the
strings_only
parameter.- Parameters:
string (
bytes
orstr
orobject
) – The string to enforce, or an object that can cast to a string type.encoding (
str
, optional) – The optional encoding, if decoding byte strings.strings_only (
bool
, optional) –Whether to only transform byte/Unicode strings.
If
True
(the default), any other type will result in an error.If
False
, the object will be cast to a string using the object’s__str__()
method.New in version 5.0.
- Returns:
The string as a unicode type.
- Return type:
- Raises:
ValueError – The given string was not a supported type.