import re


def escape(toencode):
  """URL-encode data."""
  if toencode is None:
    return None
  return re.sub(r"[^a-zA-Z0-9_.-]", lambda m: "%%%02x" % ord(m.group()),
                str(toencode))