Skip to content

Easy URL

py_simple.easy_url

easy_url reads and edits links without the urllib boilerplate.

EasyUrlError

Bases: Exception

Raised when a link cannot be read or updated.

Parameters:

Name Type Description Default
message str

Description of what went wrong.

required

add_query_param(url, key, value)

Add or replace one query parameter on a link.

Parameters:

Name Type Description Default
url str

A full link.

required
key str

Parameter name, such as "page".

required
value str

Parameter value, such as "2".

required

Returns:

Name Type Description
str str

The link with that parameter set.

Raises:

Type Description
EasyUrlError

If url is not a full link, or if key or value is not a string.

Example
from py_simple import add_query_param

print(add_query_param("https://school.example/clubs", "page", "2"))
from urllib.parse import parse_qsl, urlencode, urlparse, urlunparse

parsed = urlparse("https://school.example/clubs")
query = dict(parse_qsl(parsed.query))
query["page"] = "2"
print(urlunparse(parsed._replace(query=urlencode(query))))

get_domain(url)

Return the host name from a link.

Parameters:

Name Type Description Default
url str

A full link, such as "https://school.example/clubs".

required

Returns:

Name Type Description
str str

The host, such as "school.example".

Raises:

Type Description
EasyUrlError

If url is not a full link.

Example
from py_simple import get_domain

print(get_domain("https://school.example/clubs"))
from urllib.parse import urlparse

print(urlparse("https://school.example/clubs").hostname)

get_path(url)

Return the path component from a link.

Parameters:

Name Type Description Default
url str

A full link, such as "https://school.example/clubs/art".

required

Returns:

Name Type Description
str str

The path, such as "/clubs/art". Returns "/" if no path is given.

Raises:

Type Description
EasyUrlError

If url is not a full link.

Example
from py_simple import get_path

print(get_path("https://school.example/clubs/art"))
from urllib.parse import urlparse

print(urlparse("https://school.example/clubs/art").path or "/")

is_https(url)

Check whether a link uses HTTPS.

Parameters:

Name Type Description Default
url str

A full link.

required

Returns:

Name Type Description
bool bool

True when the scheme is https.

Raises:

Type Description
EasyUrlError

If url is not a full link.

Example
from py_simple import is_https

is_https("https://school.example")
from urllib.parse import urlparse

urlparse("https://school.example").scheme == "https"