1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
|
"""Utilities for tests.
"""
import codecs
import re
def strip_ansi(string):
"""Strip ANSI encoding from given string.
Parameters
----------
string : str
String from which encoding needs to be removed
Returns
-------
str
Encoding free string
"""
pattern = r'(\x1b\[|\x9b)[^@-_]*[@-_]|\x1b[@-_]'
return re.sub(pattern, '', string, flags=re.I)
def find_colors(string):
"""Find colors from given string
Parameters
----------
string : str
String from which colors need to be find
Returns
-------
str
List of found colors
"""
return re.findall(r'\[\d\dm', string, flags=re.I)
def decode_utf_8_text(text):
"""Decodes the text from utf-8 format.
Parameters
----------
text : str
Text to be decoded
Returns
-------
str
Decoded text
"""
try:
return codecs.decode(text, 'utf-8')
except (TypeError, ValueError):
return text
def encode_utf_8_text(text):
"""Encodes the text to utf-8 format
Parameters
----------
text : str
Text to be encoded
Returns
-------
str
Encoded text
"""
try:
return codecs.encode(text, 'utf-8')
except (TypeError, ValueError):
return text
|