Skip to main content

Object-oriented URL from `urllib.parse` and `pathlib`

Project description

urlpath provides URL manipulator class that extends pathlib.PurePath.

Requirements

Install

pip install urlpath

Examples

Import:

>>> from urlpath import URL

Create object:

>>> url = URL(
...     'https://username:password@secure.example.com:1234/path/to/file.ext?field1=1&field2=2&field1=3#fragment')

Representation:

>>> url
URL('https://username:password@secure.example.com:1234/path/to/file.ext?field1=1&field2=2&field1=3#fragment')
>>> print(url)
https://username:password@secure.example.com:1234/path/to/file.ext?field1=1&field2=2&field1=3#fragment
>>> url.as_uri()
'https://username:password@secure.example.com:1234/path/to/file.ext?field1=1&field2=2&field1=3#fragment'
>>> url.as_posix()
'https://username:password@secure.example.com:1234/path/to/file.ext?field1=1&field2=2&field1=3#fragment'

Access pathlib.PurePath compatible properties:

>>> url.drive
'https://username:password@secure.example.com:1234'
>>> url.root
'/'
>>> url.anchor
'https://username:password@secure.example.com:1234/'
>>> url.path
'/path/to/file.ext'
>>> url.name
'file.ext'
>>> url.suffix
'.ext'
>>> url.suffixes
['.ext']
>>> url.stem
'file'
>>> url.parts
('https://username:password@secure.example.com:1234/', 'path', 'to', 'file.ext')
>>> url.parent
URL('https://username:password@secure.example.com:1234/path/to')

Access scheme:

>>> url.scheme
'https'

Access netloc:

>>> url.netloc
'username:password@secure.example.com:1234'
>>> url.username
'username'
>>> url.password
'password'
>>> url.hostname
'secure.example.com'
>>> url.port
1234

Access query:

>>> url.query
'field1=1&field2=2&field1=3'
>>> url.form_fields
(('field1', '1'), ('field2', '2'), ('field1', '3'))
>>> url.form
<FrozenMultiDict {'field1': ['1', '3'], 'field2': ['2']}>
>>> url.form.get_one('field1')
'1'
>>> url.form.get_one('field3') is None
True

Access fragment:

>>> url.fragment
'fragment'

Path operation:

>>> url / 'suffix'
URL('https://username:password@secure.example.com:1234/path/to/file.ext/suffix')
>>> url / '../../rel'
URL('https://username:password@secure.example.com:1234/path/to/file.ext/../../rel')
>>> (url / '../../rel').resolve()
URL('https://username:password@secure.example.com:1234/path/rel')
>>> url / '/'
URL('https://username:password@secure.example.com:1234/')
>>> url / 'http://example.com/'
URL('http://example.com/')

Replace components:

>>> url.with_scheme('http')
URL('http://username:password@secure.example.com:1234/path/to/file.ext?field1=1&field2=2&field1=3#fragment')
>>> url.with_netloc('www.example.com')
URL('https://www.example.com/path/to/file.ext?field1=1&field2=2&field1=3#fragment')
>>> url.with_userinfo('joe', 'pa33')
URL('https://joe:pa33@secure.example.com:1234/path/to/file.ext?field1=1&field2=2&field1=3#fragment')
>>> url.with_hostinfo('example.com', 8080)
URL('https://username:password@example.com:8080/path/to/file.ext?field1=1&field2=2&field1=3#fragment')
>>> url.with_fragment('new fragment')
URL('https://username:password@secure.example.com:1234/path/to/file.ext?field1=1&field2=2&field1=3#new fragment')
>>> url.with_components(username=None, password=None, query='query', fragment='frag')
URL('https://secure.example.com:1234/path/to/file.ext?query#frag')

Replace query:

>>> url.with_query({'field3': '3', 'field4': [1, 2, 3]})
URL('https://username:password@secure.example.com:1234/path/to/file.ext?field3=3&field4=1&field4=2&field4=3#fragment')
>>> url.with_query(field3='3', field4=[1, 2, 3])
URL('https://username:password@secure.example.com:1234/path/to/file.ext?field3=3&field4=1&field4=2&field4=3#fragment')
>>> url.with_query('query')
URL('https://username:password@secure.example.com:1234/path/to/file.ext?query#fragment')
>>> url.with_query(None)
URL('https://username:password@secure.example.com:1234/path/to/file.ext#fragment')

Jail:

>>> root = 'http://www.example.com/app/'
>>> current = 'http://www.example.com/app/path/to/content'
>>> url = URL(root).jailed / current
>>> url / '/root'
JailedURL('http://www.example.com/app/root')
>>> (url / '../../../../../../root').resolve()
JailedURL('http://www.example.com/app/root')
>>> url / 'http://localhost/'
JailedURL('http://www.example.com/app/')
>>> url / 'http://www.example.com/app/file'
JailedURL('http://www.example.com/app/file')

Trailing separator will be remained:

>>> url = URL('http://www.example.com/path/with/trailing/sep/')
>>> str(url).endswith('/')
True
>>> url.name
'sep'
>>> url.path
'/path/with/trailing/sep/'
>>> url.parts[-1]
'sep'

>>> url = URL('http://www.example.com/path/with/trailing/sep')
>>> str(url).endswith('/')
False
>>> url.name
'sep'
>>> url.path
'/path/with/trailing/sep'
>>> url.parts[-1]
'sep'

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

urlpath-1.0.0.zip (11.2 kB view details)

Uploaded Source

Built Distribution

urlpath-1.0.0-py3.4.egg (12.3 kB view details)

Uploaded Source

File details

Details for the file urlpath-1.0.0.zip.

File metadata

  • Download URL: urlpath-1.0.0.zip
  • Upload date:
  • Size: 11.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for urlpath-1.0.0.zip
Algorithm Hash digest
SHA256 31f741bccbb4d488a7c2faf330c5d92d0278f6398d3534ebf708167b4ef32299
MD5 52ce3aad3a9dcfc6bb1c376b721a71df
BLAKE2b-256 53da1df2a04d1c5ebc7fba13122b32ed26c91b25f72f2091b49cff08dfd9f9d5

See more details on using hashes here.

File details

Details for the file urlpath-1.0.0-py3.4.egg.

File metadata

  • Download URL: urlpath-1.0.0-py3.4.egg
  • Upload date:
  • Size: 12.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for urlpath-1.0.0-py3.4.egg
Algorithm Hash digest
SHA256 f34714a9efc6d2820fc376632f12960c7f47d97656f58426399d3ad4628d0e5e
MD5 324965b0b3cc495d8f5a9fbdcd912a2a
BLAKE2b-256 9496fd142ba420bebbaf7007f6e9f5918dad9fda836b1940a5d234d9f72a9344

See more details on using hashes here.

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