README.rst 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. frozenlist
  2. ==========
  3. .. image:: https://github.com/aio-libs/frozenlist/workflows/CI/badge.svg
  4. :target: https://github.com/aio-libs/frozenlist/actions
  5. :alt: GitHub status for master branch
  6. .. image:: https://codecov.io/gh/aio-libs/frozenlist/branch/master/graph/badge.svg
  7. :target: https://codecov.io/gh/aio-libs/frozenlist
  8. :alt: codecov.io status for master branch
  9. .. image:: https://img.shields.io/pypi/v/frozenlist.svg?logo=Python&logoColor=white
  10. :target: https://pypi.org/project/frozenlist
  11. :alt: frozenlist @ PyPI
  12. .. image:: https://readthedocs.org/projects/frozenlist/badge/?version=latest
  13. :target: https://frozenlist.aio-libs.org
  14. :alt: Read The Docs build status badge
  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. ``frozenlist.FrozenList`` is a list-like structure which implements
  24. ``collections.abc.MutableSequence``. The list is *mutable* until ``FrozenList.freeze``
  25. is called, after which list modifications raise ``RuntimeError``:
  26. >>> from frozenlist import FrozenList
  27. >>> fl = FrozenList([17, 42])
  28. >>> fl.append('spam')
  29. >>> fl.append('Vikings')
  30. >>> fl
  31. <FrozenList(frozen=False, [17, 42, 'spam', 'Vikings'])>
  32. >>> fl.freeze()
  33. >>> fl
  34. <FrozenList(frozen=True, [17, 42, 'spam', 'Vikings'])>
  35. >>> fl.frozen
  36. True
  37. >>> fl.append("Monty")
  38. Traceback (most recent call last):
  39. File "<stdin>", line 1, in <module>
  40. File "frozenlist/_frozenlist.pyx", line 97, in frozenlist._frozenlist.FrozenList.append
  41. self._check_frozen()
  42. File "frozenlist/_frozenlist.pyx", line 19, in frozenlist._frozenlist.FrozenList._check_frozen
  43. raise RuntimeError("Cannot modify frozen list.")
  44. RuntimeError: Cannot modify frozen list.
  45. FrozenList is also hashable, but only when frozen. Otherwise it also throws a RuntimeError:
  46. >>> fl = FrozenList([17, 42, 'spam'])
  47. >>> hash(fl)
  48. Traceback (most recent call last):
  49. File "<stdin>", line 1, in <module>
  50. File "frozenlist/_frozenlist.pyx", line 111, in frozenlist._frozenlist.FrozenList.__hash__
  51. raise RuntimeError("Cannot hash unfrozen list.")
  52. RuntimeError: Cannot hash unfrozen list.
  53. >>> fl.freeze()
  54. >>> hash(fl)
  55. 3713081631934410656
  56. >>> dictionary = {fl: 'Vikings'} # frozen fl can be a dict key
  57. >>> dictionary
  58. {<FrozenList(frozen=True, [1, 2])>: 'Vikings'}
  59. Installation
  60. ------------
  61. ::
  62. $ pip install frozenlist
  63. The library requires Python 3.8 or newer.
  64. Documentation
  65. -------------
  66. https://frozenlist.aio-libs.org
  67. Communication channels
  68. ----------------------
  69. We have a *Matrix Space* `#aio-libs-space:matrix.org
  70. <https://matrix.to/#/%23aio-libs-space:matrix.org>`_ which is
  71. also accessible via Gitter.
  72. Requirements
  73. ------------
  74. - Python >= 3.8
  75. License
  76. -------
  77. ``frozenlist`` is offered under the Apache 2 license.
  78. Source code
  79. -----------
  80. The project is hosted on GitHub_
  81. Please file an issue in the `bug tracker
  82. <https://github.com/aio-libs/frozenlist/issues>`_ if you have found a bug
  83. or have some suggestions to improve the library.
  84. .. _GitHub: https://github.com/aio-libs/frozenlist