utils.encoding.smart_str()

smart_str(s, encoding='utf-8', strings_only=False, errors='strict') Alias of smart_bytes() on Python 2 and smart_text() on Python 3. This function returns a str or a lazy string. For instance, this is suitable for writing to sys.stdout on Python 2 and 3.

utils.encoding.smart_bytes()

smart_bytes(s, encoding='utf-8', strings_only=False, errors='strict') [source] Returns a bytestring version of s, encoded as specified in encoding. If strings_only is True, don’t convert (some) non-string-like objects.

utils.encoding.python_2_unicode_compatible()

python_2_unicode_compatible() [source] A decorator that defines __unicode__ and __str__ methods under Python 2. Under Python 3 it does nothing. To support Python 2 and 3 with a single code base, define a __str__ method returning text and apply this decorator to the class.

utils.encoding.is_protected_type()

is_protected_type(obj) [source] Determine if the object instance is of a protected type. Objects of protected types are preserved as-is when passed to force_text(strings_only=True).

utils.encoding.iri_to_uri()

iri_to_uri(iri) [source] Convert an Internationalized Resource Identifier (IRI) portion to a URI portion that is suitable for inclusion in a URL. This is the algorithm from section 3.1 of RFC 3987#section-3.1. However, since we are assuming input is either UTF-8 or unicode already, we can simplify things a little from the full method. Takes an IRI in UTF-8 bytes and returns ASCII bytes containing the encoded result.

utils.encoding.force_unicode()

force_unicode(s, encoding='utf-8', strings_only=False, errors='strict') Historical name of force_text(). Only available under Python 2.

utils.encoding.force_text()

force_text(s, encoding='utf-8', strings_only=False, errors='strict') [source] Similar to smart_text, except that lazy instances are resolved to strings, rather than kept as lazy objects. If strings_only is True, don’t convert (some) non-string-like objects.

utils.encoding.force_str()

force_str(s, encoding='utf-8', strings_only=False, errors='strict') Alias of force_bytes() on Python 2 and force_text() on Python 3. This function always returns a str.

utils.encoding.force_bytes()

force_bytes(s, encoding='utf-8', strings_only=False, errors='strict') [source] Similar to smart_bytes, except that lazy instances are resolved to bytestrings, rather than kept as lazy objects. If strings_only is True, don’t convert (some) non-string-like objects.

utils.encoding.filepath_to_uri()

filepath_to_uri(path) [source] Convert a file system path to a URI portion that is suitable for inclusion in a URL. The path is assumed to be either UTF-8 or unicode. This method will encode certain characters that would normally be recognized as special characters for URIs. Note that this method does not encode the ‘ character, as it is a valid character within URIs. See encodeURIComponent() JavaScript function for more details. Returns an ASCII string containing the encoded result.