Debian Codegraph @

README.rst

raw ◀ ▶
1 Debian patch touch this file
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 Quickstart
118 ----------
119
120 Installing `Sorted Containers`_ is simple with `pip
121 <https://pypi.org/project/pip/>`_::
122
123 $ pip install sortedcontainers
124
125 You can access documentation in the interpreter with Python's built-in `help`
126 function. The `help` works on modules, classes and methods in `Sorted
127 Containers`_.
128
129 .. code-block:: python
130
131 >>> import sortedcontainers
132 >>> help(sortedcontainers)
133 >>> from sortedcontainers import SortedDict
134 >>> help(SortedDict)
135 >>> help(SortedDict.popitem)
136
137 Documentation
138 -------------
139
140 Complete documentation for `Sorted Containers`_ is available at
141 http://www.grantjenks.com/docs/sortedcontainers/
142
143 User Guide
144 ..........
145
146 The user guide provides an introduction to `Sorted Containers`_ and extensive
147 performance comparisons and analysis.
148
149 - `Introduction`_
150 - `Performance Comparison`_
151 - `Load Factor Performance Comparison`_
152 - `Runtime Performance Comparison`_
153 - `Simulated Workload Performance Comparison`_
154 - `Performance at Scale`_
155
156 .. _`Introduction`: http://www.grantjenks.com/docs/sortedcontainers/introduction.html
157 .. _`Performance Comparison`: http://www.grantjenks.com/docs/sortedcontainers/performance.html
158 .. _`Load Factor Performance Comparison`: http://www.grantjenks.com/docs/sortedcontainers/performance-load.html
159 .. _`Runtime Performance Comparison`: http://www.grantjenks.com/docs/sortedcontainers/performance-runtime.html
160 .. _`Simulated Workload Performance Comparison`: http://www.grantjenks.com/docs/sortedcontainers/performance-workload.html
161 .. _`Performance at Scale`: http://www.grantjenks.com/docs/sortedcontainers/performance-scale.html
162
163 Community Guide
164 ...............
165
166 The community guide provides information on the development of `Sorted
167 Containers`_ along with support, implementation, and history details.
168
169 - `Development and Support`_
170 - `Implementation Details`_
171 - `Release History`_
172
173 .. _`Development and Support`: http://www.grantjenks.com/docs/sortedcontainers/development.html
174 .. _`Implementation Details`: http://www.grantjenks.com/docs/sortedcontainers/implementation.html
175 .. _`Release History`: http://www.grantjenks.com/docs/sortedcontainers/history.html
176
177 API Documentation
178 .................
179
180 The API documentation provides information on specific functions, classes, and
181 modules in the `Sorted Containers`_ package.
182
183 - `Sorted List`_
184 - `Sorted Dict`_
185 - `Sorted Set`_
186
187 .. _`Sorted List`: http://www.grantjenks.com/docs/sortedcontainers/sortedlist.html
188 .. _`Sorted Dict`: http://www.grantjenks.com/docs/sortedcontainers/sorteddict.html
189 .. _`Sorted Set`: http://www.grantjenks.com/docs/sortedcontainers/sortedset.html
190
191 Talks
192 -----
193
194 - `Python Sorted Collections | PyCon 2016 Talk`_
195 - `SF Python Holiday Party 2015 Lightning Talk`_
196 - `DjangoCon 2015 Lightning Talk`_
197
198 .. _`Python Sorted Collections | PyCon 2016 Talk`: http://www.grantjenks.com/docs/sortedcontainers/pycon-2016-talk.html
199 .. _`SF Python Holiday Party 2015 Lightning Talk`: http://www.grantjenks.com/docs/sortedcontainers/sf-python-2015-lightning-talk.html
200 .. _`DjangoCon 2015 Lightning Talk`: http://www.grantjenks.com/docs/sortedcontainers/djangocon-2015-lightning-talk.html
201
202 Resources
203 ---------
204
205 - `Sorted Containers Documentation`_
206 - `Sorted Containers at PyPI`_
207 - `Sorted Containers at Github`_
208 - `Sorted Containers Issue Tracker`_
209
210 .. _`Sorted Containers Documentation`: http://www.grantjenks.com/docs/sortedcontainers/
211 .. _`Sorted Containers at PyPI`: https://pypi.org/project/sortedcontainers/
212 .. _`Sorted Containers at Github`: https://github.com/grantjenks/python-sortedcontainers
213 .. _`Sorted Containers Issue Tracker`: https://github.com/grantjenks/python-sortedcontainers/issues
214
215 Sorted Containers License
216 -------------------------
217
218 Copyright 2014-2019 Grant Jenks
219
220 Licensed under the Apache License, Version 2.0 (the "License");
221 you may not use this file except in compliance with the License.
222 You may obtain a copy of the License at
223
224 http://www.apache.org/licenses/LICENSE-2.0
225
226 Unless required by applicable law or agreed to in writing, software
227 distributed under the License is distributed on an "AS IS" BASIS,
228 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
229 See the License for the specific language governing permissions and
230 limitations under the License.