Skip to main content

An immutable URL class for easy URL-building and manipulation

Project description

A simple, immutable URL class with a clean API for interrogation and manipulation. Supports Python 2.6, 2.7, 3.3, 3.4 and pypy.

Also supports template URLs as per RFC 6570

https://secure.travis-ci.org/codeinthehole/purl.png https://pypip.in/v/purl/badge.png https://pypip.in/d/purl/badge.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(u'https://www.google.com/search?q=testing')
>>> u.scheme()
u'https'
>>> u.host()
u'www.google.com'
>>> u.domain()
u'www.google.com'
>>> u.username()
>>> u.password()
>>> u.netloc()
u'www.google.com'
>>> u.port()
>>> u.path()
u'/search'
>>> u.query()
u'q=testing'
>>> u.fragment()
u''
>>> u.path_segment(0)
u'search'
>>> u.path_segments()
(u'search',)
>>> u.query_param('q')
u'testing'
>>> u.query_param('q', as_list=True)
[u'testing']
>>> u.query_param('lang', default=u'GB')
u'GB'
>>> u.query_params()
{u'q': [u'testing']}
>>> u.has_query_param('q')
True
>>> u.has_query_params(('q', 'r'))
False
>>> u.subdomains()
[u'www', u'google', u'com']
>>> u.subdomain(0)
u'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)
u'codeinthehole'

# Mutate (creates a new instance)
>>> new_url = u.path_segment(0, 'tangentlabs')
>>> new_url is u
False
>>> new_url.path_segment(0)
u'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()
u'http://www.example.com/some/path?q=search+term'

Along with the above overloaded methods, there is also a add_path_segment method for adding a segment at the end of the current path:

>>> new_url = u.add_path_segment('here')
>>> new_url.as_string()
u'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 dictionary

  • It can be pickled and restored

  • It supports equality operations

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()
u'http://example.com/red/green/blue'

or the expand function:

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

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

Changelog

v1.0

  • Slight tidy up. Document support for PyPy and Python 3.4.

v0.8

  • Support for RFC 6570 URI templates

v0.7

  • All internal strings are unicode.

  • Support for unicode chars in path, fragment, query, auth added.

v0.6

  • Added append_query_param method

  • Added remove_query_param method

v0.5

  • Added support for Python 3.2/3.3 (thanks @pmcnr and @mitchellrj)

v0.4.1

  • Added API docs

  • Added to readthedocs.org

v0.4

  • Modified constructor to accept full URL string as first arg

  • Added add_path_segment method

v0.3.2

  • Fixed bug port number in string when using from_string constructor

v0.3.1

  • Fixed bug with passing lists to query param setter methods

v0.3

  • Added support for comparison and equality

  • Added support for pickling

  • Added __slots__ so instances can be used as keys within dictionaries

Contribute

Clone and install testing dependencies:

$ makeinstall

Ensure tests pass using:

$ ./runtests.sh

or:

$ tox

Hack away.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

purl-1.0.tar.gz (11.0 kB view details)

Uploaded Source

Built Distribution

purl-1.0-py2.py3-none-any.whl (11.2 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file purl-1.0.tar.gz.

File metadata

  • Download URL: purl-1.0.tar.gz
  • Upload date:
  • Size: 11.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for purl-1.0.tar.gz
Algorithm Hash digest
SHA256 bf8ae85bb10da4d11b5495bd56229cc76b6cfc880413fef85241632000ad9423
MD5 2c22f05c6edd6e7da68f35c0d8d91742
BLAKE2b-256 00ef367d217834f07bf9dde631265d5389dcb9586e969c3ce37e2fee561e572a

See more details on using hashes here.

Provenance

File details

Details for the file purl-1.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for purl-1.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 72c59f14fa51e7cd27a4e8b40f4e06dd2e2952c602c67dc4fba90aa4ecefa40a
MD5 125a23019ccec70b9dab60921c5296f4
BLAKE2b-256 e6d62b83715de2163c2bfb91846496177b44719ef1fb02760c4161f0c1687e06

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page