Skip to main content

A simple URL class for easy URL-building and manipulation

Project description

A simple, immutable URL class with a clean API for interrogation and manipulation.

Install

Still a work-in-progress - not on PyPi yet, but you can install directly from Github:

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

Use

Construct:

from purl import URL

# Explicit constructor
u = URL(scheme='https', host='www.google.com', path='/search', query='q=testing')

# Use factory
u = URL.from_string('https://www.google.com/search?q=testing')

# Combine
u = URL.from_string('http://www.google.com').path('search') \
                                            .query_param('q', 'testing')

URL objects are immutable - all mutator methods return a new instance.

Interrogate:

u.scheme()      # 'https'
u.host()        # 'www.google.com'
u.domain()      # 'www.google.com' - alias of host
u.port()        # 80
u.path()        # '/search'
u.query()       # 'q=testing'
u.fragment()    # 'q=testing'

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.subdmains()   # ['www', 'google', 'com']
u.subdmain(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) # returns 'codeinthehole'

# Mutate (creates a new instance)
new_url = u.path_segment(0, 'tangentlabs') # returns new URL object

Contribute

Clone and install testing dependencies:

pip install -r testing-requirements.txt

Ensure tests pass:

nosetests

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-0.1.tar.gz (4.3 kB view details)

Uploaded Source

File details

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

File metadata

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

File hashes

Hashes for purl-0.1.tar.gz
Algorithm Hash digest
SHA256 4476990ab89c74a7ad7d4f7d28279398ad8c031c3996019cda923276ccacd883
MD5 cc4c8c76da295d811ff08005cfa139c1
BLAKE2b-256 1a8992a5ae34537ea42d6a3382129e96d5a26e07ebdd63a6bdf28dae62b2f2e8

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