README.rst 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. Python Sorted Containers
  2. ========================
  3. `Sorted Containers`_ is an Apache2 licensed `sorted collections library`_,
  4. written in pure-Python, and fast as C-extensions.
  5. Python's standard library is great until you need a sorted collections
  6. type. Many will attest that you can get really far without one, but the moment
  7. you **really need** a sorted list, sorted dict, or sorted set, you're faced
  8. with a dozen different implementations, most using C-extensions without great
  9. documentation and benchmarking.
  10. In Python, we can do better. And we can do it in pure-Python!
  11. .. code-block:: python
  12. >>> from sortedcontainers import SortedList
  13. >>> sl = SortedList(['e', 'a', 'c', 'd', 'b'])
  14. >>> sl
  15. SortedList(['a', 'b', 'c', 'd', 'e'])
  16. >>> sl *= 10_000_000
  17. >>> sl.count('c')
  18. 10000000
  19. >>> sl[-3:]
  20. ['e', 'e', 'e']
  21. >>> from sortedcontainers import SortedDict
  22. >>> sd = SortedDict({'c': 3, 'a': 1, 'b': 2})
  23. >>> sd
  24. SortedDict({'a': 1, 'b': 2, 'c': 3})
  25. >>> sd.popitem(index=-1)
  26. ('c', 3)
  27. >>> from sortedcontainers import SortedSet
  28. >>> ss = SortedSet('abracadabra')
  29. >>> ss
  30. SortedSet(['a', 'b', 'c', 'd', 'r'])
  31. >>> ss.bisect_left('c')
  32. 2
  33. All of the operations shown above run in faster than linear time. The above
  34. demo also takes nearly a gigabyte of memory to run. When the sorted list is
  35. multiplied by ten million, it stores ten million references to each of "a"
  36. through "e". Each reference requires eight bytes in the sorted
  37. container. That's pretty hard to beat as it's the cost of a pointer to each
  38. object. It's also 66% less overhead than a typical binary tree implementation
  39. (e.g. Red-Black Tree, AVL-Tree, AA-Tree, Splay-Tree, Treap, etc.) for which
  40. every node must also store two pointers to children nodes.
  41. `Sorted Containers`_ takes all of the work out of Python sorted collections -
  42. making your deployment and use of Python easy. There's no need to install a C
  43. compiler or pre-build and distribute custom extensions. Performance is a
  44. feature and testing has 100% coverage with unit tests and hours of stress.
  45. .. _`Sorted Containers`: http://www.grantjenks.com/docs/sortedcontainers/
  46. .. _`sorted collections library`: http://www.grantjenks.com/docs/sortedcontainers/
  47. Testimonials
  48. ------------
  49. **Alex Martelli**, `Fellow of the Python Software Foundation`_
  50. "Good stuff! ... I like the `simple, effective implementation`_ idea of
  51. splitting the sorted containers into smaller "fragments" to avoid the O(N)
  52. insertion costs."
  53. **Jeff Knupp**, `author of Writing Idiomatic Python and Python Trainer`_
  54. "That last part, "fast as C-extensions," was difficult to believe. I would need
  55. some sort of `Performance Comparison`_ to be convinced this is true. The author
  56. includes this in the docs. It is."
  57. **Kevin Samuel**, `Python and Django Trainer`_
  58. I'm quite amazed, not just by the code quality (it's incredibly readable and
  59. has more comment than code, wow), but the actual amount of work you put at
  60. stuff that is *not* code: documentation, benchmarking, implementation
  61. explanations. Even the git log is clean and the unit tests run out of the box
  62. on Python 2 and 3.
  63. **Mark Summerfield**, a short plea for `Python Sorted Collections`_
  64. Python's "batteries included" standard library seems to have a battery
  65. missing. And the argument that "we never had it before" has worn thin. It is
  66. time that Python offered a full range of collection classes out of the box,
  67. including sorted ones.
  68. `Sorted Containers`_ is used in popular open source projects such as:
  69. `Zipline`_, an algorithmic trading library from Quantopian; `Angr`_, a binary
  70. analysis platform from UC Santa Barbara; `Trio`_, an async I/O library; and
  71. `Dask Distributed`_, a distributed computation library supported by Continuum
  72. Analytics.
  73. .. _`Fellow of the Python Software Foundation`: https://en.wikipedia.org/wiki/Alex_Martelli
  74. .. _`simple, effective implementation`: http://www.grantjenks.com/docs/sortedcontainers/implementation.html
  75. .. _`author of Writing Idiomatic Python and Python Trainer`: https://jeffknupp.com/
  76. .. _`Python and Django Trainer`: https://www.elephorm.com/formateur/kevin-samuel
  77. .. _`Python Sorted Collections`: http://www.qtrac.eu/pysorted.html
  78. .. _`Zipline`: https://github.com/quantopian/zipline
  79. .. _`Angr`: https://github.com/angr/angr
  80. .. _`Trio`: https://github.com/python-trio/trio
  81. .. _`Dask Distributed`: https://github.com/dask/distributed
  82. Features
  83. --------
  84. - Pure-Python
  85. - Fully documented
  86. - Benchmark comparison (alternatives, runtimes, load-factors)
  87. - 100% test coverage
  88. - Hours of stress testing
  89. - Performance matters (often faster than C implementations)
  90. - Compatible API (nearly identical to older blist and bintrees modules)
  91. - Feature-rich (e.g. get the five largest keys in a sorted dict: d.keys()[-5:])
  92. - Pragmatic design (e.g. SortedSet is a Python set with a SortedList index)
  93. - Developed on Python 3.7
  94. - Tested on CPython 2.7, 3.2, 3.3, 3.4, 3.5, 3.6, 3.7 and PyPy, PyPy3
  95. .. image:: https://api.travis-ci.org/grantjenks/python-sortedcontainers.svg?branch=master
  96. :target: http://www.grantjenks.com/docs/sortedcontainers/
  97. .. image:: https://ci.appveyor.com/api/projects/status/github/grantjenks/python-sortedcontainers?branch=master&svg=true
  98. :target: http://www.grantjenks.com/docs/sortedcontainers/
  99. Quickstart
  100. ----------
  101. Installing `Sorted Containers`_ is simple with `pip
  102. <https://pypi.org/project/pip/>`_::
  103. $ pip install sortedcontainers
  104. You can access documentation in the interpreter with Python's built-in `help`
  105. function. The `help` works on modules, classes and methods in `Sorted
  106. Containers`_.
  107. .. code-block:: python
  108. >>> import sortedcontainers
  109. >>> help(sortedcontainers)
  110. >>> from sortedcontainers import SortedDict
  111. >>> help(SortedDict)
  112. >>> help(SortedDict.popitem)
  113. Documentation
  114. -------------
  115. Complete documentation for `Sorted Containers`_ is available at
  116. http://www.grantjenks.com/docs/sortedcontainers/
  117. User Guide
  118. ..........
  119. The user guide provides an introduction to `Sorted Containers`_ and extensive
  120. performance comparisons and analysis.
  121. - `Introduction`_
  122. - `Performance Comparison`_
  123. - `Load Factor Performance Comparison`_
  124. - `Runtime Performance Comparison`_
  125. - `Simulated Workload Performance Comparison`_
  126. - `Performance at Scale`_
  127. .. _`Introduction`: http://www.grantjenks.com/docs/sortedcontainers/introduction.html
  128. .. _`Performance Comparison`: http://www.grantjenks.com/docs/sortedcontainers/performance.html
  129. .. _`Load Factor Performance Comparison`: http://www.grantjenks.com/docs/sortedcontainers/performance-load.html
  130. .. _`Runtime Performance Comparison`: http://www.grantjenks.com/docs/sortedcontainers/performance-runtime.html
  131. .. _`Simulated Workload Performance Comparison`: http://www.grantjenks.com/docs/sortedcontainers/performance-workload.html
  132. .. _`Performance at Scale`: http://www.grantjenks.com/docs/sortedcontainers/performance-scale.html
  133. Community Guide
  134. ...............
  135. The community guide provides information on the development of `Sorted
  136. Containers`_ along with support, implementation, and history details.
  137. - `Development and Support`_
  138. - `Implementation Details`_
  139. - `Release History`_
  140. .. _`Development and Support`: http://www.grantjenks.com/docs/sortedcontainers/development.html
  141. .. _`Implementation Details`: http://www.grantjenks.com/docs/sortedcontainers/implementation.html
  142. .. _`Release History`: http://www.grantjenks.com/docs/sortedcontainers/history.html
  143. API Documentation
  144. .................
  145. The API documentation provides information on specific functions, classes, and
  146. modules in the `Sorted Containers`_ package.
  147. - `Sorted List`_
  148. - `Sorted Dict`_
  149. - `Sorted Set`_
  150. .. _`Sorted List`: http://www.grantjenks.com/docs/sortedcontainers/sortedlist.html
  151. .. _`Sorted Dict`: http://www.grantjenks.com/docs/sortedcontainers/sorteddict.html
  152. .. _`Sorted Set`: http://www.grantjenks.com/docs/sortedcontainers/sortedset.html
  153. Talks
  154. -----
  155. - `Python Sorted Collections | PyCon 2016 Talk`_
  156. - `SF Python Holiday Party 2015 Lightning Talk`_
  157. - `DjangoCon 2015 Lightning Talk`_
  158. .. _`Python Sorted Collections | PyCon 2016 Talk`: http://www.grantjenks.com/docs/sortedcontainers/pycon-2016-talk.html
  159. .. _`SF Python Holiday Party 2015 Lightning Talk`: http://www.grantjenks.com/docs/sortedcontainers/sf-python-2015-lightning-talk.html
  160. .. _`DjangoCon 2015 Lightning Talk`: http://www.grantjenks.com/docs/sortedcontainers/djangocon-2015-lightning-talk.html
  161. Resources
  162. ---------
  163. - `Sorted Containers Documentation`_
  164. - `Sorted Containers at PyPI`_
  165. - `Sorted Containers at Github`_
  166. - `Sorted Containers Issue Tracker`_
  167. .. _`Sorted Containers Documentation`: http://www.grantjenks.com/docs/sortedcontainers/
  168. .. _`Sorted Containers at PyPI`: https://pypi.org/project/sortedcontainers/
  169. .. _`Sorted Containers at Github`: https://github.com/grantjenks/python-sortedcontainers
  170. .. _`Sorted Containers Issue Tracker`: https://github.com/grantjenks/python-sortedcontainers/issues
  171. Sorted Containers License
  172. -------------------------
  173. Copyright 2014-2019 Grant Jenks
  174. Licensed under the Apache License, Version 2.0 (the "License");
  175. you may not use this file except in compliance with the License.
  176. You may obtain a copy of the License at
  177. http://www.apache.org/licenses/LICENSE-2.0
  178. Unless required by applicable law or agreed to in writing, software
  179. distributed under the License is distributed on an "AS IS" BASIS,
  180. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  181. See the License for the specific language governing permissions and
  182. limitations under the License.