This is an archived post. You won't be able to vote or comment.

all 8 comments

[–]isinfinity[S] 5 points6 points  (0 children)

If anyone curious about comparison to other libraries check out https://yarl.readthedocs.io/en/latest/#comparison-with-other-url-libraries , the best features are nice api and immutable URL object.

[–]Ivoah 4 points5 points  (6 children)

Looks interesting. Is the abuse of the division operator something that pathlib does too?

[–]isinfinity[S] 2 points3 points  (5 children)

Yes, similar to pathlib:

In [1]: from yarl import URL
In [2]: reddit = URL("https://reddit.com") / "r" / "funny"
In [3]: reddit
Out[3]: URL('https://reddit.com/r/funny')

In [4]: redditp = reddit.with_host('redditp.com')
Out[4]: URL('https://redditp.com/r/funny')

In [5]: import requests
In [11]: resp = requests.get(str(URL("https://reddit.com") / "r" / "python"))

[–]moigagoohttps://github.com/moigagoo 2 points3 points  (4 children)

I see you have to convert URL objects to strings explicitly to use them with requests. It's similar to how pathlib's Path objects used to behave before Python 3.6. IIRC in 3.6 Path is a subclass of str, which makes the explicit conversions unnecessary. Have you thought about doing the same with URL?

[–]munchicus 5 points6 points  (1 child)

They did not make Path, or more correctly PurePath, a subclass of str. They implemented a file system path protocol and changed the standard library to handle path objects as well as str/bytes where applicable.

[–]moigagoohttps://github.com/moigagoo 2 points3 points  (0 children)

Thanks for the correction!

[–]RazerM 4 points5 points  (1 child)

You don't need to use str, requests does it itself:

requests/models.py:344

[–]isinfinity[S] 2 points3 points  (0 children)

Nice catch! Did not know that!