Codecs¶
flutils contains additional codecs that, when registered, can be used to convert bytes to strings and strings to bytes.
b64¶
The
b64codec 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_escapecodec will decode a byte string containing escaped UTF-8 hexadecimal into a string with the proper characters. Strings encoded with theraw_utf8_escapecodec will be of ascii bytes and have escaped UTF-8 hexadecimal used for non printable characters and each character with anordvalue above127New 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
defaultwill be returned.default (str, optional) – If set, this encoding name will be returned if the given
nameis invalid. Defaults to:SYSTEM_ENCODING. If set toNonewhich will raise aLookupErrorif the givennameis not valid.
- Raises
LookupError – If the given
nameis not a valid encoding codec name and the givendefaultis set toNoneor an empty string.LookupError – If the given
defaultis 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'