Package: purl
A simple, immutable URL class with a clean API for interrogation and
manipulation.  Supports Pythons 2.7, 3.3, 3.4, 3.5, 3.6, 3.7, 3.8 and pypy.Also supports template URLs as perRFC 6570Contents:DocsInstallUseChangelogContributeDocshttp://purl.readthedocs.org/en/latest/InstallFrom PyPI (stable):$ pip install purlFrom Github (unstable):$ pip install git+git://github.com/codeinthehole/purl.git#egg=purlUseConstruct:>>>frompurlimportURL# String constructor>>>from_str=URL('https://www.google.com/search?q=testing')# Keyword constructor>>>from_kwargs=URL(scheme='https',host='www.google.com',path='/search',query='q=testing')# Combine>>>from_combo=URL('https://www.google.com').path('search').query_param('q','testing')URL objects are immutable - all mutator methods return a new instance.Interrogate:>>>u=URL('https://www.google.com/search?q=testing')>>>u.scheme()'https'>>>u.host()'www.google.com'>>>u.domain()'www.google.com'>>>u.username()>>>u.password()>>>u.netloc()'www.google.com'>>>u.port()>>>u.path()'/search'>>>u.query()'q=testing'>>>u.fragment()''>>>u.path_segment(0)'search'>>>u.path_segments()('search',)>>>u.query_param('q')'testing'>>>u.query_param('q',as_list=True)['testing']>>>u.query_param('lang',default='GB')'GB'>>>u.query_params(){'q':['testing']}>>>u.has_query_param('q')True>>>u.has_query_params(('q','r'))False>>>u.subdomains()['www','google','com']>>>u.subdomain(0)'www'Note that each accessor method is overloaded to be a mutator method too, similar
to the jQuery API.  Eg:>>>u=URL.from_string('https://github.com/codeinthehole')# Access>>>u.path_segment(0)'codeinthehole'# Mutate (creates a new instance)>>>new_url=u.path_segment(0,'tangentlabs')>>>new_urlisuFalse>>>new_url.path_segment(0)'tangentlabs'Hence, you can build a URL up in steps:>>>u=URL().scheme('http').domain('www.example.com').path('/some/path').query_param('q','search term')>>>u.as_string()'http://www.example.com/some/path?q=search+term'Along with the above overloaded methods, there is also aadd_path_segmentmethod for adding a segment at the end of the current path:>>>new_url=u.add_path_segment('here')>>>new_url.as_string()'http://www.example.com/some/path/here?q=search+term'Couple of other things:Since the URL class is immutable it can be used as a key in a dictionaryIt can be pickled and restoredIt supports equality operationsIt supports equality operationsURL templates can be used either via aTemplateclass:>>>frompurlimportTemplate>>>tpl=Template("http://example.com{/list*}")>>>url=tpl.expand({'list':['red','green','blue']})>>>url.as_string()'http://example.com/red/green/blue'or theexpandfunction:>>>frompurlimportexpand>>>expand(u"{/list*}",{'list':['red','green','blue']})'/red/green/blue'A wide variety of expansions are possible - refer to theRFCfor more details.Changelogv1.6 - 2021-05-15Usepytestinsteed ofnose.Fix warning around regex string.v1.5 - 2019-03-10Allow@in passwords.v1.4 - 2018-03-11Allow usernames and passwords to be removed from URLs.v1.3.1Ensure paths always have a leading slash.v1.3Allow absolute URLs to be converted into relative.v1.2Support password-less URLs.Allow slashes to be passed as path segments.v1.1Support setting username and password via mutator methodsv1.0.3Handle some unicode compatibility edge-casesv1.0.2Fix template expansion bug with no matching variables being passed in. This
ensurespurl.Templateworks correctly with the URLs returned from the
Github API.v1.0.1Fix bug with special characters in paths not being escaped.v1.0Slight tidy up. Document support for PyPy and Python 3.4.v0.8Support for RFC 6570 URI templatesv0.7All internal strings are unicode.Support for unicode chars in path, fragment, query, auth added.v0.6Addedappend_query_parammethodAddedremove_query_parammethodv0.5Added support for Python 3.2/3.3 (thanks @pmcnr and @mitchellrj)v0.4.1Added API docsAdded to readthedocs.orgv0.4Modified constructor to accept full URL string as first argAddedadd_path_segmentmethodv0.3.2Fixed bug port number in string when using from_string constructorv0.3.1Fixed bug with passing lists to query param setter methodsv0.3Added support for comparison and equalityAdded support for picklingAdded__slots__so instances can be used as keys within dictionariesContributeClone, create a virtualenv then install purl and the packages required for
testing:$ git clone git@github.com:codeinthehole/purl.git
$ cd purl
$ mkvirtualenv purl  # requires virtualenvwrapper
(purl) $ makeEnsure tests pass using:(purl) $ pytestor:$ tox