GitHub - codeinthehole/purl: A simple, immutable URL class with a clean API for interrogation and manipulation. (original) (raw)

purl - A simple Python URL class

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 per RFC 6570

Contents:

https://secure.travis-ci.org/codeinthehole/purl.png

Docs

http://purl.readthedocs.org/en/latest/

Install

From PyPI (stable):

$ pip install purl

From Github (unstable):

$ pip install git+git://github.com/codeinthehole/purl.git#egg=purl

Use

Construct:

from purl import URL

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_url is u False 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 a add_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:

URL templates can be used either via a Template class:

from purl import Template tpl = Template("http://example.com{/list*}") url = tpl.expand({'list': ['red', 'green', 'blue']}) url.as_string() 'http://example.com/red/green/blue'

or the expand function:

from purl import expand expand(u"{/list*}", {'list': ['red', 'green', 'blue']}) '/red/green/blue'

A wide variety of expansions are possible - refer to the RFC for more details.

Changelog

v1.6 - 2021-05-15

v1.5 - 2019-03-10

v1.4 - 2018-03-11

v1.3.1

v1.3

v1.2

v1.1

v1.0.3

v1.0.2

v1.0.1

v1.0

v0.8

v0.7

v0.6

v0.5

v0.4.1

v0.4

v0.3.2

v0.3.1

v0.3

Contribute

Clone, 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) $ make

Ensure tests pass using:

(purl) $ pytest

or:

$ tox