buildurl package

buildurl.BuildURL

class buildurl.BuildURL(base: str = '', force_trailing_slash: bool = False)[source]

Bases: object

Tool to simplify the creation of URLs with query parameters.

Parameters
  • base – The base URL to build upon.

  • force_trailing_slash – Whether or not to forcefully include a trailing slash at the end of path, False by default.

Examples

>>> from buildurl import BuildURL
>>> url = BuildURL("https://pypi.org")
>>> print(url.get)
https://pypi.org
>>> url = BuildURL("https://example.com/test",
... force_trailing_slash=True)
>>> print(url.get)
https://example.com/test/
__add__(query: Union[str, Dict[str, Any]]) buildurl.builder.BuildURL[source]

Generate new URL with added query.

Equivalent to first copying the URL, then using add_query.

Parameters

query – The query key and value to add.

Returns

New BuildURL instance.

Examples

>>> url = BuildURL("https://example.com")
>>> new_url = url + {"test": "it"}
>>> print(url.get)
https://example.com
>>> print(new_url.get)
https://example.com?test=it
__iadd__(query: Union[str, Dict[str, Any]]) buildurl.builder.BuildURL[source]

Add query arguments inplace.

Essentially a shortcut to add_query.

Parameters

query – The query key and value to add.

Returns

Reference to self.

Examples

>>> url = BuildURL("https://example.com")
>>> url += {"key": "value"}
>>> print(url.get)
https://example.com?key=value
>>> url += "another=query&more=stuff"
>>> print(url.get)
https://example.com?key=value&another=query&more=stuff
__init__(base: str = '', force_trailing_slash: bool = False)[source]

Initialize a new instance of BuildURL.

__itruediv__(path: Union[str, List[str]]) buildurl.builder.BuildURL[source]

Add new path part to the URL inplace.

Essentially a shortcut to add_path.

Parameters

path – New path to add.

Returns

Reference to self.

Examples

>>> url = BuildURL("https://example.com")
>>> url /= "test"
>>> print(url.get)
https://example.com/test
>>> url /= ["more", "paths"]
>>> print(url.get)
https://example.com/test/more/paths
>>> url /= "/again/and/again/"
>>> print(url.get)
https://example.com/test/more/paths/again/and/again/
__len__() int[source]

Length of the generated URL.

__repr__() str[source]

Representation of the current instance.

Returns

String representation of self.

Examples

>>> url = BuildURL("https://example.com/test?now=true")
>>> print(repr(url))
BuildURL(base='https://example.com/test?now=true', force_trailing_slash=False)
__str__() str[source]

Shortcut for getting the URL.

Can be obtained by printing the instance of the class.

Returns

Generated URL.

Examples

>>> url = BuildURL("https://example.com")
>>> url /= "test"
>>> print(str(url))
https://example.com/test
>>> print(url)
https://example.com/test
__truediv__(path: Union[str, List[str]]) buildurl.builder.BuildURL[source]

Generate new URL with added path.

Equivalent to first copying the URL, then using add_path.

Parameters

path – New path to add.

Returns

New BuildURL instance.

Examples

>>> url = BuildURL("https://example.com")
>>> new_url = url / "testing"
>>> print(url.get)
https://example.com
>>> print(new_url.get)
https://example.com/testing
add_path(*args: Union[str, List[str]]) buildurl.builder.BuildURL[source]

Add to the path.

Parameters

*args – The paths to add. Can be a string containing a single path, multiple paths separated by /, or a list of single path strings.

Returns

Reference to self.

Examples

>>> url = BuildURL("https://example.com")
>>> url.add_path("test")
BuildURL(...)
>>> print(url.get)
https://example.com/test
>>> url.add_path(["more", "paths"]).add_path("/again/and/again/")
BuildURL(...)
>>> print(url.get)
https://example.com/test/more/paths/again/and/again/
>>> url = BuildURL("https://example.com")
>>> url.add_path("never", "stopping", "to/play", ["with", "paths"])
BuildURL(...)
>>> print(url.get)
https://example.com/never/stopping/to/play/with/paths
add_query(*args: Union[str, Dict[str, Any]], **kwargs) buildurl.builder.BuildURL[source]

Add a query argument.

Parameters
  • *args – The query keys and values to add. Can be a string containing the keys and values, like “key1=value1&key2=value2”, or a dict, like {“key1”: “value1”, “key2”: “value2”}.

  • **kwargs – Keyword arguments corresponding to key-value pairs.

Returns

Reference to self.

Examples

>>> url = BuildURL("https://example.com")
>>> url.add_query({"key": "value"})
BuildURL(...)
>>> print(url.get)
https://example.com?key=value
>>> url.add_query("another=query&more=stuff")
BuildURL(...)
>>> print(url.get)
https://example.com?key=value&another=query&more=stuff
>>> url.add_query(a="b").add_query("c=d", "e=f")
BuildURL(...)
>>> print(url.get)
https://example.com?key=value&another=query&more=stuff&a=b&c=d&e=f
copy() buildurl.builder.BuildURL[source]

Create a deep copy of itself.

Examples

>>> url = BuildURL("https://example.com")
>>> url_copy = url.copy()
>>> url /= "test"
>>> print(url.get)
https://example.com/test
>>> print(url_copy.get)
https://example.com
property get: str

Get the generated URL.

property parts: Tuple[str, ...]

Tuple of necessary parts to construct the URL.

property path: str

Path string.

property query: str

Query string.

set_force_trailing_slash(enabled: bool = True) buildurl.builder.BuildURL[source]

Set the force_trailing_slash attribute.

Parameters

enabled – The new value for force_trailing_slash, default True.

Returns

Reference to self.

Examples

>>> url = BuildURL("https://example.com")
>>> url.set_force_trailing_slash().add_path("test")
BuildURL(base='https://example.com/test/', force_trailing_slash=True)
>>> url.set_force_trailing_slash(False)
BuildURL(base='https://example.com/test', force_trailing_slash=False)