Skip to content

Instantly share code, notes, and snippets.

@itkr
Created August 13, 2019 03:06
Show Gist options
  • Save itkr/e75726b7da56d8b29fa1ebcc245f6460 to your computer and use it in GitHub Desktop.
Save itkr/e75726b7da56d8b29fa1ebcc245f6460 to your computer and use it in GitHub Desktop.
url.py
# -*- coding: utf-8 -*-
from __future__ import absolute_import, print_function, unicode_literals
try:
from urllib.parse import parse_qsl, urlencode, urlparse, urlunparse
except ImportError:
from urllib import urlencode
from urlparse import parse_qsl, urlparse, urlunparse
class URL(str):
def __init__(self, url, **queries):
self.url = self._get_updated_url(url, queries)
@staticmethod
def _get_updated_url(url, queries):
(r_scheme, r_netloc, r_url, r_params, r_query, r_fragment) = urlparse(url)
query = parse_qsl(r_query)
query.extend(queries.items())
if query:
return urlunparse(
(r_scheme, r_netloc, r_url, r_params, urlencode(query), r_fragment, ))
return url
def __repr__(self):
return self.url
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment