README.rst 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. yarl
  2. ====
  3. The module provides handy URL class for URL parsing and changing.
  4. .. image:: https://github.com/aio-libs/yarl/workflows/CI/badge.svg
  5. :target: https://github.com/aio-libs/yarl/actions?query=workflow%3ACI
  6. :align: right
  7. .. image:: https://codecov.io/gh/aio-libs/yarl/branch/master/graph/badge.svg
  8. :target: https://codecov.io/gh/aio-libs/yarl
  9. .. image:: https://badge.fury.io/py/yarl.svg
  10. :target: https://badge.fury.io/py/yarl
  11. .. image:: https://readthedocs.org/projects/yarl/badge/?version=latest
  12. :target: https://yarl.aio-libs.org
  13. .. image:: https://img.shields.io/pypi/pyversions/yarl.svg
  14. :target: https://pypi.python.org/pypi/yarl
  15. .. image:: https://img.shields.io/matrix/aio-libs:matrix.org?label=Discuss%20on%20Matrix%20at%20%23aio-libs%3Amatrix.org&logo=matrix&server_fqdn=matrix.org&style=flat
  16. :target: https://matrix.to/#/%23aio-libs:matrix.org
  17. :alt: Matrix Room — #aio-libs:matrix.org
  18. .. image:: https://img.shields.io/matrix/aio-libs-space:matrix.org?label=Discuss%20on%20Matrix%20at%20%23aio-libs-space%3Amatrix.org&logo=matrix&server_fqdn=matrix.org&style=flat
  19. :target: https://matrix.to/#/%23aio-libs-space:matrix.org
  20. :alt: Matrix Space — #aio-libs-space:matrix.org
  21. Introduction
  22. ------------
  23. Url is constructed from ``str``:
  24. .. code-block:: pycon
  25. >>> from yarl import URL
  26. >>> url = URL('https://www.python.org/~guido?arg=1#frag')
  27. >>> url
  28. URL('https://www.python.org/~guido?arg=1#frag')
  29. All url parts: *scheme*, *user*, *password*, *host*, *port*, *path*,
  30. *query* and *fragment* are accessible by properties:
  31. .. code-block:: pycon
  32. >>> url.scheme
  33. 'https'
  34. >>> url.host
  35. 'www.python.org'
  36. >>> url.path
  37. '/~guido'
  38. >>> url.query_string
  39. 'arg=1'
  40. >>> url.query
  41. <MultiDictProxy('arg': '1')>
  42. >>> url.fragment
  43. 'frag'
  44. All url manipulations produce a new url object:
  45. .. code-block:: pycon
  46. >>> url = URL('https://www.python.org')
  47. >>> url / 'foo' / 'bar'
  48. URL('https://www.python.org/foo/bar')
  49. >>> url / 'foo' % {'bar': 'baz'}
  50. URL('https://www.python.org/foo?bar=baz')
  51. Strings passed to constructor and modification methods are
  52. automatically encoded giving canonical representation as result:
  53. .. code-block:: pycon
  54. >>> url = URL('https://www.python.org/шлях')
  55. >>> url
  56. URL('https://www.python.org/%D1%88%D0%BB%D1%8F%D1%85')
  57. Regular properties are *percent-decoded*, use ``raw_`` versions for
  58. getting *encoded* strings:
  59. .. code-block:: pycon
  60. >>> url.path
  61. '/шлях'
  62. >>> url.raw_path
  63. '/%D1%88%D0%BB%D1%8F%D1%85'
  64. Human readable representation of URL is available as ``.human_repr()``:
  65. .. code-block:: pycon
  66. >>> url.human_repr()
  67. 'https://www.python.org/шлях'
  68. For full documentation please read https://yarl.aio-libs.org.
  69. Installation
  70. ------------
  71. ::
  72. $ pip install yarl
  73. The library is Python 3 only!
  74. PyPI contains binary wheels for Linux, Windows and MacOS. If you want to install
  75. ``yarl`` on another operating system (like *Alpine Linux*, which is not
  76. manylinux-compliant because of the missing glibc and therefore, cannot be
  77. used with our wheels) the the tarball will be used to compile the library from
  78. the source code. It requires a C compiler and and Python headers installed.
  79. To skip the compilation you must explicitly opt-in by using a PEP 517
  80. configuration setting ``pure-python``, or setting the ``YARL_NO_EXTENSIONS``
  81. environment variable to a non-empty value, e.g.:
  82. .. code-block:: console
  83. $ pip install yarl --config-settings=pure-python=false
  84. Please note that the pure-Python (uncompiled) version is much slower. However,
  85. PyPy always uses a pure-Python implementation, and, as such, it is unaffected
  86. by this variable.
  87. Dependencies
  88. ------------
  89. YARL requires multidict_ library.
  90. API documentation
  91. ------------------
  92. The documentation is located at https://yarl.aio-libs.org.
  93. Why isn't boolean supported by the URL query API?
  94. -------------------------------------------------
  95. There is no standard for boolean representation of boolean values.
  96. Some systems prefer ``true``/``false``, others like ``yes``/``no``, ``on``/``off``,
  97. ``Y``/``N``, ``1``/``0``, etc.
  98. ``yarl`` cannot make an unambiguous decision on how to serialize ``bool`` values because
  99. it is specific to how the end-user's application is built and would be different for
  100. different apps. The library doesn't accept booleans in the API; a user should convert
  101. bools into strings using own preferred translation protocol.
  102. Comparison with other URL libraries
  103. ------------------------------------
  104. * furl (https://pypi.python.org/pypi/furl)
  105. The library has rich functionality but the ``furl`` object is mutable.
  106. I'm afraid to pass this object into foreign code: who knows if the
  107. code will modify my url in a terrible way while I just want to send URL
  108. with handy helpers for accessing URL properties.
  109. ``furl`` has other non-obvious tricky things but the main objection
  110. is mutability.
  111. * URLObject (https://pypi.python.org/pypi/URLObject)
  112. URLObject is immutable, that's pretty good.
  113. Every URL change generates a new URL object.
  114. But the library doesn't do any decode/encode transformations leaving the
  115. end user to cope with these gory details.
  116. Source code
  117. -----------
  118. The project is hosted on GitHub_
  119. Please file an issue on the `bug tracker
  120. <https://github.com/aio-libs/yarl/issues>`_ if you have found a bug
  121. or have some suggestion in order to improve the library.
  122. The library uses `Azure Pipelines <https://dev.azure.com/aio-libs/yarl>`_ for
  123. Continuous Integration.
  124. Discussion list
  125. ---------------
  126. *aio-libs* google group: https://groups.google.com/forum/#!forum/aio-libs
  127. Feel free to post your questions and ideas here.
  128. Authors and License
  129. -------------------
  130. The ``yarl`` package is written by Andrew Svetlov.
  131. It's *Apache 2* licensed and freely available.
  132. .. _GitHub: https://github.com/aio-libs/yarl
  133. .. _multidict: https://github.com/aio-libs/multidict