33
44Usage is straightforward::
55
6- >>> from hyperlink import URL
7- >>> url = URL.from_text (u'http://github.com/mahmoud/hyperlink?utm_source=docs')
6+ >>> import hyperlink
7+ >>> url = hyperlink.parse (u'http://github.com/mahmoud/hyperlink?utm_source=docs')
88 >>> url.host
99 u'github.com'
1010 >>> secure_url = url.replace(scheme=u'https')
1111 >>> secure_url.get('utm_source')[0]
1212 u'docs'
1313
14- As seen here, the API revolves around the lightweight and immutable
15- :class:`URL` type, documented below.
14+ Hyperlink's API centers on the :class:`DecodedURL` type, which wraps
15+ the lower-level :class:`URL`, both of which can be returned by the
16+ :func:`parse()` convenience function.
17+
1618""" # noqa: E501
1719
1820import re
@@ -1971,13 +1973,25 @@ def remove(
19711973
19721974EncodedURL = URL # An alias better describing what the URL really is
19731975
1976+ _EMPTY_URL = URL ()
1977+
19741978
19751979class DecodedURL (object ):
1976- """DecodedURL is a type meant to act as a higher-level interface to
1977- the URL. It is the `unicode` to URL's `bytes`. `DecodedURL` has
1978- almost exactly the same API as `URL`, but everything going in and
1979- out is in its maximally decoded state. All percent decoding is
1980- handled automatically.
1980+ """
1981+ :class:`DecodedURL` is a type designed to act as a higher-level
1982+ interface to :class:`URL` and the recommended type for most
1983+ operations. By analogy, :class:`DecodedURL` is the
1984+ :class:`unicode` to URL's :class:`bytes`.
1985+
1986+ :class:`DecodedURL` automatically handles encoding and decoding
1987+ all its components, such that all inputs and outputs are in a
1988+ maximally-decoded state. Note that this means, for some special
1989+ cases, a URL may not "roundtrip" character-for-character, but this
1990+ is considered a good tradeoff for the safety of automatic
1991+ encoding.
1992+
1993+ Otherwise, :class:`DecodedURL` has almost exactly the same API as
1994+ :class:`URL`.
19811995
19821996 Where applicable, a UTF-8 encoding is presumed. Be advised that
19831997 some interactions can raise :exc:`UnicodeEncodeErrors` and
@@ -1991,9 +2005,20 @@ class DecodedURL(object):
19912005 lazy (bool): Set to True to avoid pre-decode all parts of the URL to
19922006 check for validity. Defaults to False.
19932007
2008+ .. note::
2009+
2010+ The :class:`DecodedURL` initializer takes a :class:`URL` object,
2011+ not URL components, like :class:`URL`. To programmatically
2012+ construct a :class:`DecodedURL`, you can use this pattern:
2013+
2014+ >>> print(DecodedURL().replace(scheme=u'https',
2015+ ... host=u'pypi.org', path=(u'projects', u'hyperlink')).to_text())
2016+ https://pypi.org/projects/hyperlink
2017+
2018+ .. versionadded:: 18.0.0
19942019 """
19952020
1996- def __init__ (self , url , lazy = False ):
2021+ def __init__ (self , url = _EMPTY_URL , lazy = False ):
19972022 # type: (URL, bool) -> None
19982023 self ._url = url
19992024 if not lazy :
@@ -2353,22 +2378,29 @@ def __dir__(self):
23532378
23542379def parse (url , decoded = True , lazy = False ):
23552380 # type: (Text, bool, bool) -> Union[URL, DecodedURL]
2356- """Automatically turn text into a structured URL object.
2381+ """
2382+ Automatically turn text into a structured URL object.
2383+
2384+ >>> url = parse(u"https://github.com/python-hyper/hyperlink")
2385+ >>> print(url.to_text())
2386+ https://github.com/python-hyper/hyperlink
23572387
23582388 Args:
2359- url (Text ): A string representation of a URL.
2389+ url (str ): A text string representation of a URL.
23602390
23612391 decoded (bool): Whether or not to return a :class:`DecodedURL`,
23622392 which automatically handles all
23632393 encoding/decoding/quoting/unquoting for all the various
2364- accessors of parts of the URL, or an :class:`EncodedURL `,
2394+ accessors of parts of the URL, or a :class:`URL `,
23652395 which has the same API, but requires handling of special
23662396 characters for different parts of the URL.
23672397
23682398 lazy (bool): In the case of `decoded=True`, this controls
23692399 whether the URL is decoded immediately or as accessed. The
23702400 default, `lazy=False`, checks all encoded parts of the URL
23712401 for decodability.
2402+
2403+ .. versionadded:: 18.0.0
23722404 """
23732405 enc_url = EncodedURL .from_text (url )
23742406 if not decoded :
0 commit comments