urlformat.py 438 B

12345678910111213
  1. # encoding=utf-8
  2. '''Validation and formatting of shortcode and URL data.'''
  3. import string
  4. def quote(text, safe=string.digits):
  5. '''URL Encoding similar to urllib's, but with more strict safe characters'''
  6. if not isinstance(text, (bytes, bytearray)):
  7. raise TypeError('quote() input must be bytes or bytesarray')
  8. safe = safe.encode('ascii', 'ignore')
  9. return ''.join(['%{:02X}'.format(b) if b not in safe else chr(b) for b in text])