code: def escape(to_escape, safe=SAFE, escape_char=ESCAPE_CHAR, allow_collisions=False):
      
      if isinstance(to_escape, bytes):
          # always work on text
          to_escape = to_escape.decode('utf8')
  
      if not isinstance(safe, set):
          safe = set(safe)
  
      if allow_collisions:
          safe.add(escape_char)
      elif escape_char in safe:
          # escape char can't be in safe list
          safe.remove(escape_char)
  
      chars = []
      for c in to_escape:
          if c in safe:
              chars.append(c)
          else:
              chars.append(_escape_char(c, escape_char))
      return u''.join(chars)
docs: Escape a string so that it only contains characters in a safe set.
  
      Characters outside the safe list will be escaped with _%x_,
      where %x is the hex value of the character.
  
      If `allow_collisions` is True, occurrences of `escape_char`
      in the input will not be escaped.
  
      In this case, `unescape` cannot be used to reverse the transform
      because occurrences of the escape char in the resulting string are ambiguous.
      Only use this mode when:
  
      1. collisions cannot occur or do not matter, and
      2. unescape will never be called.
  
      .. versionadded: 1.0
          allow_collisions argument.
          Prior to 1.0, behavior was the same as allow_collisions=False (default).