def escape(value, quote=True):
def escape_one(match):<EOL><INDENT>return SPECIAL_CHARS[match.group(<NUM_LIT:0>)]<EOL><DEDENT>if isinstance(value, string_types):<EOL><INDENT>value = SPECIAL_CHARS_REGEX.sub(escape_one, value)<EOL>if quote:<EOL><INDENT>value = "<STR_LIT:'>" + value + "<STR_LIT:'>"<EOL><DEDENT><DEDENT>return text_type(value)<EOL>
If the value is a string, escapes any special characters and optionally
surrounds it with single quotes. If the value is not a string (e.g. a number),
converts it to one.