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