@@ -4,7 +4,8 @@ Python Sorted Containers
4
4
.. todo ::
5
5
6
6
* Update history and document v3 milestone
7
- * Update landing page
7
+ * Review implementation page
8
+ * Review development page
8
9
* Re-run performance benchmarks
9
10
* Rename github repo
10
11
* Tell Doug Hellmann about Sorted Containers and relation to bisect module
@@ -14,32 +15,44 @@ written in pure-Python, and fast as C-extensions.
14
15
15
16
Python's standard library is great until you need a sorted collections
16
17
type. Many will attest that you can get really far without one, but the moment
17
- you **really need ** a sorted list, dict, or set, you're faced with a dozen
18
- different implementations, most using C-extensions without great documentation
19
- and benchmarking.
18
+ you **really need ** a sorted list, sorted dict, or sorted set, you're faced
19
+ with a dozen different implementations, most using C-extensions without great
20
+ documentation and benchmarking.
20
21
21
22
In Python, we can do better. And we can do it in pure-Python!
22
23
23
24
.. code-block :: python
24
25
25
- >> > sl = sortedcontainers.SortedList(xrange (10000000 ))
26
- >> > 1234567 in sl
27
- True
28
- >> > sl[7654321 ]
29
- 7654321
30
- >> > sl.add(1234567 )
31
- >> > sl.count(1234567 )
26
+ >> > from sortedcontainers import SortedList
27
+ >> > sl = SortedList([' e' , ' a' , ' c' , ' d' , ' b' ])
28
+ >> > sl
29
+ SortedList([' a' , ' b' , ' c' , ' d' , ' e' ])
30
+ >> > sl *= 10_000_000
31
+ >> > sl.count(' c' )
32
+ 10000000
33
+ >> > sl[- 3 :]
34
+ [' e' , ' e' , ' e' ]
35
+ >> > from sortedcontainers import SortedDict
36
+ >> > sd = SortedDict({' c' : 3 , ' a' : 1 , ' b' : 2 })
37
+ >> > sd
38
+ SortedDict({' a' : 1 , ' b' : 2 , ' c' : 3 })
39
+ >> > sd.popitem(index = - 1 )
40
+ (' c' , 3 )
41
+ >> > from sortedcontainers import SortedSet
42
+ >> > ss = SortedSet(' abracadabra' )
43
+ >> > ss
44
+ SortedSet([' a' , ' b' , ' c' , ' d' , ' r' ])
45
+ >> > ss.bisect_left(' c' )
32
46
2
33
- >> > sl *= 3
34
- >> > len (sl)
35
- 30000003
36
47
37
- **Note: ** don't try this without at least a half gigabyte of memory. In Python
38
- an integer requires about 24 bytes. SortedList will add about 8 bytes per
39
- object stored in the container. That's pretty hard to beat as it's the cost of
40
- a pointer to each object. It's also 66% less overhead than a typical binary
41
- tree implementation (e.g. red-black tree, avl tree, aa tree, splay tree, treap,
42
- etc.) for which every node must also store two pointers to children nodes.
48
+ All of the operations shown above run in faster than linear time. The above
49
+ demo also take nearly a gigabyte of memory to run. When the sorted list is
50
+ multiplied by ten million, it stores ten million references to each of "a"
51
+ through "e". Each reference requires eight bytes in the sorted
52
+ container. That's pretty hard to beat as it's the cost of a pointer to each
53
+ object. It's also 66% less overhead than a typical binary tree implementation
54
+ (e.g. Red-Black Tree, AVL-Tree, AA-Tree, Splay-Tree, Treap, etc.) for which
55
+ every node must also store two pointers to children nodes.
43
56
44
57
`Sorted Containers `_ takes all of the work out of Python sorted collections -
45
58
making your deployment and use of Python easy. There's no need to install a C
@@ -52,41 +65,48 @@ feature and testing has 100% coverage with unit tests and hours of stress.
52
65
Testimonials
53
66
------------
54
67
55
- **Alex Martelli **, `Wikipedia `_
68
+ **Alex Martelli **, `Fellow of the Python Software Foundation `_ --
56
69
57
- Good stuff! ... I like the `simple, effective implementation `_ idea of splitting
58
- the sorted containers into smaller "fragments" to avoid the O(N) insertion costs.
70
+ "Good stuff! ... I like the `simple, effective implementation `_ idea of
71
+ splitting the sorted containers into smaller "fragments" to avoid the O(N)
72
+ insertion costs."
59
73
60
- .. _`Wikipedia` : http://en.wikipedia.org/wiki/Alex_Martelli
61
- .. _`simple, effective implementation` : http://www.grantjenks.com/docs/sortedcontainers/implementation.html
62
-
63
- **Jeff Knupp **, `Review of Sorted Containers `_
74
+ **Jeff Knupp **, `author of Writing Idiomatic Python and Python Trainer `_ --
64
75
65
- That last part, "fast as C-extensions," was difficult to believe. I would need
76
+ " That last part, "fast as C-extensions," was difficult to believe. I would need
66
77
some sort of `Performance Comparison `_ to be convinced this is true. The author
67
- includes this in the docs. It is.
68
-
69
- .. _`Review of Sorted Containers` : http://reviews.jeffknupp.com/reviews/sortedcontainers/3/
78
+ includes this in the docs. It is."
70
79
71
- **Kevin Samuel **, `Formations Python`_
80
+ **Kevin Samuel **, `Python and Django Trainer `_ --
72
81
73
- I'm quite amazed, not just by the code quality (it's incredibly
74
- readable and has more comment than code, wow), but the actual
75
- amount of work you put at stuff that is *not * code:
76
- documentation, benchmarking, implementation explanations. Even
77
- the git log is clean and the unit tests run out of the box on
78
- Python 2 and 3.
82
+ I'm quite amazed, not just by the code quality (it's incredibly readable and
83
+ has more comment than code, wow), but the actual amount of work you put at
84
+ stuff that is *not * code: documentation, benchmarking, implementation
85
+ explanations. Even the git log is clean and the unit tests run out of the box
86
+ on Python 2 and 3.
79
87
80
- .. _`Formations Python` : http://formationspython.com/
81
-
82
- **Mark Summerfield **, a short plea for `Python Sorted Collections `_
88
+ **Mark Summerfield **, a short plea for `Python Sorted Collections `_ --
83
89
84
90
Python's "batteries included" standard library seems to have a battery
85
91
missing. And the argument that "we never had it before" has worn thin. It is
86
92
time that Python offered a full range of collection classes out of the box,
87
93
including sorted ones.
88
94
95
+ `Sorted Containers `_ is used in popular open source projects such as:
96
+ `Zipline `_, an algorithmic trading library from Quantopian; `Angr `_, a binary
97
+ analysis platform from UC Santa Barbara; `Trio `_, an async I/O library; and
98
+ `Dask Distributed `_, a distributed computation library supported by Continuum
99
+ Analytics.
100
+
101
+ .. _`Fellow of the Python Software Foundation` : http://en.wikipedia.org/wiki/Alex_Martelli
102
+ .. _`simple, effective implementation` : http://www.grantjenks.com/docs/sortedcontainers/implementation.html
103
+ .. _`author of Writing Idiomatic Python and Python Trainer` : http://reviews.jeffknupp.com/reviews/sortedcontainers/3/
104
+ .. _`Python and Django Trainer` : https://www.elephorm.com/formateur/kevin-samuel
89
105
.. _`Python Sorted Collections` : http://www.qtrac.eu/pysorted.html
106
+ .. _`Zipline` : https://github.com/quantopian/zipline
107
+ .. _`Angr` : https://github.com/angr/angr
108
+ .. _`Trio` : https://github.com/python-trio/trio
109
+ .. _`Dask Distributed` : https://github.com/dask/distributed
90
110
91
111
Features
92
112
--------
@@ -117,15 +137,17 @@ Installing `Sorted Containers`_ is simple with `pip
117
137
118
138
$ pip install sortedcontainers
119
139
120
- You can access documentation in the interpreter with Python's built-in help
121
- function:
140
+ You can access documentation in the interpreter with Python's built-in `help `
141
+ function. The `help ` works on modules, classes and methods in `Sorted
142
+ Containers `_::
122
143
123
144
.. code-block :: python
124
145
125
- >> > from sortedcontainers import SortedList, SortedDict, SortedSet
126
- >> > help (SortedList)
146
+ >> > import sortedcontainers
147
+ >> > help (sortedcontainers)
148
+ >> > from sortedcontainers import SortedDict
127
149
>> > help (SortedDict)
128
- >> > help (SortedSet )
150
+ >> > help (SortedDict.popitem )
129
151
130
152
Documentation
131
153
-------------
0 commit comments