human
Complete this python program:
def _escape_string(text):
assistant
#Here is the Python code you requested.

    """Escape the given string so that it only contains ASCII characters
    of [a-zA-Z0-9_$].
    The dollar symbol ($) and other invalid characters are escaped into
    the string sequence of "$xx" where "xx" is the hex codepoint of the char.
    Multibyte characters are encoded into utf8 and converted into the above
    hex format.
    """
    def repl(m):
        return ''.join((('$%02x' % utils.asbyteint(ch)) for ch in m.group(0).encode('utf8')))
    ret = re.sub(_re_invalid_char, repl, text)
    if (not isinstance(ret, str)):
        return ret.encode('ascii')
    return ret