Miscellaneous functions for manipulating text¶
Collection of text functions that don’t fit in another category.
Changed in version kitchen: 1.2.0, API: kitchen.text 2.2.0
Added isbasestring(),
isbytestring(), and
isunicodestring() to help tell which string type
is which on python2 and python3
- kitchen.text.misc.byte_string_valid_encoding(byte_string, encoding='utf-8')¶
Detect if a byte
bytesis valid in a specific encoding- Parameters:
byte_string – Byte
bytesto test for bytes not valid in this encodingencoding – encoding to test against. Defaults to UTF-8.
- Returns:
Trueif there are no invalid UTF-8 characters.Falseif an invalid character is detected.
Note
This function checks whether the byte
bytesis valid in the specified encoding. It does not detect whether the bytebytesactually was encoded in that encoding. If you want that sort of functionality, you probably want to useguess_encoding()instead.
- kitchen.text.misc.byte_string_valid_xml(byte_string, encoding='utf-8')¶
Check that a byte
byteswould be valid in xml- Parameters:
byte_string – Byte
bytesto checkencoding – Encoding of the xml file. Default: UTF-8
- Returns:
Trueif the string is valid.Falseif it would be invalid in the xml file
In some cases you’ll have a whole bunch of byte strings and rather than transforming them to
strand back to bytebytesfor output to xml, you will just want to make sure they work with the xml file you’re constructing. This function will help you do that. Example:ARRAY_OF_MOSTLY_UTF8_STRINGS = [...] processed_array = [] for string in ARRAY_OF_MOSTLY_UTF8_STRINGS: if byte_string_valid_xml(string, 'utf-8'): processed_array.append(string) else: processed_array.append(guess_bytes_to_xml(string, encoding='utf-8')) output_xml(processed_array)
- kitchen.text.misc.guess_encoding(byte_string, disable_chardet=False)¶
Try to guess the encoding of a byte
bytes- Parameters:
byte_string – byte
bytesto guess the encoding ofdisable_chardet – If this is True, we never attempt to use
chardetto guess the encoding. This is useful if you need to have reproducibility whetherchardetis installed or not. Default:False.
- Raises:
TypeError – if
byte_stringis not a bytebytestype- Returns:
string containing a guess at the encoding of
byte_string. This is appropriate to pass as the encoding argument when encoding and decoding unicode strings.
We start by attempting to decode the byte
bytesas UTF-8. If this succeeds we tell the world it’s UTF-8 text. If it doesn’t andchardetis installed on the system anddisable_chardetis False this function will use it to try detecting the encoding ofbyte_string. If it is not installed orchardetcannot determine the encoding with a high enough confidence then we rather arbitrarily claim that it islatin-1. Sincelatin-1will encode to every byte, decoding fromlatin-1tostrwill not causeUnicodeErrorsalthough the output might be mangled.
- kitchen.text.misc.html_entities_unescape(string)¶
Substitute unicode characters for HTML entities
- Parameters:
string –
strstring to substitute out html entities- Raises:
TypeError – if something other than a
strstring is given- Return type:
strstring- Returns:
The plain text without html entities
- kitchen.text.misc.isbasestring(obj)¶
Determine if obj is a byte
bytesorstrstringIn python2 this is eqiuvalent to isinstance(obj, basestring). In python3 it checks whether the object is an instance of str, bytes, or bytearray. This is an aid to porting code that needed to test whether an object was derived from basestring in python2 (commonly used in unicode-bytes conversion functions)
- Parameters:
obj – Object to test
- Returns:
True if the object is a
basestring. Otherwise False.
Added in version Kitchen:: 1.2.0, API kitchen.text 2.2.0
- kitchen.text.misc.isbytestring(obj)¶
Determine if obj is a byte
bytesIn python2 this is equivalent to isinstance(obj, str). In python3 it checks whether the object is an instance of bytes or bytearray.
- Parameters:
obj – Object to test
- Returns:
True if the object is a byte
bytes. Otherwise, False.
Added in version Kitchen:: 1.2.0, API kitchen.text 2.2.0
- kitchen.text.misc.isunicodestring(obj)¶
Determine if obj is a
strstringIn python2 this is equivalent to isinstance(obj, unicode). In python3 it checks whether the object is an instance of
bytes.- Parameters:
obj – Object to test
- Returns:
True if the object is a
strstring. Otherwise, False.
Added in version Kitchen:: 1.2.0, API kitchen.text 2.2.0
- kitchen.text.misc.process_control_chars(string, strategy='replace')¶
Look for and transform control characters in a string
- Parameters:
string – string to search for and transform control characters within
strategy –
XML does not allow ASCII control characters. When we encounter those we need to know what to do. Valid options are:
- replace:
(default) Replace the control characters with
"?"- ignore:
Remove the characters altogether from the output
- strict:
Raise a
ControlCharErrorwhen we encounter a control character
- Raises:
TypeError – if
stringis not a unicode string.ValueError – if the strategy is not one of replace, ignore, or strict.
kitchen.text.exceptions.ControlCharError – if the strategy is
strictand a control character is present in thestring
- Returns:
strstring with no control characters in it.
Changed in version kitchen: 1.2.0, API: kitchen.text 2.2.0 Strip out the C1 control characters in addition to the C0 control characters.
- kitchen.text.misc.str_eq(str1, str2, encoding='utf-8', errors='replace')¶
Compare two strings, converting to byte
bytesif one isstr- Parameters:
str1 – First string to compare
str2 – Second string to compare
encoding – If we need to convert one string into a byte
bytesto compare, the encoding to use. Default is utf-8.errors – What to do if we encounter errors when encoding the string. See the
kitchen.text.converters.to_bytes()documentation for possible values. The default isreplace.
This function prevents
UnicodeError(python-2.4 or less) andUnicodeWarning(python 2.5 and higher) when we compare astrstring to a bytebytes. The errors normally arise because the conversion is done to ASCII. This function lets you convert to utf-8 or another encoding instead.Note
When we need to convert one of the strings from
strin order to compare them we convert thestrstring into a bytebytes. That means that strings can compare differently if you use different encodings for each.Note that
str1 == str2is faster than this function if you can accept the following limitations:Limited to python-2.5+ (otherwise a
UnicodeDecodeErrormay be thrown)Will generate a
UnicodeWarningif non-ASCII bytebytesis compared tostrstring.