-
Notifications
You must be signed in to change notification settings - Fork 42
Adding parse and DecodedURL docs #126
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
a06eedb
1f57a36
4afeb55
6682bd9
b086352
182f834
2bae6f6
ca40c74
2d21b70
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,17 +39,17 @@ library. The easiest way to install is with pip:: | |
|
||
Then, URLs are just an import away:: | ||
|
||
from hyperlink import URL | ||
import hyperlink | ||
|
||
url = URL.from_text(u'http://github.com/python-hyper/hyperlink?utm_source=readthedocs') | ||
url = hyperlink.parse(u'http://github.com/python-hyper/hyperlink?utm_source=readthedocs') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FWIW I'm not a fan of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah I think |
||
|
||
better_url = url.replace(scheme=u'https', port=443) | ||
org_url = better_url.click(u'.') | ||
|
||
print(org_url.to_text()) | ||
# prints: https://github.com/python-hyper/ | ||
|
||
print(better_url.get(u'utm_source')) | ||
print(better_url.get(u'utm_source')[0]) | ||
# prints: readthedocs | ||
|
||
See :ref:`the API docs <hyperlink_api>` for more usage examples. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,16 +3,18 @@ | |
|
||
Usage is straightforward:: | ||
|
||
>>> from hyperlink import URL | ||
>>> url = URL.from_text(u'http://github.com/mahmoud/hyperlink?utm_source=docs') | ||
>>> import hyperlink | ||
>>> url = hyperlink.parse(u'http://github.com/mahmoud/hyperlink?utm_source=docs') | ||
>>> url.host | ||
u'github.com' | ||
>>> secure_url = url.replace(scheme=u'https') | ||
>>> secure_url.get('utm_source')[0] | ||
u'docs' | ||
|
||
As seen here, the API revolves around the lightweight and immutable | ||
:class:`URL` type, documented below. | ||
Hyperlink's API centers on the :class:`DecodedURL` type, which wraps | ||
the lower-level :class:`URL`, both of which can be returned by the | ||
:func:`parse()` convenience function. | ||
|
||
""" # noqa: E501 | ||
|
||
import re | ||
|
@@ -1743,13 +1745,23 @@ def remove( | |
|
||
EncodedURL = URL # An alias better describing what the URL really is | ||
|
||
_EMPTY_URL = URL() | ||
|
||
class DecodedURL(object): | ||
"""DecodedURL is a type meant to act as a higher-level interface to | ||
the URL. It is the `unicode` to URL's `bytes`. `DecodedURL` has | ||
almost exactly the same API as `URL`, but everything going in and | ||
out is in its maximally decoded state. All percent decoding is | ||
handled automatically. | ||
""":class:`DecodedURL` is a type designed to act as a higher-level | ||
interface to :class:`URL` and the recommended type for most | ||
operations. By analogy, :class:`DecodedURL` is the | ||
:class:`unicode` to URL's :class:`bytes`. | ||
|
||
:class:`DecodedURL` automatically handles encoding and decoding | ||
all its components, such that all inputs and outputs are in a | ||
maximally-decoded state. Note that this means, for some special | ||
cases, a URL may not "roundtrip" character-for-character, but this | ||
is considered a good tradeoff for the safety of automatic | ||
encoding. | ||
|
||
Otherwise, :class:`DecodedURL` has almost exactly the same API as | ||
:class:`URL`. | ||
|
||
Where applicable, a UTF-8 encoding is presumed. Be advised that | ||
some interactions can raise :exc:`UnicodeEncodeErrors` and | ||
|
@@ -1763,8 +1775,18 @@ class DecodedURL(object): | |
lazy (bool): Set to True to avoid pre-decode all parts of the URL to | ||
check for validity. Defaults to False. | ||
|
||
.. note:: | ||
|
||
The :class:`DecodedURL` initializer takes a :class:`URL` object, | ||
not URL components, like :class:`URL`. To programmatically | ||
construct a :class:`DecodedURL`, you can use this pattern: | ||
|
||
>>> DecodedURL().replace(host='pypi.org', path=('projects', 'hyperlink').to_text() | ||
"http://pypi.org/projects/hyperlink" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any opinion on adding a |
||
|
||
|
||
""" | ||
def __init__(self, url, lazy=False): | ||
def __init__(self, url=_EMPTY_URL, lazy=False): | ||
mahmoud marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why the default? Perhaps merits discussion outside of doc changes. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure, a couple reasons:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, and the reason for including it in these changes was because I really wanted to avoid people programmatically constructing the newly-exposed DecodedURL by doing
Without realizing that URL's initializer arguments aren't as rigorously decoded/encoded as the rest of DecodedURL's API. I proposed adding a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK makes sense, and yes to |
||
# type: (URL, bool) -> None | ||
self._url = url | ||
if not lazy: | ||
|
@@ -2098,7 +2120,7 @@ def parse(url, decoded=True, lazy=False): | |
decoded (bool): Whether or not to return a :class:`DecodedURL`, | ||
which automatically handles all | ||
encoding/decoding/quoting/unquoting for all the various | ||
accessors of parts of the URL, or an :class:`EncodedURL`, | ||
accessors of parts of the URL, or a :class:`URL`, | ||
which has the same API, but requires handling of special | ||
characters for different parts of the URL. | ||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.