Codecs¶
flutils contains additional codecs that, when registered, can be used to convert bytes to strings and strings to bytes.
b64¶
The
b64
codec will decode bytes to base64 string characters; and will encode strings containing base64 string characters into bytes. Base64 string characters can span multiple lines and may have whitespace indentation.New in version 0.4.
Raw UTF-8 Escape¶
The
raw_utf8_escape
codec will decode a byte string containing escaped UTF-8 hexadecimal into a string with the proper characters. Strings encoded with theraw_utf8_escape
codec will be of ascii bytes and have escaped UTF-8 hexadecimal used for non printable characters and each character with anord
value above127
New in version 0.4.
Registering Codecs¶
Using any of the above codecs requires registering them with Python by using the following function:
register_codecs
()[source]¶Register additional codecs.
New in version 0.4.
- Return type
Examples
>>> from flutils.codecs import register_codecs >>> register_codecs() >>> 'test©'.encode('raw_utf8_escape') b'test\\xc2\\xa9' >>> b'test\\xc2\\xa9'.decode('raw_utf8_escape') 'test©' >>> 'dGVzdA=='.encode('b64') b'test' >>> b'test'.decode('b64') 'dGVzdA=='
General¶
-
get_encoding
(name=None, default='UTF-8')[source]¶ Validate and return the given encoding codec name.
- Parameters
name (str) – The name of the encoding to validate. if empty or invalid then the value of the given
default
will be returned.default (str, optional) – If set, this encoding name will be returned if the given
name
is invalid. Defaults to:SYSTEM_ENCODING
. If set toNone
which will raise aLookupError
if the givenname
is not valid.
- Raises
LookupError – If the given
name
is not a valid encoding codec name and the givendefault
is set toNone
or an empty string.LookupError – If the given
default
is not a valid encoding codec name.
- Return type
- Returns
str – The encoding codec name.
Example
>>> from flutils.codecs import get_encoding >>> get_encoding() 'utf-8'