annotate Lib/collections.py @ 78069:a8aa77458e12 3.2

Add c_size_t and c_ssize_t to the ctypes summary table.
author Antoine Pitrou <solipsis@pitrou.net>
date Thu, 12 Jul 2012 20:31:50 +0200
parents f7283825effa
children 31eaf8a137ea
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
46927
f0a285fc201d The __all__ value for 'collections' was missing UserList and UserString.
Brett Cannon <bcannon@gmail.com>
parents: 45424
diff changeset
1 __all__ = ['deque', 'defaultdict', 'namedtuple', 'UserDict', 'UserList',
51946
c910f6e3bb28 PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents: 51503
diff changeset
2 'UserString', 'Counter', 'OrderedDict']
42084
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41824
diff changeset
3 # For bootstrapping reasons, the collection ABCs are defined in _abcoll.py.
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41824
diff changeset
4 # They should however be considered an integral part of collections.py.
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41824
diff changeset
5 from _abcoll import *
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41824
diff changeset
6 import _abcoll
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41824
diff changeset
7 __all__ += _abcoll.__all__
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41824
diff changeset
8
44349
4dc01402d78a Merged revisions 59541-59561 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44310
diff changeset
9 from _collections import deque, defaultdict
4dc01402d78a Merged revisions 59541-59561 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44310
diff changeset
10 from operator import itemgetter as _itemgetter
4dc01402d78a Merged revisions 59541-59561 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44310
diff changeset
11 from keyword import iskeyword as _iskeyword
4dc01402d78a Merged revisions 59541-59561 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44310
diff changeset
12 import sys as _sys
50793
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
13 import heapq as _heapq
52287
60d06c98a605 Make imported name private and wrap long-line.
Raymond Hettinger <python@rcn.com>
parents: 52283
diff changeset
14 from weakref import proxy as _proxy
51947
ca87c4707bfd Missed my last update to __eq__ to check matching length.
Raymond Hettinger <python@rcn.com>
parents: 51946
diff changeset
15 from itertools import repeat as _repeat, chain as _chain, starmap as _starmap
64726
cfaf0f7ee0c2 Issue 9840: Add reprlib.recursive_repr(), a decorator for handling recursive calls to __repr__ methods.
Raymond Hettinger <python@rcn.com>
parents: 64684
diff changeset
16 from reprlib import recursive_repr as _recursive_repr
51946
c910f6e3bb28 PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents: 51503
diff changeset
17
c910f6e3bb28 PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents: 51503
diff changeset
18 ################################################################################
c910f6e3bb28 PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents: 51503
diff changeset
19 ### OrderedDict
c910f6e3bb28 PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents: 51503
diff changeset
20 ################################################################################
c910f6e3bb28 PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents: 51503
diff changeset
21
52283
13f887e5fa0d Forward port r70533 and r70538.
Raymond Hettinger <python@rcn.com>
parents: 52232
diff changeset
22 class _Link(object):
13f887e5fa0d Forward port r70533 and r70538.
Raymond Hettinger <python@rcn.com>
parents: 52232
diff changeset
23 __slots__ = 'prev', 'next', 'key', '__weakref__'
13f887e5fa0d Forward port r70533 and r70538.
Raymond Hettinger <python@rcn.com>
parents: 52232
diff changeset
24
67359
4557d495a484 Backport r87613 to make OrderedDict subclassing match dict subclassing.
Raymond Hettinger <python@rcn.com>
parents: 67290
diff changeset
25 class OrderedDict(dict):
52230
422e2e6763e9 Forward port 70475: Add implementation notes. Put methods in more readable order.
Raymond Hettinger <python@rcn.com>
parents: 52228
diff changeset
26 'Dictionary that remembers insertion order'
52283
13f887e5fa0d Forward port r70533 and r70538.
Raymond Hettinger <python@rcn.com>
parents: 52232
diff changeset
27 # An inherited dict maps keys to values.
52230
422e2e6763e9 Forward port 70475: Add implementation notes. Put methods in more readable order.
Raymond Hettinger <python@rcn.com>
parents: 52228
diff changeset
28 # The inherited dict provides __getitem__, __len__, __contains__, and get.
422e2e6763e9 Forward port 70475: Add implementation notes. Put methods in more readable order.
Raymond Hettinger <python@rcn.com>
parents: 52228
diff changeset
29 # The remaining methods are order-aware.
69541
b0385ec55e7e Minor clean-ups to docstrings, comments, and var names.
Raymond Hettinger <python@rcn.com>
parents: 69493
diff changeset
30 # Big-O running times for all methods are the same as regular dictionaries.
52283
13f887e5fa0d Forward port r70533 and r70538.
Raymond Hettinger <python@rcn.com>
parents: 52232
diff changeset
31
69541
b0385ec55e7e Minor clean-ups to docstrings, comments, and var names.
Raymond Hettinger <python@rcn.com>
parents: 69493
diff changeset
32 # The internal self.__map dict maps keys to links in a doubly linked list.
52283
13f887e5fa0d Forward port r70533 and r70538.
Raymond Hettinger <python@rcn.com>
parents: 52232
diff changeset
33 # The circular doubly linked list starts and ends with a sentinel element.
13f887e5fa0d Forward port r70533 and r70538.
Raymond Hettinger <python@rcn.com>
parents: 52232
diff changeset
34 # The sentinel element never gets deleted (this simplifies the algorithm).
69541
b0385ec55e7e Minor clean-ups to docstrings, comments, and var names.
Raymond Hettinger <python@rcn.com>
parents: 69493
diff changeset
35 # The sentinel is in self.__hardroot with a weakref proxy in self.__root.
74628
f7283825effa Issue 13870: Fix out of date comment.
Raymond Hettinger <python@rcn.com>
parents: 73386
diff changeset
36 # The prev links are weakref proxies (to prevent circular references).
52283
13f887e5fa0d Forward port r70533 and r70538.
Raymond Hettinger <python@rcn.com>
parents: 52232
diff changeset
37 # Individual links are kept alive by the hard reference in self.__map.
13f887e5fa0d Forward port r70533 and r70538.
Raymond Hettinger <python@rcn.com>
parents: 52232
diff changeset
38 # Those hard references disappear when a key is deleted from an OrderedDict.
51946
c910f6e3bb28 PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents: 51503
diff changeset
39
c910f6e3bb28 PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents: 51503
diff changeset
40 def __init__(self, *args, **kwds):
69541
b0385ec55e7e Minor clean-ups to docstrings, comments, and var names.
Raymond Hettinger <python@rcn.com>
parents: 69493
diff changeset
41 '''Initialize an ordered dictionary. The signature is the same as
b0385ec55e7e Minor clean-ups to docstrings, comments, and var names.
Raymond Hettinger <python@rcn.com>
parents: 69493
diff changeset
42 regular dictionaries, but keyword arguments are not recommended because
b0385ec55e7e Minor clean-ups to docstrings, comments, and var names.
Raymond Hettinger <python@rcn.com>
parents: 69493
diff changeset
43 their insertion order is arbitrary.
52980
74d9f75d9dc1 Add docstrings.
Raymond Hettinger <python@rcn.com>
parents: 52834
diff changeset
44
74d9f75d9dc1 Add docstrings.
Raymond Hettinger <python@rcn.com>
parents: 52834
diff changeset
45 '''
51946
c910f6e3bb28 PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents: 51503
diff changeset
46 if len(args) > 1:
c910f6e3bb28 PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents: 51503
diff changeset
47 raise TypeError('expected at most 1 arguments, got %d' % len(args))
51972
0a609ba54f5c Make the underlying data structure more private.
Raymond Hettinger <python@rcn.com>
parents: 51950
diff changeset
48 try:
52283
13f887e5fa0d Forward port r70533 and r70538.
Raymond Hettinger <python@rcn.com>
parents: 52232
diff changeset
49 self.__root
51972
0a609ba54f5c Make the underlying data structure more private.
Raymond Hettinger <python@rcn.com>
parents: 51950
diff changeset
50 except AttributeError:
64752
3977dc349ae7 Improve iteration speed by only proxying back links.
Raymond Hettinger <python@rcn.com>
parents: 64726
diff changeset
51 self.__hardroot = _Link()
3977dc349ae7 Improve iteration speed by only proxying back links.
Raymond Hettinger <python@rcn.com>
parents: 64726
diff changeset
52 self.__root = root = _proxy(self.__hardroot)
52337
f27704f45d4a Separate initialization from clearing.
Raymond Hettinger <python@rcn.com>
parents: 52287
diff changeset
53 root.prev = root.next = root
f27704f45d4a Separate initialization from clearing.
Raymond Hettinger <python@rcn.com>
parents: 52287
diff changeset
54 self.__map = {}
67359
4557d495a484 Backport r87613 to make OrderedDict subclassing match dict subclassing.
Raymond Hettinger <python@rcn.com>
parents: 67290
diff changeset
55 self.__update(*args, **kwds)
51946
c910f6e3bb28 PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents: 51503
diff changeset
56
64663
f3b1eea11ce1 Issue #9825: Replace OrderedDict.__del__() with weakrefs.
Raymond Hettinger <python@rcn.com>
parents: 64642
diff changeset
57 def __setitem__(self, key, value,
f3b1eea11ce1 Issue #9825: Replace OrderedDict.__del__() with weakrefs.
Raymond Hettinger <python@rcn.com>
parents: 64642
diff changeset
58 dict_setitem=dict.__setitem__, proxy=_proxy, Link=_Link):
52980
74d9f75d9dc1 Add docstrings.
Raymond Hettinger <python@rcn.com>
parents: 52834
diff changeset
59 'od.__setitem__(i, y) <==> od[i]=y'
69541
b0385ec55e7e Minor clean-ups to docstrings, comments, and var names.
Raymond Hettinger <python@rcn.com>
parents: 69493
diff changeset
60 # Setting a new item creates a new link at the end of the linked list,
b0385ec55e7e Minor clean-ups to docstrings, comments, and var names.
Raymond Hettinger <python@rcn.com>
parents: 69493
diff changeset
61 # and the inherited dictionary is updated with the new key/value pair.
51946
c910f6e3bb28 PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents: 51503
diff changeset
62 if key not in self:
64663
f3b1eea11ce1 Issue #9825: Replace OrderedDict.__del__() with weakrefs.
Raymond Hettinger <python@rcn.com>
parents: 64642
diff changeset
63 self.__map[key] = link = Link()
52283
13f887e5fa0d Forward port r70533 and r70538.
Raymond Hettinger <python@rcn.com>
parents: 52232
diff changeset
64 root = self.__root
13f887e5fa0d Forward port r70533 and r70538.
Raymond Hettinger <python@rcn.com>
parents: 52232
diff changeset
65 last = root.prev
13f887e5fa0d Forward port r70533 and r70538.
Raymond Hettinger <python@rcn.com>
parents: 52232
diff changeset
66 link.prev, link.next, link.key = last, root, key
64752
3977dc349ae7 Improve iteration speed by only proxying back links.
Raymond Hettinger <python@rcn.com>
parents: 64726
diff changeset
67 last.next = link
3977dc349ae7 Improve iteration speed by only proxying back links.
Raymond Hettinger <python@rcn.com>
parents: 64726
diff changeset
68 root.prev = proxy(link)
3977dc349ae7 Improve iteration speed by only proxying back links.
Raymond Hettinger <python@rcn.com>
parents: 64726
diff changeset
69 dict_setitem(self, key, value)
51946
c910f6e3bb28 PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents: 51503
diff changeset
70
64663
f3b1eea11ce1 Issue #9825: Replace OrderedDict.__del__() with weakrefs.
Raymond Hettinger <python@rcn.com>
parents: 64642
diff changeset
71 def __delitem__(self, key, dict_delitem=dict.__delitem__):
52980
74d9f75d9dc1 Add docstrings.
Raymond Hettinger <python@rcn.com>
parents: 52834
diff changeset
72 'od.__delitem__(y) <==> del od[y]'
69541
b0385ec55e7e Minor clean-ups to docstrings, comments, and var names.
Raymond Hettinger <python@rcn.com>
parents: 69493
diff changeset
73 # Deleting an existing item uses self.__map to find the link which gets
b0385ec55e7e Minor clean-ups to docstrings, comments, and var names.
Raymond Hettinger <python@rcn.com>
parents: 69493
diff changeset
74 # removed by updating the links in the predecessor and successor nodes.
63510
b7d5cbbea180 Update OrderedDict implementation to match that in Py2.7.
Raymond Hettinger <python@rcn.com>
parents: 60512
diff changeset
75 dict_delitem(self, key)
52283
13f887e5fa0d Forward port r70533 and r70538.
Raymond Hettinger <python@rcn.com>
parents: 52232
diff changeset
76 link = self.__map.pop(key)
64663
f3b1eea11ce1 Issue #9825: Replace OrderedDict.__del__() with weakrefs.
Raymond Hettinger <python@rcn.com>
parents: 64642
diff changeset
77 link_prev = link.prev
f3b1eea11ce1 Issue #9825: Replace OrderedDict.__del__() with weakrefs.
Raymond Hettinger <python@rcn.com>
parents: 64642
diff changeset
78 link_next = link.next
f3b1eea11ce1 Issue #9825: Replace OrderedDict.__del__() with weakrefs.
Raymond Hettinger <python@rcn.com>
parents: 64642
diff changeset
79 link_prev.next = link_next
f3b1eea11ce1 Issue #9825: Replace OrderedDict.__del__() with weakrefs.
Raymond Hettinger <python@rcn.com>
parents: 64642
diff changeset
80 link_next.prev = link_prev
51946
c910f6e3bb28 PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents: 51503
diff changeset
81
c910f6e3bb28 PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents: 51503
diff changeset
82 def __iter__(self):
52980
74d9f75d9dc1 Add docstrings.
Raymond Hettinger <python@rcn.com>
parents: 52834
diff changeset
83 'od.__iter__() <==> iter(od)'
52283
13f887e5fa0d Forward port r70533 and r70538.
Raymond Hettinger <python@rcn.com>
parents: 52232
diff changeset
84 # Traverse the linked list in order.
13f887e5fa0d Forward port r70533 and r70538.
Raymond Hettinger <python@rcn.com>
parents: 52232
diff changeset
85 root = self.__root
13f887e5fa0d Forward port r70533 and r70538.
Raymond Hettinger <python@rcn.com>
parents: 52232
diff changeset
86 curr = root.next
13f887e5fa0d Forward port r70533 and r70538.
Raymond Hettinger <python@rcn.com>
parents: 52232
diff changeset
87 while curr is not root:
13f887e5fa0d Forward port r70533 and r70538.
Raymond Hettinger <python@rcn.com>
parents: 52232
diff changeset
88 yield curr.key
13f887e5fa0d Forward port r70533 and r70538.
Raymond Hettinger <python@rcn.com>
parents: 52232
diff changeset
89 curr = curr.next
51946
c910f6e3bb28 PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents: 51503
diff changeset
90
c910f6e3bb28 PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents: 51503
diff changeset
91 def __reversed__(self):
52981
Raymond Hettinger <python@rcn.com>
parents: 52980
diff changeset
92 'od.__reversed__() <==> reversed(od)'
52283
13f887e5fa0d Forward port r70533 and r70538.
Raymond Hettinger <python@rcn.com>
parents: 52232
diff changeset
93 # Traverse the linked list in reverse order.
13f887e5fa0d Forward port r70533 and r70538.
Raymond Hettinger <python@rcn.com>
parents: 52232
diff changeset
94 root = self.__root
13f887e5fa0d Forward port r70533 and r70538.
Raymond Hettinger <python@rcn.com>
parents: 52232
diff changeset
95 curr = root.prev
13f887e5fa0d Forward port r70533 and r70538.
Raymond Hettinger <python@rcn.com>
parents: 52232
diff changeset
96 while curr is not root:
13f887e5fa0d Forward port r70533 and r70538.
Raymond Hettinger <python@rcn.com>
parents: 52232
diff changeset
97 yield curr.key
13f887e5fa0d Forward port r70533 and r70538.
Raymond Hettinger <python@rcn.com>
parents: 52232
diff changeset
98 curr = curr.prev
51946
c910f6e3bb28 PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents: 51503
diff changeset
99
63510
b7d5cbbea180 Update OrderedDict implementation to match that in Py2.7.
Raymond Hettinger <python@rcn.com>
parents: 60512
diff changeset
100 def clear(self):
b7d5cbbea180 Update OrderedDict implementation to match that in Py2.7.
Raymond Hettinger <python@rcn.com>
parents: 60512
diff changeset
101 'od.clear() -> None. Remove all items from od.'
64663
f3b1eea11ce1 Issue #9825: Replace OrderedDict.__del__() with weakrefs.
Raymond Hettinger <python@rcn.com>
parents: 64642
diff changeset
102 root = self.__root
f3b1eea11ce1 Issue #9825: Replace OrderedDict.__del__() with weakrefs.
Raymond Hettinger <python@rcn.com>
parents: 64642
diff changeset
103 root.prev = root.next = root
f3b1eea11ce1 Issue #9825: Replace OrderedDict.__del__() with weakrefs.
Raymond Hettinger <python@rcn.com>
parents: 64642
diff changeset
104 self.__map.clear()
63510
b7d5cbbea180 Update OrderedDict implementation to match that in Py2.7.
Raymond Hettinger <python@rcn.com>
parents: 60512
diff changeset
105 dict.clear(self)
b7d5cbbea180 Update OrderedDict implementation to match that in Py2.7.
Raymond Hettinger <python@rcn.com>
parents: 60512
diff changeset
106
64663
f3b1eea11ce1 Issue #9825: Replace OrderedDict.__del__() with weakrefs.
Raymond Hettinger <python@rcn.com>
parents: 64642
diff changeset
107 def popitem(self, last=True):
64379
bd7435a8708b Make OrderedDict.popitem() a bit smarter and faster
Raymond Hettinger <python@rcn.com>
parents: 64375
diff changeset
108 '''od.popitem() -> (k, v), return and remove a (key, value) pair.
bd7435a8708b Make OrderedDict.popitem() a bit smarter and faster
Raymond Hettinger <python@rcn.com>
parents: 64375
diff changeset
109 Pairs are returned in LIFO order if last is true or FIFO order if false.
bd7435a8708b Make OrderedDict.popitem() a bit smarter and faster
Raymond Hettinger <python@rcn.com>
parents: 64375
diff changeset
110
bd7435a8708b Make OrderedDict.popitem() a bit smarter and faster
Raymond Hettinger <python@rcn.com>
parents: 64375
diff changeset
111 '''
bd7435a8708b Make OrderedDict.popitem() a bit smarter and faster
Raymond Hettinger <python@rcn.com>
parents: 64375
diff changeset
112 if not self:
bd7435a8708b Make OrderedDict.popitem() a bit smarter and faster
Raymond Hettinger <python@rcn.com>
parents: 64375
diff changeset
113 raise KeyError('dictionary is empty')
bd7435a8708b Make OrderedDict.popitem() a bit smarter and faster
Raymond Hettinger <python@rcn.com>
parents: 64375
diff changeset
114 root = self.__root
64663
f3b1eea11ce1 Issue #9825: Replace OrderedDict.__del__() with weakrefs.
Raymond Hettinger <python@rcn.com>
parents: 64642
diff changeset
115 if last:
f3b1eea11ce1 Issue #9825: Replace OrderedDict.__del__() with weakrefs.
Raymond Hettinger <python@rcn.com>
parents: 64642
diff changeset
116 link = root.prev
f3b1eea11ce1 Issue #9825: Replace OrderedDict.__del__() with weakrefs.
Raymond Hettinger <python@rcn.com>
parents: 64642
diff changeset
117 link_prev = link.prev
f3b1eea11ce1 Issue #9825: Replace OrderedDict.__del__() with weakrefs.
Raymond Hettinger <python@rcn.com>
parents: 64642
diff changeset
118 link_prev.next = root
f3b1eea11ce1 Issue #9825: Replace OrderedDict.__del__() with weakrefs.
Raymond Hettinger <python@rcn.com>
parents: 64642
diff changeset
119 root.prev = link_prev
f3b1eea11ce1 Issue #9825: Replace OrderedDict.__del__() with weakrefs.
Raymond Hettinger <python@rcn.com>
parents: 64642
diff changeset
120 else:
f3b1eea11ce1 Issue #9825: Replace OrderedDict.__del__() with weakrefs.
Raymond Hettinger <python@rcn.com>
parents: 64642
diff changeset
121 link = root.next
f3b1eea11ce1 Issue #9825: Replace OrderedDict.__del__() with weakrefs.
Raymond Hettinger <python@rcn.com>
parents: 64642
diff changeset
122 link_next = link.next
f3b1eea11ce1 Issue #9825: Replace OrderedDict.__del__() with weakrefs.
Raymond Hettinger <python@rcn.com>
parents: 64642
diff changeset
123 root.next = link_next
f3b1eea11ce1 Issue #9825: Replace OrderedDict.__del__() with weakrefs.
Raymond Hettinger <python@rcn.com>
parents: 64642
diff changeset
124 link_next.prev = root
f3b1eea11ce1 Issue #9825: Replace OrderedDict.__del__() with weakrefs.
Raymond Hettinger <python@rcn.com>
parents: 64642
diff changeset
125 key = link.key
64379
bd7435a8708b Make OrderedDict.popitem() a bit smarter and faster
Raymond Hettinger <python@rcn.com>
parents: 64375
diff changeset
126 del self.__map[key]
64663
f3b1eea11ce1 Issue #9825: Replace OrderedDict.__del__() with weakrefs.
Raymond Hettinger <python@rcn.com>
parents: 64642
diff changeset
127 value = dict.pop(self, key)
64379
bd7435a8708b Make OrderedDict.popitem() a bit smarter and faster
Raymond Hettinger <python@rcn.com>
parents: 64375
diff changeset
128 return key, value
bd7435a8708b Make OrderedDict.popitem() a bit smarter and faster
Raymond Hettinger <python@rcn.com>
parents: 64375
diff changeset
129
64663
f3b1eea11ce1 Issue #9825: Replace OrderedDict.__del__() with weakrefs.
Raymond Hettinger <python@rcn.com>
parents: 64642
diff changeset
130 def move_to_end(self, key, last=True):
f3b1eea11ce1 Issue #9825: Replace OrderedDict.__del__() with weakrefs.
Raymond Hettinger <python@rcn.com>
parents: 64642
diff changeset
131 '''Move an existing element to the end (or beginning if last==False).
f3b1eea11ce1 Issue #9825: Replace OrderedDict.__del__() with weakrefs.
Raymond Hettinger <python@rcn.com>
parents: 64642
diff changeset
132
f3b1eea11ce1 Issue #9825: Replace OrderedDict.__del__() with weakrefs.
Raymond Hettinger <python@rcn.com>
parents: 64642
diff changeset
133 Raises KeyError if the element does not exist.
f3b1eea11ce1 Issue #9825: Replace OrderedDict.__del__() with weakrefs.
Raymond Hettinger <python@rcn.com>
parents: 64642
diff changeset
134 When last=True, acts like a fast version of self[key]=self.pop(key).
f3b1eea11ce1 Issue #9825: Replace OrderedDict.__del__() with weakrefs.
Raymond Hettinger <python@rcn.com>
parents: 64642
diff changeset
135
f3b1eea11ce1 Issue #9825: Replace OrderedDict.__del__() with weakrefs.
Raymond Hettinger <python@rcn.com>
parents: 64642
diff changeset
136 '''
f3b1eea11ce1 Issue #9825: Replace OrderedDict.__del__() with weakrefs.
Raymond Hettinger <python@rcn.com>
parents: 64642
diff changeset
137 link = self.__map[key]
f3b1eea11ce1 Issue #9825: Replace OrderedDict.__del__() with weakrefs.
Raymond Hettinger <python@rcn.com>
parents: 64642
diff changeset
138 link_prev = link.prev
f3b1eea11ce1 Issue #9825: Replace OrderedDict.__del__() with weakrefs.
Raymond Hettinger <python@rcn.com>
parents: 64642
diff changeset
139 link_next = link.next
f3b1eea11ce1 Issue #9825: Replace OrderedDict.__del__() with weakrefs.
Raymond Hettinger <python@rcn.com>
parents: 64642
diff changeset
140 link_prev.next = link_next
f3b1eea11ce1 Issue #9825: Replace OrderedDict.__del__() with weakrefs.
Raymond Hettinger <python@rcn.com>
parents: 64642
diff changeset
141 link_next.prev = link_prev
f3b1eea11ce1 Issue #9825: Replace OrderedDict.__del__() with weakrefs.
Raymond Hettinger <python@rcn.com>
parents: 64642
diff changeset
142 root = self.__root
f3b1eea11ce1 Issue #9825: Replace OrderedDict.__del__() with weakrefs.
Raymond Hettinger <python@rcn.com>
parents: 64642
diff changeset
143 if last:
f3b1eea11ce1 Issue #9825: Replace OrderedDict.__del__() with weakrefs.
Raymond Hettinger <python@rcn.com>
parents: 64642
diff changeset
144 last = root.prev
f3b1eea11ce1 Issue #9825: Replace OrderedDict.__del__() with weakrefs.
Raymond Hettinger <python@rcn.com>
parents: 64642
diff changeset
145 link.prev = last
f3b1eea11ce1 Issue #9825: Replace OrderedDict.__del__() with weakrefs.
Raymond Hettinger <python@rcn.com>
parents: 64642
diff changeset
146 link.next = root
f3b1eea11ce1 Issue #9825: Replace OrderedDict.__del__() with weakrefs.
Raymond Hettinger <python@rcn.com>
parents: 64642
diff changeset
147 last.next = root.prev = link
f3b1eea11ce1 Issue #9825: Replace OrderedDict.__del__() with weakrefs.
Raymond Hettinger <python@rcn.com>
parents: 64642
diff changeset
148 else:
f3b1eea11ce1 Issue #9825: Replace OrderedDict.__del__() with weakrefs.
Raymond Hettinger <python@rcn.com>
parents: 64642
diff changeset
149 first = root.next
f3b1eea11ce1 Issue #9825: Replace OrderedDict.__del__() with weakrefs.
Raymond Hettinger <python@rcn.com>
parents: 64642
diff changeset
150 link.prev = root
f3b1eea11ce1 Issue #9825: Replace OrderedDict.__del__() with weakrefs.
Raymond Hettinger <python@rcn.com>
parents: 64642
diff changeset
151 link.next = first
f3b1eea11ce1 Issue #9825: Replace OrderedDict.__del__() with weakrefs.
Raymond Hettinger <python@rcn.com>
parents: 64642
diff changeset
152 root.next = first.prev = link
f3b1eea11ce1 Issue #9825: Replace OrderedDict.__del__() with weakrefs.
Raymond Hettinger <python@rcn.com>
parents: 64642
diff changeset
153
64783
a177ddde45d1 Issue 9865: add __sizeof__ to OrderedDict.
Raymond Hettinger <python@rcn.com>
parents: 64777
diff changeset
154 def __sizeof__(self):
a177ddde45d1 Issue 9865: add __sizeof__ to OrderedDict.
Raymond Hettinger <python@rcn.com>
parents: 64777
diff changeset
155 sizeof = _sys.getsizeof
a177ddde45d1 Issue 9865: add __sizeof__ to OrderedDict.
Raymond Hettinger <python@rcn.com>
parents: 64777
diff changeset
156 n = len(self) + 1 # number of links including root
a177ddde45d1 Issue 9865: add __sizeof__ to OrderedDict.
Raymond Hettinger <python@rcn.com>
parents: 64777
diff changeset
157 size = sizeof(self.__dict__) # instance dictionary
a177ddde45d1 Issue 9865: add __sizeof__ to OrderedDict.
Raymond Hettinger <python@rcn.com>
parents: 64777
diff changeset
158 size += sizeof(self.__map) * 2 # internal dict and inherited dict
a177ddde45d1 Issue 9865: add __sizeof__ to OrderedDict.
Raymond Hettinger <python@rcn.com>
parents: 64777
diff changeset
159 size += sizeof(self.__hardroot) * n # link objects
a177ddde45d1 Issue 9865: add __sizeof__ to OrderedDict.
Raymond Hettinger <python@rcn.com>
parents: 64777
diff changeset
160 size += sizeof(self.__root) * n # proxy objects
a177ddde45d1 Issue 9865: add __sizeof__ to OrderedDict.
Raymond Hettinger <python@rcn.com>
parents: 64777
diff changeset
161 return size
a177ddde45d1 Issue 9865: add __sizeof__ to OrderedDict.
Raymond Hettinger <python@rcn.com>
parents: 64777
diff changeset
162
67359
4557d495a484 Backport r87613 to make OrderedDict subclassing match dict subclassing.
Raymond Hettinger <python@rcn.com>
parents: 67290
diff changeset
163 update = __update = MutableMapping.update
51946
c910f6e3bb28 PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents: 51503
diff changeset
164 keys = MutableMapping.keys
c910f6e3bb28 PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents: 51503
diff changeset
165 values = MutableMapping.values
c910f6e3bb28 PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents: 51503
diff changeset
166 items = MutableMapping.items
69444
15afaf8aa155 Hmm, __ne__ was missing
Raymond Hettinger <python@rcn.com>
parents: 69443
diff changeset
167 __ne__ = MutableMapping.__ne__
51946
c910f6e3bb28 PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents: 51503
diff changeset
168
67359
4557d495a484 Backport r87613 to make OrderedDict subclassing match dict subclassing.
Raymond Hettinger <python@rcn.com>
parents: 67290
diff changeset
169 __marker = object()
4557d495a484 Backport r87613 to make OrderedDict subclassing match dict subclassing.
Raymond Hettinger <python@rcn.com>
parents: 67290
diff changeset
170
4557d495a484 Backport r87613 to make OrderedDict subclassing match dict subclassing.
Raymond Hettinger <python@rcn.com>
parents: 67290
diff changeset
171 def pop(self, key, default=__marker):
69541
b0385ec55e7e Minor clean-ups to docstrings, comments, and var names.
Raymond Hettinger <python@rcn.com>
parents: 69493
diff changeset
172 '''od.pop(k[,d]) -> v, remove specified key and return the corresponding
b0385ec55e7e Minor clean-ups to docstrings, comments, and var names.
Raymond Hettinger <python@rcn.com>
parents: 69493
diff changeset
173 value. If key is not found, d is returned if given, otherwise KeyError
b0385ec55e7e Minor clean-ups to docstrings, comments, and var names.
Raymond Hettinger <python@rcn.com>
parents: 69493
diff changeset
174 is raised.
b0385ec55e7e Minor clean-ups to docstrings, comments, and var names.
Raymond Hettinger <python@rcn.com>
parents: 69493
diff changeset
175
b0385ec55e7e Minor clean-ups to docstrings, comments, and var names.
Raymond Hettinger <python@rcn.com>
parents: 69493
diff changeset
176 '''
67359
4557d495a484 Backport r87613 to make OrderedDict subclassing match dict subclassing.
Raymond Hettinger <python@rcn.com>
parents: 67290
diff changeset
177 if key in self:
4557d495a484 Backport r87613 to make OrderedDict subclassing match dict subclassing.
Raymond Hettinger <python@rcn.com>
parents: 67290
diff changeset
178 result = self[key]
4557d495a484 Backport r87613 to make OrderedDict subclassing match dict subclassing.
Raymond Hettinger <python@rcn.com>
parents: 67290
diff changeset
179 del self[key]
4557d495a484 Backport r87613 to make OrderedDict subclassing match dict subclassing.
Raymond Hettinger <python@rcn.com>
parents: 67290
diff changeset
180 return result
4557d495a484 Backport r87613 to make OrderedDict subclassing match dict subclassing.
Raymond Hettinger <python@rcn.com>
parents: 67290
diff changeset
181 if default is self.__marker:
4557d495a484 Backport r87613 to make OrderedDict subclassing match dict subclassing.
Raymond Hettinger <python@rcn.com>
parents: 67290
diff changeset
182 raise KeyError(key)
4557d495a484 Backport r87613 to make OrderedDict subclassing match dict subclassing.
Raymond Hettinger <python@rcn.com>
parents: 67290
diff changeset
183 return default
4557d495a484 Backport r87613 to make OrderedDict subclassing match dict subclassing.
Raymond Hettinger <python@rcn.com>
parents: 67290
diff changeset
184
4557d495a484 Backport r87613 to make OrderedDict subclassing match dict subclassing.
Raymond Hettinger <python@rcn.com>
parents: 67290
diff changeset
185 def setdefault(self, key, default=None):
69541
b0385ec55e7e Minor clean-ups to docstrings, comments, and var names.
Raymond Hettinger <python@rcn.com>
parents: 69493
diff changeset
186 'od.setdefault(k[,d]) -> od.get(k,d), also set od[k]=d if k not in od'
67359
4557d495a484 Backport r87613 to make OrderedDict subclassing match dict subclassing.
Raymond Hettinger <python@rcn.com>
parents: 67290
diff changeset
187 if key in self:
4557d495a484 Backport r87613 to make OrderedDict subclassing match dict subclassing.
Raymond Hettinger <python@rcn.com>
parents: 67290
diff changeset
188 return self[key]
4557d495a484 Backport r87613 to make OrderedDict subclassing match dict subclassing.
Raymond Hettinger <python@rcn.com>
parents: 67290
diff changeset
189 self[key] = default
4557d495a484 Backport r87613 to make OrderedDict subclassing match dict subclassing.
Raymond Hettinger <python@rcn.com>
parents: 67290
diff changeset
190 return default
4557d495a484 Backport r87613 to make OrderedDict subclassing match dict subclassing.
Raymond Hettinger <python@rcn.com>
parents: 67290
diff changeset
191
64726
cfaf0f7ee0c2 Issue 9840: Add reprlib.recursive_repr(), a decorator for handling recursive calls to __repr__ methods.
Raymond Hettinger <python@rcn.com>
parents: 64684
diff changeset
192 @_recursive_repr()
51946
c910f6e3bb28 PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents: 51503
diff changeset
193 def __repr__(self):
52980
74d9f75d9dc1 Add docstrings.
Raymond Hettinger <python@rcn.com>
parents: 52834
diff changeset
194 'od.__repr__() <==> repr(od)'
51946
c910f6e3bb28 PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents: 51503
diff changeset
195 if not self:
c910f6e3bb28 PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents: 51503
diff changeset
196 return '%s()' % (self.__class__.__name__,)
64726
cfaf0f7ee0c2 Issue 9840: Add reprlib.recursive_repr(), a decorator for handling recursive calls to __repr__ methods.
Raymond Hettinger <python@rcn.com>
parents: 64684
diff changeset
197 return '%s(%r)' % (self.__class__.__name__, list(self.items()))
51946
c910f6e3bb28 PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents: 51503
diff changeset
198
69492
e9979852fc02 Minor text rearrangement.
Raymond Hettinger <python@rcn.com>
parents: 69460
diff changeset
199 def __reduce__(self):
e9979852fc02 Minor text rearrangement.
Raymond Hettinger <python@rcn.com>
parents: 69460
diff changeset
200 'Return state information for pickling'
e9979852fc02 Minor text rearrangement.
Raymond Hettinger <python@rcn.com>
parents: 69460
diff changeset
201 items = [[k, self[k]] for k in self]
e9979852fc02 Minor text rearrangement.
Raymond Hettinger <python@rcn.com>
parents: 69460
diff changeset
202 inst_dict = vars(self).copy()
e9979852fc02 Minor text rearrangement.
Raymond Hettinger <python@rcn.com>
parents: 69460
diff changeset
203 for k in vars(OrderedDict()):
e9979852fc02 Minor text rearrangement.
Raymond Hettinger <python@rcn.com>
parents: 69460
diff changeset
204 inst_dict.pop(k, None)
e9979852fc02 Minor text rearrangement.
Raymond Hettinger <python@rcn.com>
parents: 69460
diff changeset
205 if inst_dict:
e9979852fc02 Minor text rearrangement.
Raymond Hettinger <python@rcn.com>
parents: 69460
diff changeset
206 return (self.__class__, (items,), inst_dict)
e9979852fc02 Minor text rearrangement.
Raymond Hettinger <python@rcn.com>
parents: 69460
diff changeset
207 return self.__class__, (items,)
e9979852fc02 Minor text rearrangement.
Raymond Hettinger <python@rcn.com>
parents: 69460
diff changeset
208
51946
c910f6e3bb28 PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents: 51503
diff changeset
209 def copy(self):
52980
74d9f75d9dc1 Add docstrings.
Raymond Hettinger <python@rcn.com>
parents: 52834
diff changeset
210 'od.copy() -> a shallow copy of od'
51946
c910f6e3bb28 PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents: 51503
diff changeset
211 return self.__class__(self)
c910f6e3bb28 PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents: 51503
diff changeset
212
c910f6e3bb28 PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents: 51503
diff changeset
213 @classmethod
c910f6e3bb28 PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents: 51503
diff changeset
214 def fromkeys(cls, iterable, value=None):
69541
b0385ec55e7e Minor clean-ups to docstrings, comments, and var names.
Raymond Hettinger <python@rcn.com>
parents: 69493
diff changeset
215 '''OD.fromkeys(S[, v]) -> New ordered dictionary with keys from S.
b0385ec55e7e Minor clean-ups to docstrings, comments, and var names.
Raymond Hettinger <python@rcn.com>
parents: 69493
diff changeset
216 If not specified, the value defaults to None.
52980
74d9f75d9dc1 Add docstrings.
Raymond Hettinger <python@rcn.com>
parents: 52834
diff changeset
217
74d9f75d9dc1 Add docstrings.
Raymond Hettinger <python@rcn.com>
parents: 52834
diff changeset
218 '''
69541
b0385ec55e7e Minor clean-ups to docstrings, comments, and var names.
Raymond Hettinger <python@rcn.com>
parents: 69493
diff changeset
219 self = cls()
51946
c910f6e3bb28 PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents: 51503
diff changeset
220 for key in iterable:
69541
b0385ec55e7e Minor clean-ups to docstrings, comments, and var names.
Raymond Hettinger <python@rcn.com>
parents: 69493
diff changeset
221 self[key] = value
b0385ec55e7e Minor clean-ups to docstrings, comments, and var names.
Raymond Hettinger <python@rcn.com>
parents: 69493
diff changeset
222 return self
51946
c910f6e3bb28 PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents: 51503
diff changeset
223
c910f6e3bb28 PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents: 51503
diff changeset
224 def __eq__(self, other):
52980
74d9f75d9dc1 Add docstrings.
Raymond Hettinger <python@rcn.com>
parents: 52834
diff changeset
225 '''od.__eq__(y) <==> od==y. Comparison to another OD is order-sensitive
74d9f75d9dc1 Add docstrings.
Raymond Hettinger <python@rcn.com>
parents: 52834
diff changeset
226 while comparison to a regular mapping is order-insensitive.
74d9f75d9dc1 Add docstrings.
Raymond Hettinger <python@rcn.com>
parents: 52834
diff changeset
227
74d9f75d9dc1 Add docstrings.
Raymond Hettinger <python@rcn.com>
parents: 52834
diff changeset
228 '''
51946
c910f6e3bb28 PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents: 51503
diff changeset
229 if isinstance(other, OrderedDict):
52287
60d06c98a605 Make imported name private and wrap long-line.
Raymond Hettinger <python@rcn.com>
parents: 52283
diff changeset
230 return len(self)==len(other) and \
60d06c98a605 Make imported name private and wrap long-line.
Raymond Hettinger <python@rcn.com>
parents: 52283
diff changeset
231 all(p==q for p, q in zip(self.items(), other.items()))
51946
c910f6e3bb28 PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents: 51503
diff changeset
232 return dict.__eq__(self, other)
c910f6e3bb28 PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents: 51503
diff changeset
233
44349
4dc01402d78a Merged revisions 59541-59561 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44310
diff changeset
234
45162
646347444faa Put an updated UserDict class in the collections module and
Raymond Hettinger <python@rcn.com>
parents: 44643
diff changeset
235 ################################################################################
646347444faa Put an updated UserDict class in the collections module and
Raymond Hettinger <python@rcn.com>
parents: 44643
diff changeset
236 ### namedtuple
646347444faa Put an updated UserDict class in the collections module and
Raymond Hettinger <python@rcn.com>
parents: 44643
diff changeset
237 ################################################################################
646347444faa Put an updated UserDict class in the collections module and
Raymond Hettinger <python@rcn.com>
parents: 44643
diff changeset
238
69786
b312f8f512a9 Backport code cleanup for namedtuples.
Raymond Hettinger <python@rcn.com>
parents: 69541
diff changeset
239 _class_template = '''\
b312f8f512a9 Backport code cleanup for namedtuples.
Raymond Hettinger <python@rcn.com>
parents: 69541
diff changeset
240 from builtins import property as _property, tuple as _tuple
b312f8f512a9 Backport code cleanup for namedtuples.
Raymond Hettinger <python@rcn.com>
parents: 69541
diff changeset
241 from operator import itemgetter as _itemgetter
b312f8f512a9 Backport code cleanup for namedtuples.
Raymond Hettinger <python@rcn.com>
parents: 69541
diff changeset
242 from collections import OrderedDict
b312f8f512a9 Backport code cleanup for namedtuples.
Raymond Hettinger <python@rcn.com>
parents: 69541
diff changeset
243
b312f8f512a9 Backport code cleanup for namedtuples.
Raymond Hettinger <python@rcn.com>
parents: 69541
diff changeset
244 class {typename}(tuple):
b312f8f512a9 Backport code cleanup for namedtuples.
Raymond Hettinger <python@rcn.com>
parents: 69541
diff changeset
245 '{typename}({arg_list})'
b312f8f512a9 Backport code cleanup for namedtuples.
Raymond Hettinger <python@rcn.com>
parents: 69541
diff changeset
246
b312f8f512a9 Backport code cleanup for namedtuples.
Raymond Hettinger <python@rcn.com>
parents: 69541
diff changeset
247 __slots__ = ()
b312f8f512a9 Backport code cleanup for namedtuples.
Raymond Hettinger <python@rcn.com>
parents: 69541
diff changeset
248
b312f8f512a9 Backport code cleanup for namedtuples.
Raymond Hettinger <python@rcn.com>
parents: 69541
diff changeset
249 _fields = {field_names!r}
b312f8f512a9 Backport code cleanup for namedtuples.
Raymond Hettinger <python@rcn.com>
parents: 69541
diff changeset
250
b312f8f512a9 Backport code cleanup for namedtuples.
Raymond Hettinger <python@rcn.com>
parents: 69541
diff changeset
251 def __new__(_cls, {arg_list}):
b312f8f512a9 Backport code cleanup for namedtuples.
Raymond Hettinger <python@rcn.com>
parents: 69541
diff changeset
252 'Create new instance of {typename}({arg_list})'
b312f8f512a9 Backport code cleanup for namedtuples.
Raymond Hettinger <python@rcn.com>
parents: 69541
diff changeset
253 return _tuple.__new__(_cls, ({arg_list}))
b312f8f512a9 Backport code cleanup for namedtuples.
Raymond Hettinger <python@rcn.com>
parents: 69541
diff changeset
254
b312f8f512a9 Backport code cleanup for namedtuples.
Raymond Hettinger <python@rcn.com>
parents: 69541
diff changeset
255 @classmethod
b312f8f512a9 Backport code cleanup for namedtuples.
Raymond Hettinger <python@rcn.com>
parents: 69541
diff changeset
256 def _make(cls, iterable, new=tuple.__new__, len=len):
b312f8f512a9 Backport code cleanup for namedtuples.
Raymond Hettinger <python@rcn.com>
parents: 69541
diff changeset
257 'Make a new {typename} object from a sequence or iterable'
b312f8f512a9 Backport code cleanup for namedtuples.
Raymond Hettinger <python@rcn.com>
parents: 69541
diff changeset
258 result = new(cls, iterable)
b312f8f512a9 Backport code cleanup for namedtuples.
Raymond Hettinger <python@rcn.com>
parents: 69541
diff changeset
259 if len(result) != {num_fields:d}:
b312f8f512a9 Backport code cleanup for namedtuples.
Raymond Hettinger <python@rcn.com>
parents: 69541
diff changeset
260 raise TypeError('Expected {num_fields:d} arguments, got %d' % len(result))
b312f8f512a9 Backport code cleanup for namedtuples.
Raymond Hettinger <python@rcn.com>
parents: 69541
diff changeset
261 return result
b312f8f512a9 Backport code cleanup for namedtuples.
Raymond Hettinger <python@rcn.com>
parents: 69541
diff changeset
262
b312f8f512a9 Backport code cleanup for namedtuples.
Raymond Hettinger <python@rcn.com>
parents: 69541
diff changeset
263 def __repr__(self):
b312f8f512a9 Backport code cleanup for namedtuples.
Raymond Hettinger <python@rcn.com>
parents: 69541
diff changeset
264 'Return a nicely formatted representation string'
b312f8f512a9 Backport code cleanup for namedtuples.
Raymond Hettinger <python@rcn.com>
parents: 69541
diff changeset
265 return self.__class__.__name__ + '({repr_fmt})' % self
b312f8f512a9 Backport code cleanup for namedtuples.
Raymond Hettinger <python@rcn.com>
parents: 69541
diff changeset
266
b312f8f512a9 Backport code cleanup for namedtuples.
Raymond Hettinger <python@rcn.com>
parents: 69541
diff changeset
267 def _asdict(self):
b312f8f512a9 Backport code cleanup for namedtuples.
Raymond Hettinger <python@rcn.com>
parents: 69541
diff changeset
268 'Return a new OrderedDict which maps field names to their values'
b312f8f512a9 Backport code cleanup for namedtuples.
Raymond Hettinger <python@rcn.com>
parents: 69541
diff changeset
269 return OrderedDict(zip(self._fields, self))
b312f8f512a9 Backport code cleanup for namedtuples.
Raymond Hettinger <python@rcn.com>
parents: 69541
diff changeset
270
70609
40ad90580935 Fix named tuples to work with vars().
Raymond Hettinger <python@rcn.com>
parents: 69791
diff changeset
271 __dict__ = property(_asdict)
40ad90580935 Fix named tuples to work with vars().
Raymond Hettinger <python@rcn.com>
parents: 69791
diff changeset
272
69786
b312f8f512a9 Backport code cleanup for namedtuples.
Raymond Hettinger <python@rcn.com>
parents: 69541
diff changeset
273 def _replace(_self, **kwds):
b312f8f512a9 Backport code cleanup for namedtuples.
Raymond Hettinger <python@rcn.com>
parents: 69541
diff changeset
274 'Return a new {typename} object replacing specified fields with new values'
b312f8f512a9 Backport code cleanup for namedtuples.
Raymond Hettinger <python@rcn.com>
parents: 69541
diff changeset
275 result = _self._make(map(kwds.pop, {field_names!r}, _self))
b312f8f512a9 Backport code cleanup for namedtuples.
Raymond Hettinger <python@rcn.com>
parents: 69541
diff changeset
276 if kwds:
b312f8f512a9 Backport code cleanup for namedtuples.
Raymond Hettinger <python@rcn.com>
parents: 69541
diff changeset
277 raise ValueError('Got unexpected field names: %r' % list(kwds))
b312f8f512a9 Backport code cleanup for namedtuples.
Raymond Hettinger <python@rcn.com>
parents: 69541
diff changeset
278 return result
b312f8f512a9 Backport code cleanup for namedtuples.
Raymond Hettinger <python@rcn.com>
parents: 69541
diff changeset
279
b312f8f512a9 Backport code cleanup for namedtuples.
Raymond Hettinger <python@rcn.com>
parents: 69541
diff changeset
280 def __getnewargs__(self):
b312f8f512a9 Backport code cleanup for namedtuples.
Raymond Hettinger <python@rcn.com>
parents: 69541
diff changeset
281 'Return self as a plain tuple. Used by copy and pickle.'
b312f8f512a9 Backport code cleanup for namedtuples.
Raymond Hettinger <python@rcn.com>
parents: 69541
diff changeset
282 return tuple(self)
b312f8f512a9 Backport code cleanup for namedtuples.
Raymond Hettinger <python@rcn.com>
parents: 69541
diff changeset
283
b312f8f512a9 Backport code cleanup for namedtuples.
Raymond Hettinger <python@rcn.com>
parents: 69541
diff changeset
284 {field_defs}
b312f8f512a9 Backport code cleanup for namedtuples.
Raymond Hettinger <python@rcn.com>
parents: 69541
diff changeset
285 '''
b312f8f512a9 Backport code cleanup for namedtuples.
Raymond Hettinger <python@rcn.com>
parents: 69541
diff changeset
286
b312f8f512a9 Backport code cleanup for namedtuples.
Raymond Hettinger <python@rcn.com>
parents: 69541
diff changeset
287 _repr_template = '{name}=%r'
b312f8f512a9 Backport code cleanup for namedtuples.
Raymond Hettinger <python@rcn.com>
parents: 69541
diff changeset
288
b312f8f512a9 Backport code cleanup for namedtuples.
Raymond Hettinger <python@rcn.com>
parents: 69541
diff changeset
289 _field_template = '''\
b312f8f512a9 Backport code cleanup for namedtuples.
Raymond Hettinger <python@rcn.com>
parents: 69541
diff changeset
290 {name} = _property(_itemgetter({index:d}), doc='Alias for field number {index:d}')
b312f8f512a9 Backport code cleanup for namedtuples.
Raymond Hettinger <python@rcn.com>
parents: 69541
diff changeset
291 '''
b312f8f512a9 Backport code cleanup for namedtuples.
Raymond Hettinger <python@rcn.com>
parents: 69541
diff changeset
292
51503
e759da93df64 Merged revisions 69466,69480 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 51121
diff changeset
293 def namedtuple(typename, field_names, verbose=False, rename=False):
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
diff changeset
294 """Returns a new subclass of tuple with named fields.
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
diff changeset
295
69786
b312f8f512a9 Backport code cleanup for namedtuples.
Raymond Hettinger <python@rcn.com>
parents: 69541
diff changeset
296 >>> Point = namedtuple('Point', ['x', 'y'])
43400
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 42322
diff changeset
297 >>> Point.__doc__ # docstring for the new class
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
diff changeset
298 'Point(x, y)'
43400
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 42322
diff changeset
299 >>> p = Point(11, y=22) # instantiate with positional args or keywords
44349
4dc01402d78a Merged revisions 59541-59561 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44310
diff changeset
300 >>> p[0] + p[1] # indexable like a plain tuple
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
diff changeset
301 33
44349
4dc01402d78a Merged revisions 59541-59561 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44310
diff changeset
302 >>> x, y = p # unpack like a regular tuple
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
diff changeset
303 >>> x, y
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
diff changeset
304 (11, 22)
43400
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 42322
diff changeset
305 >>> p.x + p.y # fields also accessable by name
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
diff changeset
306 33
44310
e70acbf26cda Merged revisions 59488-59511 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44291
diff changeset
307 >>> d = p._asdict() # convert to a dictionary
43734
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43400
diff changeset
308 >>> d['x']
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43400
diff changeset
309 11
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43400
diff changeset
310 >>> Point(**d) # convert from a dictionary
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
diff changeset
311 Point(x=11, y=22)
44310
e70acbf26cda Merged revisions 59488-59511 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44291
diff changeset
312 >>> p._replace(x=100) # _replace() is like str.replace() but targets named fields
43400
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 42322
diff changeset
313 Point(x=100, y=22)
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
diff changeset
314
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
diff changeset
315 """
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
diff changeset
316
44604
94ab572ed6dd Merged revisions 59843-59863 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44587
diff changeset
317 # Parse and validate the field names. Validation serves two purposes,
94ab572ed6dd Merged revisions 59843-59863 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44587
diff changeset
318 # generating informative error messages and preventing template injection attacks.
43734
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43400
diff changeset
319 if isinstance(field_names, str):
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43400
diff changeset
320 field_names = field_names.replace(',', ' ').split() # names separated by whitespace and/or commas
69786
b312f8f512a9 Backport code cleanup for namedtuples.
Raymond Hettinger <python@rcn.com>
parents: 69541
diff changeset
321 field_names = list(map(str, field_names))
51503
e759da93df64 Merged revisions 69466,69480 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 51121
diff changeset
322 if rename:
e759da93df64 Merged revisions 69466,69480 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 51121
diff changeset
323 seen = set()
69786
b312f8f512a9 Backport code cleanup for namedtuples.
Raymond Hettinger <python@rcn.com>
parents: 69541
diff changeset
324 for index, name in enumerate(field_names):
b312f8f512a9 Backport code cleanup for namedtuples.
Raymond Hettinger <python@rcn.com>
parents: 69541
diff changeset
325 if (not all(c.isalnum() or c=='_' for c in name)
b312f8f512a9 Backport code cleanup for namedtuples.
Raymond Hettinger <python@rcn.com>
parents: 69541
diff changeset
326 or _iskeyword(name)
b312f8f512a9 Backport code cleanup for namedtuples.
Raymond Hettinger <python@rcn.com>
parents: 69541
diff changeset
327 or not name
b312f8f512a9 Backport code cleanup for namedtuples.
Raymond Hettinger <python@rcn.com>
parents: 69541
diff changeset
328 or name[0].isdigit()
b312f8f512a9 Backport code cleanup for namedtuples.
Raymond Hettinger <python@rcn.com>
parents: 69541
diff changeset
329 or name.startswith('_')
51503
e759da93df64 Merged revisions 69466,69480 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 51121
diff changeset
330 or name in seen):
69786
b312f8f512a9 Backport code cleanup for namedtuples.
Raymond Hettinger <python@rcn.com>
parents: 69541
diff changeset
331 field_names[index] = '_%d' % index
51503
e759da93df64 Merged revisions 69466,69480 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 51121
diff changeset
332 seen.add(name)
69786
b312f8f512a9 Backport code cleanup for namedtuples.
Raymond Hettinger <python@rcn.com>
parents: 69541
diff changeset
333 for name in [typename] + field_names:
44187
05da49eb12c1 Merged revisions 59333-59370 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43878
diff changeset
334 if not all(c.isalnum() or c=='_' for c in name):
43734
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43400
diff changeset
335 raise ValueError('Type names and field names can only contain alphanumeric characters and underscores: %r' % name)
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43400
diff changeset
336 if _iskeyword(name):
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43400
diff changeset
337 raise ValueError('Type names and field names cannot be a keyword: %r' % name)
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43400
diff changeset
338 if name[0].isdigit():
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43400
diff changeset
339 raise ValueError('Type names and field names cannot start with a number: %r' % name)
69786
b312f8f512a9 Backport code cleanup for namedtuples.
Raymond Hettinger <python@rcn.com>
parents: 69541
diff changeset
340 seen = set()
43734
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43400
diff changeset
341 for name in field_names:
51503
e759da93df64 Merged revisions 69466,69480 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 51121
diff changeset
342 if name.startswith('_') and not rename:
44310
e70acbf26cda Merged revisions 59488-59511 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44291
diff changeset
343 raise ValueError('Field names cannot start with an underscore: %r' % name)
69786
b312f8f512a9 Backport code cleanup for namedtuples.
Raymond Hettinger <python@rcn.com>
parents: 69541
diff changeset
344 if name in seen:
43734
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43400
diff changeset
345 raise ValueError('Encountered duplicate field name: %r' % name)
69786
b312f8f512a9 Backport code cleanup for namedtuples.
Raymond Hettinger <python@rcn.com>
parents: 69541
diff changeset
346 seen.add(name)
43734
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43400
diff changeset
347
69786
b312f8f512a9 Backport code cleanup for namedtuples.
Raymond Hettinger <python@rcn.com>
parents: 69541
diff changeset
348 # Fill-in the class template
b312f8f512a9 Backport code cleanup for namedtuples.
Raymond Hettinger <python@rcn.com>
parents: 69541
diff changeset
349 class_definition = _class_template.format(
b312f8f512a9 Backport code cleanup for namedtuples.
Raymond Hettinger <python@rcn.com>
parents: 69541
diff changeset
350 typename = typename,
b312f8f512a9 Backport code cleanup for namedtuples.
Raymond Hettinger <python@rcn.com>
parents: 69541
diff changeset
351 field_names = tuple(field_names),
b312f8f512a9 Backport code cleanup for namedtuples.
Raymond Hettinger <python@rcn.com>
parents: 69541
diff changeset
352 num_fields = len(field_names),
b312f8f512a9 Backport code cleanup for namedtuples.
Raymond Hettinger <python@rcn.com>
parents: 69541
diff changeset
353 arg_list = repr(tuple(field_names)).replace("'", "")[1:-1],
b312f8f512a9 Backport code cleanup for namedtuples.
Raymond Hettinger <python@rcn.com>
parents: 69541
diff changeset
354 repr_fmt = ', '.join(_repr_template.format(name=name) for name in field_names),
b312f8f512a9 Backport code cleanup for namedtuples.
Raymond Hettinger <python@rcn.com>
parents: 69541
diff changeset
355 field_defs = '\n'.join(_field_template.format(index=index, name=name)
b312f8f512a9 Backport code cleanup for namedtuples.
Raymond Hettinger <python@rcn.com>
parents: 69541
diff changeset
356 for index, name in enumerate(field_names))
b312f8f512a9 Backport code cleanup for namedtuples.
Raymond Hettinger <python@rcn.com>
parents: 69541
diff changeset
357 )
43734
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43400
diff changeset
358
47557
98827a21e352 Merged revisions 63724,63726,63732,63744,63754-63755,63757-63758,63760,63775,63781-63782,63787,63805-63808,63818-63819,63823-63824 via svnmerge from
Georg Brandl <georg@python.org>
parents: 46927
diff changeset
359 # Execute the template string in a temporary namespace and
98827a21e352 Merged revisions 63724,63726,63732,63744,63754-63755,63757-63758,63760,63775,63781-63782,63787,63805-63808,63818-63819,63823-63824 via svnmerge from
Georg Brandl <georg@python.org>
parents: 46927
diff changeset
360 # support tracing utilities by setting a value for frame.f_globals['__name__']
69786
b312f8f512a9 Backport code cleanup for namedtuples.
Raymond Hettinger <python@rcn.com>
parents: 69541
diff changeset
361 namespace = dict(__name__='namedtuple_%s' % typename)
43734
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43400
diff changeset
362 try:
69786
b312f8f512a9 Backport code cleanup for namedtuples.
Raymond Hettinger <python@rcn.com>
parents: 69541
diff changeset
363 exec(class_definition, namespace)
43734
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43400
diff changeset
364 except SyntaxError as e:
69786
b312f8f512a9 Backport code cleanup for namedtuples.
Raymond Hettinger <python@rcn.com>
parents: 69541
diff changeset
365 raise SyntaxError(e.msg + ':\n\n' + class_definition)
43734
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43400
diff changeset
366 result = namespace[typename]
69786
b312f8f512a9 Backport code cleanup for namedtuples.
Raymond Hettinger <python@rcn.com>
parents: 69541
diff changeset
367 if verbose:
b312f8f512a9 Backport code cleanup for namedtuples.
Raymond Hettinger <python@rcn.com>
parents: 69541
diff changeset
368 print(class_definition)
43734
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43400
diff changeset
369
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43400
diff changeset
370 # For pickling to work, the __module__ variable needs to be set to the frame
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43400
diff changeset
371 # where the named tuple is created. Bypass this step in enviroments where
53920
7a9e081ece4c Merged revisions 70768,71657,71721,71729,71794,71976,72036-72037,72079,72085,72131-72134,72191,72197-72198,72219,72221,72225,72303,72434,72467,72476 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 52986
diff changeset
372 # sys._getframe is not defined (Jython for example) or sys._getframe is not
7a9e081ece4c Merged revisions 70768,71657,71721,71729,71794,71976,72036-72037,72079,72085,72131-72134,72191,72197-72198,72219,72221,72225,72303,72434,72467,72476 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 52986
diff changeset
373 # defined for arguments greater than 0 (IronPython).
7a9e081ece4c Merged revisions 70768,71657,71721,71729,71794,71976,72036-72037,72079,72085,72131-72134,72191,72197-72198,72219,72221,72225,72303,72434,72467,72476 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 52986
diff changeset
374 try:
51111
7ed9ccc0b519 Issue 5021: doctest.testfile should set __name__
Raymond Hettinger <python@rcn.com>
parents: 50973
diff changeset
375 result.__module__ = _sys._getframe(1).f_globals.get('__name__', '__main__')
53920
7a9e081ece4c Merged revisions 70768,71657,71721,71729,71794,71976,72036-72037,72079,72085,72131-72134,72191,72197-72198,72219,72221,72225,72303,72434,72467,72476 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 52986
diff changeset
376 except (AttributeError, ValueError):
7a9e081ece4c Merged revisions 70768,71657,71721,71729,71794,71976,72036-72037,72079,72085,72131-72134,72191,72197-72198,72219,72221,72225,72303,72434,72467,72476 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 52986
diff changeset
377 pass
43734
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43400
diff changeset
378
41824
24b60622b944 Merged revisions 55407-55513 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
379 return result
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
diff changeset
380
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
diff changeset
381
50793
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
382 ########################################################################
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
383 ### Counter
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
384 ########################################################################
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
385
66920
40125cddafb9 Issue 10667: Fast path for collections.Counter
Raymond Hettinger <python@rcn.com>
parents: 64783
diff changeset
386 def _count_elements(mapping, iterable):
40125cddafb9 Issue 10667: Fast path for collections.Counter
Raymond Hettinger <python@rcn.com>
parents: 64783
diff changeset
387 'Tally elements from the iterable.'
40125cddafb9 Issue 10667: Fast path for collections.Counter
Raymond Hettinger <python@rcn.com>
parents: 64783
diff changeset
388 mapping_get = mapping.get
40125cddafb9 Issue 10667: Fast path for collections.Counter
Raymond Hettinger <python@rcn.com>
parents: 64783
diff changeset
389 for elem in iterable:
40125cddafb9 Issue 10667: Fast path for collections.Counter
Raymond Hettinger <python@rcn.com>
parents: 64783
diff changeset
390 mapping[elem] = mapping_get(elem, 0) + 1
40125cddafb9 Issue 10667: Fast path for collections.Counter
Raymond Hettinger <python@rcn.com>
parents: 64783
diff changeset
391
40125cddafb9 Issue 10667: Fast path for collections.Counter
Raymond Hettinger <python@rcn.com>
parents: 64783
diff changeset
392 try: # Load C helper function if available
40125cddafb9 Issue 10667: Fast path for collections.Counter
Raymond Hettinger <python@rcn.com>
parents: 64783
diff changeset
393 from _collections import _count_elements
40125cddafb9 Issue 10667: Fast path for collections.Counter
Raymond Hettinger <python@rcn.com>
parents: 64783
diff changeset
394 except ImportError:
40125cddafb9 Issue 10667: Fast path for collections.Counter
Raymond Hettinger <python@rcn.com>
parents: 64783
diff changeset
395 pass
40125cddafb9 Issue 10667: Fast path for collections.Counter
Raymond Hettinger <python@rcn.com>
parents: 64783
diff changeset
396
50793
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
397 class Counter(dict):
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
398 '''Dict subclass for counting hashable items. Sometimes called a bag
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
399 or multiset. Elements are stored as dictionary keys and their counts
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
400 are stored as dictionary values.
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
401
67290
e8bcc7dbab7d Backport r87672 and r87615, improving tests, using super() instead of direct parent references, and using __reduce__ method for pickling.
Raymond Hettinger <python@rcn.com>
parents: 64665
diff changeset
402 >>> c = Counter('abcdeabcdabcaba') # count elements from a string
50793
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
403
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
404 >>> c.most_common(3) # three most common elements
67290
e8bcc7dbab7d Backport r87672 and r87615, improving tests, using super() instead of direct parent references, and using __reduce__ method for pickling.
Raymond Hettinger <python@rcn.com>
parents: 64665
diff changeset
405 [('a', 5), ('b', 4), ('c', 3)]
50793
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
406 >>> sorted(c) # list all unique elements
67290
e8bcc7dbab7d Backport r87672 and r87615, improving tests, using super() instead of direct parent references, and using __reduce__ method for pickling.
Raymond Hettinger <python@rcn.com>
parents: 64665
diff changeset
407 ['a', 'b', 'c', 'd', 'e']
50793
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
408 >>> ''.join(sorted(c.elements())) # list elements with repetitions
67290
e8bcc7dbab7d Backport r87672 and r87615, improving tests, using super() instead of direct parent references, and using __reduce__ method for pickling.
Raymond Hettinger <python@rcn.com>
parents: 64665
diff changeset
409 'aaaaabbbbcccdde'
50793
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
410 >>> sum(c.values()) # total of all counts
67290
e8bcc7dbab7d Backport r87672 and r87615, improving tests, using super() instead of direct parent references, and using __reduce__ method for pickling.
Raymond Hettinger <python@rcn.com>
parents: 64665
diff changeset
411 15
50793
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
412
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
413 >>> c['a'] # count of letter 'a'
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
414 5
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
415 >>> for elem in 'shazam': # update counts from an iterable
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
416 ... c[elem] += 1 # by adding 1 to each element's count
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
417 >>> c['a'] # now there are seven 'a'
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
418 7
67290
e8bcc7dbab7d Backport r87672 and r87615, improving tests, using super() instead of direct parent references, and using __reduce__ method for pickling.
Raymond Hettinger <python@rcn.com>
parents: 64665
diff changeset
419 >>> del c['b'] # remove all 'b'
e8bcc7dbab7d Backport r87672 and r87615, improving tests, using super() instead of direct parent references, and using __reduce__ method for pickling.
Raymond Hettinger <python@rcn.com>
parents: 64665
diff changeset
420 >>> c['b'] # now there are zero 'b'
50793
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
421 0
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
422
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
423 >>> d = Counter('simsalabim') # make another counter
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
424 >>> c.update(d) # add in the second counter
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
425 >>> c['a'] # now there are nine 'a'
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
426 9
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
427
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
428 >>> c.clear() # empty the counter
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
429 >>> c
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
430 Counter()
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
431
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
432 Note: If a count is set to zero or reduced to zero, it will remain
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
433 in the counter until the entry is deleted or the counter is cleared:
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
434
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
435 >>> c = Counter('aaabbc')
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
436 >>> c['b'] -= 2 # reduce the count of 'b' by two
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
437 >>> c.most_common() # 'b' is still in, but its count is zero
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
438 [('a', 3), ('c', 1), ('b', 0)]
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
439
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
440 '''
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
441 # References:
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
442 # http://en.wikipedia.org/wiki/Multiset
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
443 # http://www.gnu.org/software/smalltalk/manual-base/html_node/Bag.html
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
444 # http://www.demo2s.com/Tutorial/Cpp/0380__set-multiset/Catalog0380__set-multiset.htm
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
445 # http://code.activestate.com/recipes/259174/
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
446 # Knuth, TAOCP Vol. II section 4.6.3
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
447
50925
6a6b590912fe Forward port r68792 and r68789 putting Counter in __all__ and adding Counter buildouts.
Raymond Hettinger <python@rcn.com>
parents: 50793
diff changeset
448 def __init__(self, iterable=None, **kwds):
50793
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
449 '''Create a new, empty Counter object. And if given, count elements
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
450 from an input iterable. Or, initialize the count from another mapping
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
451 of elements to their counts.
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
452
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
453 >>> c = Counter() # a new, empty counter
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
454 >>> c = Counter('gallahad') # a new counter from an iterable
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
455 >>> c = Counter({'a': 4, 'b': 2}) # a new counter from a mapping
50925
6a6b590912fe Forward port r68792 and r68789 putting Counter in __all__ and adding Counter buildouts.
Raymond Hettinger <python@rcn.com>
parents: 50793
diff changeset
456 >>> c = Counter(a=4, b=2) # a new counter from keyword args
50793
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
457
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
458 '''
67290
e8bcc7dbab7d Backport r87672 and r87615, improving tests, using super() instead of direct parent references, and using __reduce__ method for pickling.
Raymond Hettinger <python@rcn.com>
parents: 64665
diff changeset
459 super().__init__()
50925
6a6b590912fe Forward port r68792 and r68789 putting Counter in __all__ and adding Counter buildouts.
Raymond Hettinger <python@rcn.com>
parents: 50793
diff changeset
460 self.update(iterable, **kwds)
50793
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
461
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
462 def __missing__(self, key):
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
463 'The count of elements not in the Counter is zero.'
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
464 # Needed so that self[missing_item] does not raise KeyError
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
465 return 0
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
466
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
467 def most_common(self, n=None):
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
468 '''List the n most common elements and their counts from the most
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
469 common to the least. If n is None, then list all element counts.
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
470
67290
e8bcc7dbab7d Backport r87672 and r87615, improving tests, using super() instead of direct parent references, and using __reduce__ method for pickling.
Raymond Hettinger <python@rcn.com>
parents: 64665
diff changeset
471 >>> Counter('abcdeabcdabcaba').most_common(3)
e8bcc7dbab7d Backport r87672 and r87615, improving tests, using super() instead of direct parent references, and using __reduce__ method for pickling.
Raymond Hettinger <python@rcn.com>
parents: 64665
diff changeset
472 [('a', 5), ('b', 4), ('c', 3)]
50793
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
473
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
474 '''
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
475 # Emulate Bag.sortedByCount from Smalltalk
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
476 if n is None:
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
477 return sorted(self.items(), key=_itemgetter(1), reverse=True)
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
478 return _heapq.nlargest(n, self.items(), key=_itemgetter(1))
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
479
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
480 def elements(self):
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
481 '''Iterator over elements repeating each as many times as its count.
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
482
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
483 >>> c = Counter('ABCABC')
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
484 >>> sorted(c.elements())
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
485 ['A', 'A', 'B', 'B', 'C', 'C']
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
486
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
487 # Knuth's example for prime factors of 1836: 2**2 * 3**3 * 17**1
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
488 >>> prime_factors = Counter({2: 2, 3: 3, 17: 1})
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
489 >>> product = 1
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
490 >>> for factor in prime_factors.elements(): # loop over factors
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
491 ... product *= factor # and multiply them
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
492 >>> product
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
493 1836
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
494
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
495 Note, if an element's count has been set to zero or is a negative
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
496 number, elements() will ignore it.
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
497
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
498 '''
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
499 # Emulate Bag.do from Smalltalk and Multiset.begin from C++.
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
500 return _chain.from_iterable(_starmap(_repeat, self.items()))
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
501
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
502 # Override dict methods where necessary
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
503
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
504 @classmethod
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
505 def fromkeys(cls, iterable, v=None):
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
506 # There is no equivalent method for counters because setting v=1
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
507 # means that no element can have a count greater than one.
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
508 raise NotImplementedError(
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
509 'Counter.fromkeys() is undefined. Use Counter(iterable) instead.')
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
510
50925
6a6b590912fe Forward port r68792 and r68789 putting Counter in __all__ and adding Counter buildouts.
Raymond Hettinger <python@rcn.com>
parents: 50793
diff changeset
511 def update(self, iterable=None, **kwds):
50793
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
512 '''Like dict.update() but add counts instead of replacing them.
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
513
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
514 Source can be an iterable, a dictionary, or another Counter instance.
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
515
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
516 >>> c = Counter('which')
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
517 >>> c.update('witch') # add elements from another iterable
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
518 >>> d = Counter('watch')
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
519 >>> c.update(d) # add elements from another counter
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
520 >>> c['h'] # four 'h' in which, witch, and watch
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
521 4
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
522
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
523 '''
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
524 # The regular dict.update() operation makes no sense here because the
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
525 # replace behavior results in the some of original untouched counts
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
526 # being mixed-in with all of the other counts for a mismash that
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
527 # doesn't have a straight-forward interpretation in most counting
50925
6a6b590912fe Forward port r68792 and r68789 putting Counter in __all__ and adding Counter buildouts.
Raymond Hettinger <python@rcn.com>
parents: 50793
diff changeset
528 # contexts. Instead, we implement straight-addition. Both the inputs
6a6b590912fe Forward port r68792 and r68789 putting Counter in __all__ and adding Counter buildouts.
Raymond Hettinger <python@rcn.com>
parents: 50793
diff changeset
529 # and outputs are allowed to contain zero and negative counts.
50793
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
530
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
531 if iterable is not None:
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
532 if isinstance(iterable, Mapping):
50973
f5d7d060911a Update comments and add an optimized path for Counter.update().
Raymond Hettinger <python@rcn.com>
parents: 50965
diff changeset
533 if self:
54985
932033c304ef Issue 6370: Performance issue with collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 54362
diff changeset
534 self_get = self.get
50973
f5d7d060911a Update comments and add an optimized path for Counter.update().
Raymond Hettinger <python@rcn.com>
parents: 50965
diff changeset
535 for elem, count in iterable.items():
54985
932033c304ef Issue 6370: Performance issue with collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 54362
diff changeset
536 self[elem] = count + self_get(elem, 0)
50973
f5d7d060911a Update comments and add an optimized path for Counter.update().
Raymond Hettinger <python@rcn.com>
parents: 50965
diff changeset
537 else:
67290
e8bcc7dbab7d Backport r87672 and r87615, improving tests, using super() instead of direct parent references, and using __reduce__ method for pickling.
Raymond Hettinger <python@rcn.com>
parents: 64665
diff changeset
538 super().update(iterable) # fast path when counter is empty
50793
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
539 else:
66920
40125cddafb9 Issue 10667: Fast path for collections.Counter
Raymond Hettinger <python@rcn.com>
parents: 64783
diff changeset
540 _count_elements(self, iterable)
50925
6a6b590912fe Forward port r68792 and r68789 putting Counter in __all__ and adding Counter buildouts.
Raymond Hettinger <python@rcn.com>
parents: 50793
diff changeset
541 if kwds:
6a6b590912fe Forward port r68792 and r68789 putting Counter in __all__ and adding Counter buildouts.
Raymond Hettinger <python@rcn.com>
parents: 50793
diff changeset
542 self.update(kwds)
50793
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
543
60226
7ca6a3431f9e Add a subtract() method to collections.Counter()
Raymond Hettinger <python@rcn.com>
parents: 60175
diff changeset
544 def subtract(self, iterable=None, **kwds):
7ca6a3431f9e Add a subtract() method to collections.Counter()
Raymond Hettinger <python@rcn.com>
parents: 60175
diff changeset
545 '''Like dict.update() but subtracts counts instead of replacing them.
7ca6a3431f9e Add a subtract() method to collections.Counter()
Raymond Hettinger <python@rcn.com>
parents: 60175
diff changeset
546 Counts can be reduced below zero. Both the inputs and outputs are
7ca6a3431f9e Add a subtract() method to collections.Counter()
Raymond Hettinger <python@rcn.com>
parents: 60175
diff changeset
547 allowed to contain zero and negative counts.
7ca6a3431f9e Add a subtract() method to collections.Counter()
Raymond Hettinger <python@rcn.com>
parents: 60175
diff changeset
548
7ca6a3431f9e Add a subtract() method to collections.Counter()
Raymond Hettinger <python@rcn.com>
parents: 60175
diff changeset
549 Source can be an iterable, a dictionary, or another Counter instance.
7ca6a3431f9e Add a subtract() method to collections.Counter()
Raymond Hettinger <python@rcn.com>
parents: 60175
diff changeset
550
7ca6a3431f9e Add a subtract() method to collections.Counter()
Raymond Hettinger <python@rcn.com>
parents: 60175
diff changeset
551 >>> c = Counter('which')
7ca6a3431f9e Add a subtract() method to collections.Counter()
Raymond Hettinger <python@rcn.com>
parents: 60175
diff changeset
552 >>> c.subtract('witch') # subtract elements from another iterable
7ca6a3431f9e Add a subtract() method to collections.Counter()
Raymond Hettinger <python@rcn.com>
parents: 60175
diff changeset
553 >>> c.subtract(Counter('watch')) # subtract elements from another counter
7ca6a3431f9e Add a subtract() method to collections.Counter()
Raymond Hettinger <python@rcn.com>
parents: 60175
diff changeset
554 >>> c['h'] # 2 in which, minus 1 in witch, minus 1 in watch
7ca6a3431f9e Add a subtract() method to collections.Counter()
Raymond Hettinger <python@rcn.com>
parents: 60175
diff changeset
555 0
7ca6a3431f9e Add a subtract() method to collections.Counter()
Raymond Hettinger <python@rcn.com>
parents: 60175
diff changeset
556 >>> c['w'] # 1 in which, minus 1 in witch, minus 1 in watch
7ca6a3431f9e Add a subtract() method to collections.Counter()
Raymond Hettinger <python@rcn.com>
parents: 60175
diff changeset
557 -1
7ca6a3431f9e Add a subtract() method to collections.Counter()
Raymond Hettinger <python@rcn.com>
parents: 60175
diff changeset
558
7ca6a3431f9e Add a subtract() method to collections.Counter()
Raymond Hettinger <python@rcn.com>
parents: 60175
diff changeset
559 '''
7ca6a3431f9e Add a subtract() method to collections.Counter()
Raymond Hettinger <python@rcn.com>
parents: 60175
diff changeset
560 if iterable is not None:
60512
9f9cb1c57f2a Minor factoring
Raymond Hettinger <python@rcn.com>
parents: 60226
diff changeset
561 self_get = self.get
60226
7ca6a3431f9e Add a subtract() method to collections.Counter()
Raymond Hettinger <python@rcn.com>
parents: 60175
diff changeset
562 if isinstance(iterable, Mapping):
7ca6a3431f9e Add a subtract() method to collections.Counter()
Raymond Hettinger <python@rcn.com>
parents: 60175
diff changeset
563 for elem, count in iterable.items():
7ca6a3431f9e Add a subtract() method to collections.Counter()
Raymond Hettinger <python@rcn.com>
parents: 60175
diff changeset
564 self[elem] = self_get(elem, 0) - count
7ca6a3431f9e Add a subtract() method to collections.Counter()
Raymond Hettinger <python@rcn.com>
parents: 60175
diff changeset
565 else:
7ca6a3431f9e Add a subtract() method to collections.Counter()
Raymond Hettinger <python@rcn.com>
parents: 60175
diff changeset
566 for elem in iterable:
7ca6a3431f9e Add a subtract() method to collections.Counter()
Raymond Hettinger <python@rcn.com>
parents: 60175
diff changeset
567 self[elem] = self_get(elem, 0) - 1
7ca6a3431f9e Add a subtract() method to collections.Counter()
Raymond Hettinger <python@rcn.com>
parents: 60175
diff changeset
568 if kwds:
7ca6a3431f9e Add a subtract() method to collections.Counter()
Raymond Hettinger <python@rcn.com>
parents: 60175
diff changeset
569 self.subtract(kwds)
7ca6a3431f9e Add a subtract() method to collections.Counter()
Raymond Hettinger <python@rcn.com>
parents: 60175
diff changeset
570
50793
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
571 def copy(self):
69379
a164f67d27be Fix minor subclassing issue with collections.Counter
Raymond Hettinger <python@rcn.com>
parents: 67359
diff changeset
572 'Return a shallow copy.'
a164f67d27be Fix minor subclassing issue with collections.Counter
Raymond Hettinger <python@rcn.com>
parents: 67359
diff changeset
573 return self.__class__(self)
50793
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
574
67290
e8bcc7dbab7d Backport r87672 and r87615, improving tests, using super() instead of direct parent references, and using __reduce__ method for pickling.
Raymond Hettinger <python@rcn.com>
parents: 64665
diff changeset
575 def __reduce__(self):
e8bcc7dbab7d Backport r87672 and r87615, improving tests, using super() instead of direct parent references, and using __reduce__ method for pickling.
Raymond Hettinger <python@rcn.com>
parents: 64665
diff changeset
576 return self.__class__, (dict(self),)
e8bcc7dbab7d Backport r87672 and r87615, improving tests, using super() instead of direct parent references, and using __reduce__ method for pickling.
Raymond Hettinger <python@rcn.com>
parents: 64665
diff changeset
577
50925
6a6b590912fe Forward port r68792 and r68789 putting Counter in __all__ and adding Counter buildouts.
Raymond Hettinger <python@rcn.com>
parents: 50793
diff changeset
578 def __delitem__(self, elem):
6a6b590912fe Forward port r68792 and r68789 putting Counter in __all__ and adding Counter buildouts.
Raymond Hettinger <python@rcn.com>
parents: 50793
diff changeset
579 'Like dict.__delitem__() but does not raise KeyError for missing values.'
6a6b590912fe Forward port r68792 and r68789 putting Counter in __all__ and adding Counter buildouts.
Raymond Hettinger <python@rcn.com>
parents: 50793
diff changeset
580 if elem in self:
67290
e8bcc7dbab7d Backport r87672 and r87615, improving tests, using super() instead of direct parent references, and using __reduce__ method for pickling.
Raymond Hettinger <python@rcn.com>
parents: 64665
diff changeset
581 super().__delitem__(elem)
50925
6a6b590912fe Forward port r68792 and r68789 putting Counter in __all__ and adding Counter buildouts.
Raymond Hettinger <python@rcn.com>
parents: 50793
diff changeset
582
50793
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
583 def __repr__(self):
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
584 if not self:
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
585 return '%s()' % self.__class__.__name__
73386
970ec22ab368 Improve Counter.__repr__() to not fail with unorderable values
Raymond Hettinger <python@rcn.com>
parents: 70609
diff changeset
586 try:
970ec22ab368 Improve Counter.__repr__() to not fail with unorderable values
Raymond Hettinger <python@rcn.com>
parents: 70609
diff changeset
587 items = ', '.join(map('%r: %r'.__mod__, self.most_common()))
970ec22ab368 Improve Counter.__repr__() to not fail with unorderable values
Raymond Hettinger <python@rcn.com>
parents: 70609
diff changeset
588 return '%s({%s})' % (self.__class__.__name__, items)
970ec22ab368 Improve Counter.__repr__() to not fail with unorderable values
Raymond Hettinger <python@rcn.com>
parents: 70609
diff changeset
589 except TypeError:
970ec22ab368 Improve Counter.__repr__() to not fail with unorderable values
Raymond Hettinger <python@rcn.com>
parents: 70609
diff changeset
590 # handle case where values are not orderable
970ec22ab368 Improve Counter.__repr__() to not fail with unorderable values
Raymond Hettinger <python@rcn.com>
parents: 70609
diff changeset
591 return '{0}({1!r})'.format(self.__class__.__name__, dict(self))
50793
0e05e078bced Issue #1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents: 50733
diff changeset
592
50925
6a6b590912fe Forward port r68792 and r68789 putting Counter in __all__ and adding Counter buildouts.
Raymond Hettinger <python@rcn.com>
parents: 50793
diff changeset
593 # Multiset-style mathematical operations discussed in:
6a6b590912fe Forward port r68792 and r68789 putting Counter in __all__ and adding Counter buildouts.
Raymond Hettinger <python@rcn.com>
parents: 50793
diff changeset
594 # Knuth TAOCP Volume II section 4.6.3 exercise 19
6a6b590912fe Forward port r68792 and r68789 putting Counter in __all__ and adding Counter buildouts.
Raymond Hettinger <python@rcn.com>
parents: 50793
diff changeset
595 # and at http://en.wikipedia.org/wiki/Multiset
6a6b590912fe Forward port r68792 and r68789 putting Counter in __all__ and adding Counter buildouts.
Raymond Hettinger <python@rcn.com>
parents: 50793
diff changeset
596 #
6a6b590912fe Forward port r68792 and r68789 putting Counter in __all__ and adding Counter buildouts.
Raymond Hettinger <python@rcn.com>
parents: 50793
diff changeset
597 # Outputs guaranteed to only include positive counts.
6a6b590912fe Forward port r68792 and r68789 putting Counter in __all__ and adding Counter buildouts.
Raymond Hettinger <python@rcn.com>
parents: 50793
diff changeset
598 #
6a6b590912fe Forward port r68792 and r68789 putting Counter in __all__ and adding Counter buildouts.
Raymond Hettinger <python@rcn.com>
parents: 50793
diff changeset
599 # To strip negative and zero counts, add-in an empty counter:
6a6b590912fe Forward port r68792 and r68789 putting Counter in __all__ and adding Counter buildouts.
Raymond Hettinger <python@rcn.com>
parents: 50793
diff changeset
600 # c += Counter()
6a6b590912fe Forward port r68792 and r68789 putting Counter in __all__ and adding Counter buildouts.
Raymond Hettinger <python@rcn.com>
parents: 50793
diff changeset
601
6a6b590912fe Forward port r68792 and r68789 putting Counter in __all__ and adding Counter buildouts.
Raymond Hettinger <python@rcn.com>
parents: 50793
diff changeset
602 def __add__(self, other):
6a6b590912fe Forward port r68792 and r68789 putting Counter in __all__ and adding Counter buildouts.
Raymond Hettinger <python@rcn.com>
parents: 50793
diff changeset
603 '''Add counts from two counters.
6a6b590912fe Forward port r68792 and r68789 putting Counter in __all__ and adding Counter buildouts.
Raymond Hettinger <python@rcn.com>
parents: 50793
diff changeset
604
6a6b590912fe Forward port r68792 and r68789 putting Counter in __all__ and adding Counter buildouts.
Raymond Hettinger <python@rcn.com>
parents: 50793
diff changeset
605 >>> Counter('abbb') + Counter('bcc')
6a6b590912fe Forward port r68792 and r68789 putting Counter in __all__ and adding Counter buildouts.
Raymond Hettinger <python@rcn.com>
parents: 50793
diff changeset
606 Counter({'b': 4, 'c': 2, 'a': 1})
6a6b590912fe Forward port r68792 and r68789 putting Counter in __all__ and adding Counter buildouts.
Raymond Hettinger <python@rcn.com>
parents: 50793
diff changeset
607
6a6b590912fe Forward port r68792 and r68789 putting Counter in __all__ and adding Counter buildouts.
Raymond Hettinger <python@rcn.com>
parents: 50793
diff changeset
608 '''
6a6b590912fe Forward port r68792 and r68789 putting Counter in __all__ and adding Counter buildouts.
Raymond Hettinger <python@rcn.com>
parents: 50793
diff changeset
609 if not isinstance(other, Counter):
6a6b590912fe Forward port r68792 and r68789 putting Counter in __all__ and adding Counter buildouts.
Raymond Hettinger <python@rcn.com>
parents: 50793
diff changeset
610 return NotImplemented
6a6b590912fe Forward port r68792 and r68789 putting Counter in __all__ and adding Counter buildouts.
Raymond Hettinger <python@rcn.com>
parents: 50793
diff changeset
611 result = Counter()
69412
49e756c8c08b Rework multiset methods to use less memory and to make fewer calls to __hash__.
Raymond Hettinger <python@rcn.com>
parents: 69379
diff changeset
612 for elem, count in self.items():
49e756c8c08b Rework multiset methods to use less memory and to make fewer calls to __hash__.
Raymond Hettinger <python@rcn.com>
parents: 69379
diff changeset
613 newcount = count + other[elem]
50925
6a6b590912fe Forward port r68792 and r68789 putting Counter in __all__ and adding Counter buildouts.
Raymond Hettinger <python@rcn.com>
parents: 50793
diff changeset
614 if newcount > 0:
6a6b590912fe Forward port r68792 and r68789 putting Counter in __all__ and adding Counter buildouts.
Raymond Hettinger <python@rcn.com>
parents: 50793
diff changeset
615 result[elem] = newcount
69412
49e756c8c08b Rework multiset methods to use less memory and to make fewer calls to __hash__.
Raymond Hettinger <python@rcn.com>
parents: 69379
diff changeset
616 for elem, count in other.items():
49e756c8c08b Rework multiset methods to use less memory and to make fewer calls to __hash__.
Raymond Hettinger <python@rcn.com>
parents: 69379
diff changeset
617 if elem not in self and count > 0:
49e756c8c08b Rework multiset methods to use less memory and to make fewer calls to __hash__.
Raymond Hettinger <python@rcn.com>
parents: 69379
diff changeset
618 result[elem] = count
50925
6a6b590912fe Forward port r68792 and r68789 putting Counter in __all__ and adding Counter buildouts.
Raymond Hettinger <python@rcn.com>
parents: 50793
diff changeset
619 return result
6a6b590912fe Forward port r68792 and r68789 putting Counter in __all__ and adding Counter buildouts.
Raymond Hettinger <python@rcn.com>
parents: 50793
diff changeset
620
6a6b590912fe Forward port r68792 and r68789 putting Counter in __all__ and adding Counter buildouts.
Raymond Hettinger <python@rcn.com>
parents: 50793
diff changeset
621 def __sub__(self, other):
6a6b590912fe Forward port r68792 and r68789 putting Counter in __all__ and adding Counter buildouts.
Raymond Hettinger <python@rcn.com>
parents: 50793
diff changeset
622 ''' Subtract count, but keep only results with positive counts.
6a6b590912fe Forward port r68792 and r68789 putting Counter in __all__ and adding Counter buildouts.
Raymond Hettinger <python@rcn.com>
parents: 50793
diff changeset
623
6a6b590912fe Forward port r68792 and r68789 putting Counter in __all__ and adding Counter buildouts.
Raymond Hettinger <python@rcn.com>
parents: 50793
diff changeset
624 >>> Counter('abbbc') - Counter('bccd')
6a6b590912fe Forward port r68792 and r68789 putting Counter in __all__ and adding Counter buildouts.
Raymond Hettinger <python@rcn.com>
parents: 50793
diff changeset
625 Counter({'b': 2, 'a': 1})
6a6b590912fe Forward port r68792 and r68789 putting Counter in __all__ and adding Counter buildouts.
Raymond Hettinger <python@rcn.com>
parents: 50793
diff changeset
626
6a6b590912fe Forward port r68792 and r68789 putting Counter in __all__ and adding Counter buildouts.
Raymond Hettinger <python@rcn.com>
parents: 50793
diff changeset
627 '''
6a6b590912fe Forward port r68792 and r68789 putting Counter in __all__ and adding Counter buildouts.
Raymond Hettinger <python@rcn.com>
parents: 50793
diff changeset
628 if not isinstance(other, Counter):
6a6b590912fe Forward port r68792 and r68789 putting Counter in __all__ and adding Counter buildouts.
Raymond Hettinger <python@rcn.com>
parents: 50793
diff changeset
629 return NotImplemented
6a6b590912fe Forward port r68792 and r68789 putting Counter in __all__ and adding Counter buildouts.
Raymond Hettinger <python@rcn.com>
parents: 50793
diff changeset
630 result = Counter()
69412
49e756c8c08b Rework multiset methods to use less memory and to make fewer calls to __hash__.
Raymond Hettinger <python@rcn.com>
parents: 69379
diff changeset
631 for elem, count in self.items():
49e756c8c08b Rework multiset methods to use less memory and to make fewer calls to __hash__.
Raymond Hettinger <python@rcn.com>
parents: 69379
diff changeset
632 newcount = count - other[elem]
50925
6a6b590912fe Forward port r68792 and r68789 putting Counter in __all__ and adding Counter buildouts.
Raymond Hettinger <python@rcn.com>
parents: 50793
diff changeset
633 if newcount > 0:
6a6b590912fe Forward port r68792 and r68789 putting Counter in __all__ and adding Counter buildouts.
Raymond Hettinger <python@rcn.com>
parents: 50793
diff changeset
634 result[elem] = newcount
69412
49e756c8c08b Rework multiset methods to use less memory and to make fewer calls to __hash__.
Raymond Hettinger <python@rcn.com>
parents: 69379
diff changeset
635 for elem, count in other.items():
49e756c8c08b Rework multiset methods to use less memory and to make fewer calls to __hash__.
Raymond Hettinger <python@rcn.com>
parents: 69379
diff changeset
636 if elem not in self and count < 0:
49e756c8c08b Rework multiset methods to use less memory and to make fewer calls to __hash__.
Raymond Hettinger <python@rcn.com>
parents: 69379
diff changeset
637 result[elem] = 0 - count
50925
6a6b590912fe Forward port r68792 and r68789 putting Counter in __all__ and adding Counter buildouts.
Raymond Hettinger <python@rcn.com>
parents: 50793
diff changeset
638 return result
6a6b590912fe Forward port r68792 and r68789 putting Counter in __all__ and adding Counter buildouts.
Raymond Hettinger <python@rcn.com>
parents: 50793
diff changeset
639
6a6b590912fe Forward port r68792 and r68789 putting Counter in __all__ and adding Counter buildouts.
Raymond Hettinger <python@rcn.com>
parents: 50793
diff changeset
640 def __or__(self, other):
6a6b590912fe Forward port r68792 and r68789 putting Counter in __all__ and adding Counter buildouts.
Raymond Hettinger <python@rcn.com>
parents: 50793
diff changeset
641 '''Union is the maximum of value in either of the input counters.
6a6b590912fe Forward port r68792 and r68789 putting Counter in __all__ and adding Counter buildouts.
Raymond Hettinger <python@rcn.com>
parents: 50793
diff changeset
642
6a6b590912fe Forward port r68792 and r68789 putting Counter in __all__ and adding Counter buildouts.
Raymond Hettinger <python@rcn.com>
parents: 50793
diff changeset
643 >>> Counter('abbb') | Counter('bcc')
6a6b590912fe Forward port r68792 and r68789 putting Counter in __all__ and adding Counter buildouts.
Raymond Hettinger <python@rcn.com>
parents: 50793
diff changeset
644 Counter({'b': 3, 'c': 2, 'a': 1})
6a6b590912fe Forward port r68792 and r68789 putting Counter in __all__ and adding Counter buildouts.
Raymond Hettinger <python@rcn.com>
parents: 50793
diff changeset
645
6a6b590912fe Forward port r68792 and r68789 putting Counter in __all__ and adding Counter buildouts.
Raymond Hettinger <python@rcn.com>
parents: 50793
diff changeset
646 '''
6a6b590912fe Forward port r68792 and r68789 putting Counter in __all__ and adding Counter buildouts.
Raymond Hettinger <python@rcn.com>
parents: 50793
diff changeset
647 if not isinstance(other, Counter):
6a6b590912fe Forward port r68792 and r68789 putting Counter in __all__ and adding Counter buildouts.
Raymond Hettinger <python@rcn.com>
parents: 50793
diff changeset
648 return NotImplemented
6a6b590912fe Forward port r68792 and r68789 putting Counter in __all__ and adding Counter buildouts.
Raymond Hettinger <python@rcn.com>
parents: 50793
diff changeset
649 result = Counter()
69412
49e756c8c08b Rework multiset methods to use less memory and to make fewer calls to __hash__.
Raymond Hettinger <python@rcn.com>
parents: 69379
diff changeset
650 for elem, count in self.items():
49e756c8c08b Rework multiset methods to use less memory and to make fewer calls to __hash__.
Raymond Hettinger <python@rcn.com>
parents: 69379
diff changeset
651 other_count = other[elem]
49e756c8c08b Rework multiset methods to use less memory and to make fewer calls to __hash__.
Raymond Hettinger <python@rcn.com>
parents: 69379
diff changeset
652 newcount = other_count if count < other_count else count
50925
6a6b590912fe Forward port r68792 and r68789 putting Counter in __all__ and adding Counter buildouts.
Raymond Hettinger <python@rcn.com>
parents: 50793
diff changeset
653 if newcount > 0:
6a6b590912fe Forward port r68792 and r68789 putting Counter in __all__ and adding Counter buildouts.
Raymond Hettinger <python@rcn.com>
parents: 50793
diff changeset
654 result[elem] = newcount
69412
49e756c8c08b Rework multiset methods to use less memory and to make fewer calls to __hash__.
Raymond Hettinger <python@rcn.com>
parents: 69379
diff changeset
655 for elem, count in other.items():
49e756c8c08b Rework multiset methods to use less memory and to make fewer calls to __hash__.
Raymond Hettinger <python@rcn.com>
parents: 69379
diff changeset
656 if elem not in self and count > 0:
49e756c8c08b Rework multiset methods to use less memory and to make fewer calls to __hash__.
Raymond Hettinger <python@rcn.com>
parents: 69379
diff changeset
657 result[elem] = count
50925
6a6b590912fe Forward port r68792 and r68789 putting Counter in __all__ and adding Counter buildouts.
Raymond Hettinger <python@rcn.com>
parents: 50793
diff changeset
658 return result
6a6b590912fe Forward port r68792 and r68789 putting Counter in __all__ and adding Counter buildouts.
Raymond Hettinger <python@rcn.com>
parents: 50793
diff changeset
659
6a6b590912fe Forward port r68792 and r68789 putting Counter in __all__ and adding Counter buildouts.
Raymond Hettinger <python@rcn.com>
parents: 50793
diff changeset
660 def __and__(self, other):
6a6b590912fe Forward port r68792 and r68789 putting Counter in __all__ and adding Counter buildouts.
Raymond Hettinger <python@rcn.com>
parents: 50793
diff changeset
661 ''' Intersection is the minimum of corresponding counts.
6a6b590912fe Forward port r68792 and r68789 putting Counter in __all__ and adding Counter buildouts.
Raymond Hettinger <python@rcn.com>
parents: 50793
diff changeset
662
6a6b590912fe Forward port r68792 and r68789 putting Counter in __all__ and adding Counter buildouts.
Raymond Hettinger <python@rcn.com>
parents: 50793
diff changeset
663 >>> Counter('abbb') & Counter('bcc')
6a6b590912fe Forward port r68792 and r68789 putting Counter in __all__ and adding Counter buildouts.
Raymond Hettinger <python@rcn.com>
parents: 50793
diff changeset
664 Counter({'b': 1})
6a6b590912fe Forward port r68792 and r68789 putting Counter in __all__ and adding Counter buildouts.
Raymond Hettinger <python@rcn.com>
parents: 50793
diff changeset
665
6a6b590912fe Forward port r68792 and r68789 putting Counter in __all__ and adding Counter buildouts.
Raymond Hettinger <python@rcn.com>
parents: 50793
diff changeset
666 '''
6a6b590912fe Forward port r68792 and r68789 putting Counter in __all__ and adding Counter buildouts.
Raymond Hettinger <python@rcn.com>
parents: 50793
diff changeset
667 if not isinstance(other, Counter):
6a6b590912fe Forward port r68792 and r68789 putting Counter in __all__ and adding Counter buildouts.
Raymond Hettinger <python@rcn.com>
parents: 50793
diff changeset
668 return NotImplemented
6a6b590912fe Forward port r68792 and r68789 putting Counter in __all__ and adding Counter buildouts.
Raymond Hettinger <python@rcn.com>
parents: 50793
diff changeset
669 result = Counter()
69412
49e756c8c08b Rework multiset methods to use less memory and to make fewer calls to __hash__.
Raymond Hettinger <python@rcn.com>
parents: 69379
diff changeset
670 for elem, count in self.items():
49e756c8c08b Rework multiset methods to use less memory and to make fewer calls to __hash__.
Raymond Hettinger <python@rcn.com>
parents: 69379
diff changeset
671 other_count = other[elem]
49e756c8c08b Rework multiset methods to use less memory and to make fewer calls to __hash__.
Raymond Hettinger <python@rcn.com>
parents: 69379
diff changeset
672 newcount = count if count < other_count else other_count
50925
6a6b590912fe Forward port r68792 and r68789 putting Counter in __all__ and adding Counter buildouts.
Raymond Hettinger <python@rcn.com>
parents: 50793
diff changeset
673 if newcount > 0:
6a6b590912fe Forward port r68792 and r68789 putting Counter in __all__ and adding Counter buildouts.
Raymond Hettinger <python@rcn.com>
parents: 50793
diff changeset
674 result[elem] = newcount
6a6b590912fe Forward port r68792 and r68789 putting Counter in __all__ and adding Counter buildouts.
Raymond Hettinger <python@rcn.com>
parents: 50793
diff changeset
675 return result
6a6b590912fe Forward port r68792 and r68789 putting Counter in __all__ and adding Counter buildouts.
Raymond Hettinger <python@rcn.com>
parents: 50793
diff changeset
676
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
diff changeset
677
68081
b17bba1c7303 Issue #11089: Fix performance issue limiting the use of ConfigParser()
Raymond Hettinger <python@rcn.com>
parents: 67288
diff changeset
678 ########################################################################
b17bba1c7303 Issue #11089: Fix performance issue limiting the use of ConfigParser()
Raymond Hettinger <python@rcn.com>
parents: 67288
diff changeset
679 ### ChainMap (helper for configparser)
b17bba1c7303 Issue #11089: Fix performance issue limiting the use of ConfigParser()
Raymond Hettinger <python@rcn.com>
parents: 67288
diff changeset
680 ########################################################################
b17bba1c7303 Issue #11089: Fix performance issue limiting the use of ConfigParser()
Raymond Hettinger <python@rcn.com>
parents: 67288
diff changeset
681
b17bba1c7303 Issue #11089: Fix performance issue limiting the use of ConfigParser()
Raymond Hettinger <python@rcn.com>
parents: 67288
diff changeset
682 class _ChainMap(MutableMapping):
b17bba1c7303 Issue #11089: Fix performance issue limiting the use of ConfigParser()
Raymond Hettinger <python@rcn.com>
parents: 67288
diff changeset
683 ''' A ChainMap groups multiple dicts (or other mappings) together
b17bba1c7303 Issue #11089: Fix performance issue limiting the use of ConfigParser()
Raymond Hettinger <python@rcn.com>
parents: 67288
diff changeset
684 to create a single, updateable view.
b17bba1c7303 Issue #11089: Fix performance issue limiting the use of ConfigParser()
Raymond Hettinger <python@rcn.com>
parents: 67288
diff changeset
685
b17bba1c7303 Issue #11089: Fix performance issue limiting the use of ConfigParser()
Raymond Hettinger <python@rcn.com>
parents: 67288
diff changeset
686 The underlying mappings are stored in a list. That list is public and can
b17bba1c7303 Issue #11089: Fix performance issue limiting the use of ConfigParser()
Raymond Hettinger <python@rcn.com>
parents: 67288
diff changeset
687 accessed or updated using the *maps* attribute. There is no other state.
b17bba1c7303 Issue #11089: Fix performance issue limiting the use of ConfigParser()
Raymond Hettinger <python@rcn.com>
parents: 67288
diff changeset
688
b17bba1c7303 Issue #11089: Fix performance issue limiting the use of ConfigParser()
Raymond Hettinger <python@rcn.com>
parents: 67288
diff changeset
689 Lookups search the underlying mappings successively until a key is found.
b17bba1c7303 Issue #11089: Fix performance issue limiting the use of ConfigParser()
Raymond Hettinger <python@rcn.com>
parents: 67288
diff changeset
690 In contrast, writes, updates, and deletions only operate on the first
b17bba1c7303 Issue #11089: Fix performance issue limiting the use of ConfigParser()
Raymond Hettinger <python@rcn.com>
parents: 67288
diff changeset
691 mapping.
b17bba1c7303 Issue #11089: Fix performance issue limiting the use of ConfigParser()
Raymond Hettinger <python@rcn.com>
parents: 67288
diff changeset
692
b17bba1c7303 Issue #11089: Fix performance issue limiting the use of ConfigParser()
Raymond Hettinger <python@rcn.com>
parents: 67288
diff changeset
693 '''
b17bba1c7303 Issue #11089: Fix performance issue limiting the use of ConfigParser()
Raymond Hettinger <python@rcn.com>
parents: 67288
diff changeset
694
b17bba1c7303 Issue #11089: Fix performance issue limiting the use of ConfigParser()
Raymond Hettinger <python@rcn.com>
parents: 67288
diff changeset
695 def __init__(self, *maps):
b17bba1c7303 Issue #11089: Fix performance issue limiting the use of ConfigParser()
Raymond Hettinger <python@rcn.com>
parents: 67288
diff changeset
696 '''Initialize a ChainMap by setting *maps* to the given mappings.
b17bba1c7303 Issue #11089: Fix performance issue limiting the use of ConfigParser()
Raymond Hettinger <python@rcn.com>
parents: 67288
diff changeset
697 If no mappings are provided, a single empty dictionary is used.
b17bba1c7303 Issue #11089: Fix performance issue limiting the use of ConfigParser()
Raymond Hettinger <python@rcn.com>
parents: 67288
diff changeset
698
b17bba1c7303 Issue #11089: Fix performance issue limiting the use of ConfigParser()
Raymond Hettinger <python@rcn.com>
parents: 67288
diff changeset
699 '''
b17bba1c7303 Issue #11089: Fix performance issue limiting the use of ConfigParser()
Raymond Hettinger <python@rcn.com>
parents: 67288
diff changeset
700 self.maps = list(maps) or [{}] # always at least one map
b17bba1c7303 Issue #11089: Fix performance issue limiting the use of ConfigParser()
Raymond Hettinger <python@rcn.com>
parents: 67288
diff changeset
701
b17bba1c7303 Issue #11089: Fix performance issue limiting the use of ConfigParser()
Raymond Hettinger <python@rcn.com>
parents: 67288
diff changeset
702 def __missing__(self, key):
b17bba1c7303 Issue #11089: Fix performance issue limiting the use of ConfigParser()
Raymond Hettinger <python@rcn.com>
parents: 67288
diff changeset
703 raise KeyError(key)
b17bba1c7303 Issue #11089: Fix performance issue limiting the use of ConfigParser()
Raymond Hettinger <python@rcn.com>
parents: 67288
diff changeset
704
b17bba1c7303 Issue #11089: Fix performance issue limiting the use of ConfigParser()
Raymond Hettinger <python@rcn.com>
parents: 67288
diff changeset
705 def __getitem__(self, key):
b17bba1c7303 Issue #11089: Fix performance issue limiting the use of ConfigParser()
Raymond Hettinger <python@rcn.com>
parents: 67288
diff changeset
706 for mapping in self.maps:
b17bba1c7303 Issue #11089: Fix performance issue limiting the use of ConfigParser()
Raymond Hettinger <python@rcn.com>
parents: 67288
diff changeset
707 try:
b17bba1c7303 Issue #11089: Fix performance issue limiting the use of ConfigParser()
Raymond Hettinger <python@rcn.com>
parents: 67288
diff changeset
708 return mapping[key] # can't use 'key in mapping' with defaultdict
b17bba1c7303 Issue #11089: Fix performance issue limiting the use of ConfigParser()
Raymond Hettinger <python@rcn.com>
parents: 67288
diff changeset
709 except KeyError:
b17bba1c7303 Issue #11089: Fix performance issue limiting the use of ConfigParser()
Raymond Hettinger <python@rcn.com>
parents: 67288
diff changeset
710 pass
b17bba1c7303 Issue #11089: Fix performance issue limiting the use of ConfigParser()
Raymond Hettinger <python@rcn.com>
parents: 67288
diff changeset
711 return self.__missing__(key) # support subclasses that define __missing__
b17bba1c7303 Issue #11089: Fix performance issue limiting the use of ConfigParser()
Raymond Hettinger <python@rcn.com>
parents: 67288
diff changeset
712
b17bba1c7303 Issue #11089: Fix performance issue limiting the use of ConfigParser()
Raymond Hettinger <python@rcn.com>
parents: 67288
diff changeset
713 def get(self, key, default=None):
b17bba1c7303 Issue #11089: Fix performance issue limiting the use of ConfigParser()
Raymond Hettinger <python@rcn.com>
parents: 67288
diff changeset
714 return self[key] if key in self else default
b17bba1c7303 Issue #11089: Fix performance issue limiting the use of ConfigParser()
Raymond Hettinger <python@rcn.com>
parents: 67288
diff changeset
715
b17bba1c7303 Issue #11089: Fix performance issue limiting the use of ConfigParser()
Raymond Hettinger <python@rcn.com>
parents: 67288
diff changeset
716 def __len__(self):
b17bba1c7303 Issue #11089: Fix performance issue limiting the use of ConfigParser()
Raymond Hettinger <python@rcn.com>
parents: 67288
diff changeset
717 return len(set().union(*self.maps)) # reuses stored hash values if possible
b17bba1c7303 Issue #11089: Fix performance issue limiting the use of ConfigParser()
Raymond Hettinger <python@rcn.com>
parents: 67288
diff changeset
718
b17bba1c7303 Issue #11089: Fix performance issue limiting the use of ConfigParser()
Raymond Hettinger <python@rcn.com>
parents: 67288
diff changeset
719 def __iter__(self):
b17bba1c7303 Issue #11089: Fix performance issue limiting the use of ConfigParser()
Raymond Hettinger <python@rcn.com>
parents: 67288
diff changeset
720 return iter(set().union(*self.maps))
b17bba1c7303 Issue #11089: Fix performance issue limiting the use of ConfigParser()
Raymond Hettinger <python@rcn.com>
parents: 67288
diff changeset
721
b17bba1c7303 Issue #11089: Fix performance issue limiting the use of ConfigParser()
Raymond Hettinger <python@rcn.com>
parents: 67288
diff changeset
722 def __contains__(self, key):
b17bba1c7303 Issue #11089: Fix performance issue limiting the use of ConfigParser()
Raymond Hettinger <python@rcn.com>
parents: 67288
diff changeset
723 return any(key in m for m in self.maps)
b17bba1c7303 Issue #11089: Fix performance issue limiting the use of ConfigParser()
Raymond Hettinger <python@rcn.com>
parents: 67288
diff changeset
724
69791
bb58d777eaa9 Backport 3.3 fixes and cleans ups.
Raymond Hettinger <python@rcn.com>
parents: 69786
diff changeset
725 def __bool__(self):
bb58d777eaa9 Backport 3.3 fixes and cleans ups.
Raymond Hettinger <python@rcn.com>
parents: 69786
diff changeset
726 return any(self.maps)
bb58d777eaa9 Backport 3.3 fixes and cleans ups.
Raymond Hettinger <python@rcn.com>
parents: 69786
diff changeset
727
68081
b17bba1c7303 Issue #11089: Fix performance issue limiting the use of ConfigParser()
Raymond Hettinger <python@rcn.com>
parents: 67288
diff changeset
728 @_recursive_repr()
b17bba1c7303 Issue #11089: Fix performance issue limiting the use of ConfigParser()
Raymond Hettinger <python@rcn.com>
parents: 67288
diff changeset
729 def __repr__(self):
b17bba1c7303 Issue #11089: Fix performance issue limiting the use of ConfigParser()
Raymond Hettinger <python@rcn.com>
parents: 67288
diff changeset
730 return '{0.__class__.__name__}({1})'.format(
b17bba1c7303 Issue #11089: Fix performance issue limiting the use of ConfigParser()
Raymond Hettinger <python@rcn.com>
parents: 67288
diff changeset
731 self, ', '.join(map(repr, self.maps)))
b17bba1c7303 Issue #11089: Fix performance issue limiting the use of ConfigParser()
Raymond Hettinger <python@rcn.com>
parents: 67288
diff changeset
732
b17bba1c7303 Issue #11089: Fix performance issue limiting the use of ConfigParser()
Raymond Hettinger <python@rcn.com>
parents: 67288
diff changeset
733 @classmethod
b17bba1c7303 Issue #11089: Fix performance issue limiting the use of ConfigParser()
Raymond Hettinger <python@rcn.com>
parents: 67288
diff changeset
734 def fromkeys(cls, iterable, *args):
b17bba1c7303 Issue #11089: Fix performance issue limiting the use of ConfigParser()
Raymond Hettinger <python@rcn.com>
parents: 67288
diff changeset
735 'Create a ChainMap with a single dict created from the iterable.'
b17bba1c7303 Issue #11089: Fix performance issue limiting the use of ConfigParser()
Raymond Hettinger <python@rcn.com>
parents: 67288
diff changeset
736 return cls(dict.fromkeys(iterable, *args))
b17bba1c7303 Issue #11089: Fix performance issue limiting the use of ConfigParser()
Raymond Hettinger <python@rcn.com>
parents: 67288
diff changeset
737
b17bba1c7303 Issue #11089: Fix performance issue limiting the use of ConfigParser()
Raymond Hettinger <python@rcn.com>
parents: 67288
diff changeset
738 def copy(self):
b17bba1c7303 Issue #11089: Fix performance issue limiting the use of ConfigParser()
Raymond Hettinger <python@rcn.com>
parents: 67288
diff changeset
739 'New ChainMap or subclass with a new copy of maps[0] and refs to maps[1:]'
b17bba1c7303 Issue #11089: Fix performance issue limiting the use of ConfigParser()
Raymond Hettinger <python@rcn.com>
parents: 67288
diff changeset
740 return self.__class__(self.maps[0].copy(), *self.maps[1:])
b17bba1c7303 Issue #11089: Fix performance issue limiting the use of ConfigParser()
Raymond Hettinger <python@rcn.com>
parents: 67288
diff changeset
741
b17bba1c7303 Issue #11089: Fix performance issue limiting the use of ConfigParser()
Raymond Hettinger <python@rcn.com>
parents: 67288
diff changeset
742 __copy__ = copy
b17bba1c7303 Issue #11089: Fix performance issue limiting the use of ConfigParser()
Raymond Hettinger <python@rcn.com>
parents: 67288
diff changeset
743
67994
ba1aeb362182 Add tests for the collections helper class and sync-up with py3k branch.
Raymond Hettinger <python@rcn.com>
parents: 68081
diff changeset
744 def new_child(self): # like Django's Context.push()
ba1aeb362182 Add tests for the collections helper class and sync-up with py3k branch.
Raymond Hettinger <python@rcn.com>
parents: 68081
diff changeset
745 'New ChainMap with a new dict followed by all previous maps.'
ba1aeb362182 Add tests for the collections helper class and sync-up with py3k branch.
Raymond Hettinger <python@rcn.com>
parents: 68081
diff changeset
746 return self.__class__({}, *self.maps)
ba1aeb362182 Add tests for the collections helper class and sync-up with py3k branch.
Raymond Hettinger <python@rcn.com>
parents: 68081
diff changeset
747
ba1aeb362182 Add tests for the collections helper class and sync-up with py3k branch.
Raymond Hettinger <python@rcn.com>
parents: 68081
diff changeset
748 @property
ba1aeb362182 Add tests for the collections helper class and sync-up with py3k branch.
Raymond Hettinger <python@rcn.com>
parents: 68081
diff changeset
749 def parents(self): # like Django's Context.pop()
ba1aeb362182 Add tests for the collections helper class and sync-up with py3k branch.
Raymond Hettinger <python@rcn.com>
parents: 68081
diff changeset
750 'New ChainMap from maps[1:].'
ba1aeb362182 Add tests for the collections helper class and sync-up with py3k branch.
Raymond Hettinger <python@rcn.com>
parents: 68081
diff changeset
751 return self.__class__(*self.maps[1:])
ba1aeb362182 Add tests for the collections helper class and sync-up with py3k branch.
Raymond Hettinger <python@rcn.com>
parents: 68081
diff changeset
752
68081
b17bba1c7303 Issue #11089: Fix performance issue limiting the use of ConfigParser()
Raymond Hettinger <python@rcn.com>
parents: 67288
diff changeset
753 def __setitem__(self, key, value):
b17bba1c7303 Issue #11089: Fix performance issue limiting the use of ConfigParser()
Raymond Hettinger <python@rcn.com>
parents: 67288
diff changeset
754 self.maps[0][key] = value
b17bba1c7303 Issue #11089: Fix performance issue limiting the use of ConfigParser()
Raymond Hettinger <python@rcn.com>
parents: 67288
diff changeset
755
b17bba1c7303 Issue #11089: Fix performance issue limiting the use of ConfigParser()
Raymond Hettinger <python@rcn.com>
parents: 67288
diff changeset
756 def __delitem__(self, key):
b17bba1c7303 Issue #11089: Fix performance issue limiting the use of ConfigParser()
Raymond Hettinger <python@rcn.com>
parents: 67288
diff changeset
757 try:
b17bba1c7303 Issue #11089: Fix performance issue limiting the use of ConfigParser()
Raymond Hettinger <python@rcn.com>
parents: 67288
diff changeset
758 del self.maps[0][key]
b17bba1c7303 Issue #11089: Fix performance issue limiting the use of ConfigParser()
Raymond Hettinger <python@rcn.com>
parents: 67288
diff changeset
759 except KeyError:
b17bba1c7303 Issue #11089: Fix performance issue limiting the use of ConfigParser()
Raymond Hettinger <python@rcn.com>
parents: 67288
diff changeset
760 raise KeyError('Key not found in the first mapping: {!r}'.format(key))
b17bba1c7303 Issue #11089: Fix performance issue limiting the use of ConfigParser()
Raymond Hettinger <python@rcn.com>
parents: 67288
diff changeset
761
b17bba1c7303 Issue #11089: Fix performance issue limiting the use of ConfigParser()
Raymond Hettinger <python@rcn.com>
parents: 67288
diff changeset
762 def popitem(self):
b17bba1c7303 Issue #11089: Fix performance issue limiting the use of ConfigParser()
Raymond Hettinger <python@rcn.com>
parents: 67288
diff changeset
763 'Remove and return an item pair from maps[0]. Raise KeyError is maps[0] is empty.'
b17bba1c7303 Issue #11089: Fix performance issue limiting the use of ConfigParser()
Raymond Hettinger <python@rcn.com>
parents: 67288
diff changeset
764 try:
b17bba1c7303 Issue #11089: Fix performance issue limiting the use of ConfigParser()
Raymond Hettinger <python@rcn.com>
parents: 67288
diff changeset
765 return self.maps[0].popitem()
b17bba1c7303 Issue #11089: Fix performance issue limiting the use of ConfigParser()
Raymond Hettinger <python@rcn.com>
parents: 67288
diff changeset
766 except KeyError:
b17bba1c7303 Issue #11089: Fix performance issue limiting the use of ConfigParser()
Raymond Hettinger <python@rcn.com>
parents: 67288
diff changeset
767 raise KeyError('No keys found in the first mapping.')
b17bba1c7303 Issue #11089: Fix performance issue limiting the use of ConfigParser()
Raymond Hettinger <python@rcn.com>
parents: 67288
diff changeset
768
b17bba1c7303 Issue #11089: Fix performance issue limiting the use of ConfigParser()
Raymond Hettinger <python@rcn.com>
parents: 67288
diff changeset
769 def pop(self, key, *args):
b17bba1c7303 Issue #11089: Fix performance issue limiting the use of ConfigParser()
Raymond Hettinger <python@rcn.com>
parents: 67288
diff changeset
770 'Remove *key* from maps[0] and return its value. Raise KeyError if *key* not in maps[0].'
b17bba1c7303 Issue #11089: Fix performance issue limiting the use of ConfigParser()
Raymond Hettinger <python@rcn.com>
parents: 67288
diff changeset
771 try:
b17bba1c7303 Issue #11089: Fix performance issue limiting the use of ConfigParser()
Raymond Hettinger <python@rcn.com>
parents: 67288
diff changeset
772 return self.maps[0].pop(key, *args)
b17bba1c7303 Issue #11089: Fix performance issue limiting the use of ConfigParser()
Raymond Hettinger <python@rcn.com>
parents: 67288
diff changeset
773 except KeyError:
b17bba1c7303 Issue #11089: Fix performance issue limiting the use of ConfigParser()
Raymond Hettinger <python@rcn.com>
parents: 67288
diff changeset
774 raise KeyError('Key not found in the first mapping: {!r}'.format(key))
b17bba1c7303 Issue #11089: Fix performance issue limiting the use of ConfigParser()
Raymond Hettinger <python@rcn.com>
parents: 67288
diff changeset
775
b17bba1c7303 Issue #11089: Fix performance issue limiting the use of ConfigParser()
Raymond Hettinger <python@rcn.com>
parents: 67288
diff changeset
776 def clear(self):
b17bba1c7303 Issue #11089: Fix performance issue limiting the use of ConfigParser()
Raymond Hettinger <python@rcn.com>
parents: 67288
diff changeset
777 'Clear maps[0], leaving maps[1:] intact.'
b17bba1c7303 Issue #11089: Fix performance issue limiting the use of ConfigParser()
Raymond Hettinger <python@rcn.com>
parents: 67288
diff changeset
778 self.maps[0].clear()
b17bba1c7303 Issue #11089: Fix performance issue limiting the use of ConfigParser()
Raymond Hettinger <python@rcn.com>
parents: 67288
diff changeset
779
b17bba1c7303 Issue #11089: Fix performance issue limiting the use of ConfigParser()
Raymond Hettinger <python@rcn.com>
parents: 67288
diff changeset
780
45162
646347444faa Put an updated UserDict class in the collections module and
Raymond Hettinger <python@rcn.com>
parents: 44643
diff changeset
781 ################################################################################
646347444faa Put an updated UserDict class in the collections module and
Raymond Hettinger <python@rcn.com>
parents: 44643
diff changeset
782 ### UserDict
646347444faa Put an updated UserDict class in the collections module and
Raymond Hettinger <python@rcn.com>
parents: 44643
diff changeset
783 ################################################################################
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
diff changeset
784
45162
646347444faa Put an updated UserDict class in the collections module and
Raymond Hettinger <python@rcn.com>
parents: 44643
diff changeset
785 class UserDict(MutableMapping):
646347444faa Put an updated UserDict class in the collections module and
Raymond Hettinger <python@rcn.com>
parents: 44643
diff changeset
786
646347444faa Put an updated UserDict class in the collections module and
Raymond Hettinger <python@rcn.com>
parents: 44643
diff changeset
787 # Start by filling-out the abstract methods
646347444faa Put an updated UserDict class in the collections module and
Raymond Hettinger <python@rcn.com>
parents: 44643
diff changeset
788 def __init__(self, dict=None, **kwargs):
646347444faa Put an updated UserDict class in the collections module and
Raymond Hettinger <python@rcn.com>
parents: 44643
diff changeset
789 self.data = {}
646347444faa Put an updated UserDict class in the collections module and
Raymond Hettinger <python@rcn.com>
parents: 44643
diff changeset
790 if dict is not None:
646347444faa Put an updated UserDict class in the collections module and
Raymond Hettinger <python@rcn.com>
parents: 44643
diff changeset
791 self.update(dict)
646347444faa Put an updated UserDict class in the collections module and
Raymond Hettinger <python@rcn.com>
parents: 44643
diff changeset
792 if len(kwargs):
646347444faa Put an updated UserDict class in the collections module and
Raymond Hettinger <python@rcn.com>
parents: 44643
diff changeset
793 self.update(kwargs)
646347444faa Put an updated UserDict class in the collections module and
Raymond Hettinger <python@rcn.com>
parents: 44643
diff changeset
794 def __len__(self): return len(self.data)
646347444faa Put an updated UserDict class in the collections module and
Raymond Hettinger <python@rcn.com>
parents: 44643
diff changeset
795 def __getitem__(self, key):
646347444faa Put an updated UserDict class in the collections module and
Raymond Hettinger <python@rcn.com>
parents: 44643
diff changeset
796 if key in self.data:
646347444faa Put an updated UserDict class in the collections module and
Raymond Hettinger <python@rcn.com>
parents: 44643
diff changeset
797 return self.data[key]
646347444faa Put an updated UserDict class in the collections module and
Raymond Hettinger <python@rcn.com>
parents: 44643
diff changeset
798 if hasattr(self.__class__, "__missing__"):
646347444faa Put an updated UserDict class in the collections module and
Raymond Hettinger <python@rcn.com>
parents: 44643
diff changeset
799 return self.__class__.__missing__(self, key)
646347444faa Put an updated UserDict class in the collections module and
Raymond Hettinger <python@rcn.com>
parents: 44643
diff changeset
800 raise KeyError(key)
646347444faa Put an updated UserDict class in the collections module and
Raymond Hettinger <python@rcn.com>
parents: 44643
diff changeset
801 def __setitem__(self, key, item): self.data[key] = item
646347444faa Put an updated UserDict class in the collections module and
Raymond Hettinger <python@rcn.com>
parents: 44643
diff changeset
802 def __delitem__(self, key): del self.data[key]
646347444faa Put an updated UserDict class in the collections module and
Raymond Hettinger <python@rcn.com>
parents: 44643
diff changeset
803 def __iter__(self):
646347444faa Put an updated UserDict class in the collections module and
Raymond Hettinger <python@rcn.com>
parents: 44643
diff changeset
804 return iter(self.data)
646347444faa Put an updated UserDict class in the collections module and
Raymond Hettinger <python@rcn.com>
parents: 44643
diff changeset
805
45184
3ecd526c35c5 Convert test_userdict to use the collections.UserDict.
Raymond Hettinger <python@rcn.com>
parents: 45162
diff changeset
806 # Modify __contains__ to work correctly when __missing__ is present
3ecd526c35c5 Convert test_userdict to use the collections.UserDict.
Raymond Hettinger <python@rcn.com>
parents: 45162
diff changeset
807 def __contains__(self, key):
3ecd526c35c5 Convert test_userdict to use the collections.UserDict.
Raymond Hettinger <python@rcn.com>
parents: 45162
diff changeset
808 return key in self.data
45162
646347444faa Put an updated UserDict class in the collections module and
Raymond Hettinger <python@rcn.com>
parents: 44643
diff changeset
809
646347444faa Put an updated UserDict class in the collections module and
Raymond Hettinger <python@rcn.com>
parents: 44643
diff changeset
810 # Now, add the methods in dicts but not in MutableMapping
646347444faa Put an updated UserDict class in the collections module and
Raymond Hettinger <python@rcn.com>
parents: 44643
diff changeset
811 def __repr__(self): return repr(self.data)
646347444faa Put an updated UserDict class in the collections module and
Raymond Hettinger <python@rcn.com>
parents: 44643
diff changeset
812 def copy(self):
646347444faa Put an updated UserDict class in the collections module and
Raymond Hettinger <python@rcn.com>
parents: 44643
diff changeset
813 if self.__class__ is UserDict:
646347444faa Put an updated UserDict class in the collections module and
Raymond Hettinger <python@rcn.com>
parents: 44643
diff changeset
814 return UserDict(self.data.copy())
646347444faa Put an updated UserDict class in the collections module and
Raymond Hettinger <python@rcn.com>
parents: 44643
diff changeset
815 import copy
646347444faa Put an updated UserDict class in the collections module and
Raymond Hettinger <python@rcn.com>
parents: 44643
diff changeset
816 data = self.data
646347444faa Put an updated UserDict class in the collections module and
Raymond Hettinger <python@rcn.com>
parents: 44643
diff changeset
817 try:
646347444faa Put an updated UserDict class in the collections module and
Raymond Hettinger <python@rcn.com>
parents: 44643
diff changeset
818 self.data = {}
646347444faa Put an updated UserDict class in the collections module and
Raymond Hettinger <python@rcn.com>
parents: 44643
diff changeset
819 c = copy.copy(self)
646347444faa Put an updated UserDict class in the collections module and
Raymond Hettinger <python@rcn.com>
parents: 44643
diff changeset
820 finally:
646347444faa Put an updated UserDict class in the collections module and
Raymond Hettinger <python@rcn.com>
parents: 44643
diff changeset
821 self.data = data
646347444faa Put an updated UserDict class in the collections module and
Raymond Hettinger <python@rcn.com>
parents: 44643
diff changeset
822 c.update(self)
646347444faa Put an updated UserDict class in the collections module and
Raymond Hettinger <python@rcn.com>
parents: 44643
diff changeset
823 return c
646347444faa Put an updated UserDict class in the collections module and
Raymond Hettinger <python@rcn.com>
parents: 44643
diff changeset
824 @classmethod
646347444faa Put an updated UserDict class in the collections module and
Raymond Hettinger <python@rcn.com>
parents: 44643
diff changeset
825 def fromkeys(cls, iterable, value=None):
646347444faa Put an updated UserDict class in the collections module and
Raymond Hettinger <python@rcn.com>
parents: 44643
diff changeset
826 d = cls()
646347444faa Put an updated UserDict class in the collections module and
Raymond Hettinger <python@rcn.com>
parents: 44643
diff changeset
827 for key in iterable:
646347444faa Put an updated UserDict class in the collections module and
Raymond Hettinger <python@rcn.com>
parents: 44643
diff changeset
828 d[key] = value
646347444faa Put an updated UserDict class in the collections module and
Raymond Hettinger <python@rcn.com>
parents: 44643
diff changeset
829 return d
646347444faa Put an updated UserDict class in the collections module and
Raymond Hettinger <python@rcn.com>
parents: 44643
diff changeset
830
646347444faa Put an updated UserDict class in the collections module and
Raymond Hettinger <python@rcn.com>
parents: 44643
diff changeset
831
646347444faa Put an updated UserDict class in the collections module and
Raymond Hettinger <python@rcn.com>
parents: 44643
diff changeset
832
646347444faa Put an updated UserDict class in the collections module and
Raymond Hettinger <python@rcn.com>
parents: 44643
diff changeset
833 ################################################################################
45302
b1cb6b44b798 Move UserList to collections.
Raymond Hettinger <python@rcn.com>
parents: 45284
diff changeset
834 ### UserList
b1cb6b44b798 Move UserList to collections.
Raymond Hettinger <python@rcn.com>
parents: 45284
diff changeset
835 ################################################################################
b1cb6b44b798 Move UserList to collections.
Raymond Hettinger <python@rcn.com>
parents: 45284
diff changeset
836
b1cb6b44b798 Move UserList to collections.
Raymond Hettinger <python@rcn.com>
parents: 45284
diff changeset
837 class UserList(MutableSequence):
b1cb6b44b798 Move UserList to collections.
Raymond Hettinger <python@rcn.com>
parents: 45284
diff changeset
838 """A more or less complete user-defined wrapper around list objects."""
b1cb6b44b798 Move UserList to collections.
Raymond Hettinger <python@rcn.com>
parents: 45284
diff changeset
839 def __init__(self, initlist=None):
b1cb6b44b798 Move UserList to collections.
Raymond Hettinger <python@rcn.com>
parents: 45284
diff changeset
840 self.data = []
b1cb6b44b798 Move UserList to collections.
Raymond Hettinger <python@rcn.com>
parents: 45284
diff changeset
841 if initlist is not None:
b1cb6b44b798 Move UserList to collections.
Raymond Hettinger <python@rcn.com>
parents: 45284
diff changeset
842 # XXX should this accept an arbitrary sequence?
b1cb6b44b798 Move UserList to collections.
Raymond Hettinger <python@rcn.com>
parents: 45284
diff changeset
843 if type(initlist) == type(self.data):
b1cb6b44b798 Move UserList to collections.
Raymond Hettinger <python@rcn.com>
parents: 45284
diff changeset
844 self.data[:] = initlist
b1cb6b44b798 Move UserList to collections.
Raymond Hettinger <python@rcn.com>
parents: 45284
diff changeset
845 elif isinstance(initlist, UserList):
b1cb6b44b798 Move UserList to collections.
Raymond Hettinger <python@rcn.com>
parents: 45284
diff changeset
846 self.data[:] = initlist.data[:]
b1cb6b44b798 Move UserList to collections.
Raymond Hettinger <python@rcn.com>
parents: 45284
diff changeset
847 else:
b1cb6b44b798 Move UserList to collections.
Raymond Hettinger <python@rcn.com>
parents: 45284
diff changeset
848 self.data = list(initlist)
b1cb6b44b798 Move UserList to collections.
Raymond Hettinger <python@rcn.com>
parents: 45284
diff changeset
849 def __repr__(self): return repr(self.data)
b1cb6b44b798 Move UserList to collections.
Raymond Hettinger <python@rcn.com>
parents: 45284
diff changeset
850 def __lt__(self, other): return self.data < self.__cast(other)
b1cb6b44b798 Move UserList to collections.
Raymond Hettinger <python@rcn.com>
parents: 45284
diff changeset
851 def __le__(self, other): return self.data <= self.__cast(other)
b1cb6b44b798 Move UserList to collections.
Raymond Hettinger <python@rcn.com>
parents: 45284
diff changeset
852 def __eq__(self, other): return self.data == self.__cast(other)
b1cb6b44b798 Move UserList to collections.
Raymond Hettinger <python@rcn.com>
parents: 45284
diff changeset
853 def __ne__(self, other): return self.data != self.__cast(other)
b1cb6b44b798 Move UserList to collections.
Raymond Hettinger <python@rcn.com>
parents: 45284
diff changeset
854 def __gt__(self, other): return self.data > self.__cast(other)
b1cb6b44b798 Move UserList to collections.
Raymond Hettinger <python@rcn.com>
parents: 45284
diff changeset
855 def __ge__(self, other): return self.data >= self.__cast(other)
b1cb6b44b798 Move UserList to collections.
Raymond Hettinger <python@rcn.com>
parents: 45284
diff changeset
856 def __cast(self, other):
b1cb6b44b798 Move UserList to collections.
Raymond Hettinger <python@rcn.com>
parents: 45284
diff changeset
857 return other.data if isinstance(other, UserList) else other
b1cb6b44b798 Move UserList to collections.
Raymond Hettinger <python@rcn.com>
parents: 45284
diff changeset
858 def __contains__(self, item): return item in self.data
b1cb6b44b798 Move UserList to collections.
Raymond Hettinger <python@rcn.com>
parents: 45284
diff changeset
859 def __len__(self): return len(self.data)
b1cb6b44b798 Move UserList to collections.
Raymond Hettinger <python@rcn.com>
parents: 45284
diff changeset
860 def __getitem__(self, i): return self.data[i]
b1cb6b44b798 Move UserList to collections.
Raymond Hettinger <python@rcn.com>
parents: 45284
diff changeset
861 def __setitem__(self, i, item): self.data[i] = item
b1cb6b44b798 Move UserList to collections.
Raymond Hettinger <python@rcn.com>
parents: 45284
diff changeset
862 def __delitem__(self, i): del self.data[i]
b1cb6b44b798 Move UserList to collections.
Raymond Hettinger <python@rcn.com>
parents: 45284
diff changeset
863 def __add__(self, other):
b1cb6b44b798 Move UserList to collections.
Raymond Hettinger <python@rcn.com>
parents: 45284
diff changeset
864 if isinstance(other, UserList):
b1cb6b44b798 Move UserList to collections.
Raymond Hettinger <python@rcn.com>
parents: 45284
diff changeset
865 return self.__class__(self.data + other.data)
b1cb6b44b798 Move UserList to collections.
Raymond Hettinger <python@rcn.com>
parents: 45284
diff changeset
866 elif isinstance(other, type(self.data)):
b1cb6b44b798 Move UserList to collections.
Raymond Hettinger <python@rcn.com>
parents: 45284
diff changeset
867 return self.__class__(self.data + other)
b1cb6b44b798 Move UserList to collections.
Raymond Hettinger <python@rcn.com>
parents: 45284
diff changeset
868 return self.__class__(self.data + list(other))
b1cb6b44b798 Move UserList to collections.
Raymond Hettinger <python@rcn.com>
parents: 45284
diff changeset
869 def __radd__(self, other):
b1cb6b44b798 Move UserList to collections.
Raymond Hettinger <python@rcn.com>
parents: 45284
diff changeset
870 if isinstance(other, UserList):
b1cb6b44b798 Move UserList to collections.
Raymond Hettinger <python@rcn.com>
parents: 45284
diff changeset
871 return self.__class__(other.data + self.data)
b1cb6b44b798 Move UserList to collections.
Raymond Hettinger <python@rcn.com>
parents: 45284
diff changeset
872 elif isinstance(other, type(self.data)):
b1cb6b44b798 Move UserList to collections.
Raymond Hettinger <python@rcn.com>
parents: 45284
diff changeset
873 return self.__class__(other + self.data)
b1cb6b44b798 Move UserList to collections.
Raymond Hettinger <python@rcn.com>
parents: 45284
diff changeset
874 return self.__class__(list(other) + self.data)
b1cb6b44b798 Move UserList to collections.
Raymond Hettinger <python@rcn.com>
parents: 45284
diff changeset
875 def __iadd__(self, other):
b1cb6b44b798 Move UserList to collections.
Raymond Hettinger <python@rcn.com>
parents: 45284
diff changeset
876 if isinstance(other, UserList):
b1cb6b44b798 Move UserList to collections.
Raymond Hettinger <python@rcn.com>
parents: 45284
diff changeset
877 self.data += other.data
b1cb6b44b798 Move UserList to collections.
Raymond Hettinger <python@rcn.com>
parents: 45284
diff changeset
878 elif isinstance(other, type(self.data)):
b1cb6b44b798 Move UserList to collections.
Raymond Hettinger <python@rcn.com>
parents: 45284
diff changeset
879 self.data += other
b1cb6b44b798 Move UserList to collections.
Raymond Hettinger <python@rcn.com>
parents: 45284
diff changeset
880 else:
b1cb6b44b798 Move UserList to collections.
Raymond Hettinger <python@rcn.com>
parents: 45284
diff changeset
881 self.data += list(other)
b1cb6b44b798 Move UserList to collections.
Raymond Hettinger <python@rcn.com>
parents: 45284
diff changeset
882 return self
b1cb6b44b798 Move UserList to collections.
Raymond Hettinger <python@rcn.com>
parents: 45284
diff changeset
883 def __mul__(self, n):
b1cb6b44b798 Move UserList to collections.
Raymond Hettinger <python@rcn.com>
parents: 45284
diff changeset
884 return self.__class__(self.data*n)
b1cb6b44b798 Move UserList to collections.
Raymond Hettinger <python@rcn.com>
parents: 45284
diff changeset
885 __rmul__ = __mul__
b1cb6b44b798 Move UserList to collections.
Raymond Hettinger <python@rcn.com>
parents: 45284
diff changeset
886 def __imul__(self, n):
b1cb6b44b798 Move UserList to collections.
Raymond Hettinger <python@rcn.com>
parents: 45284
diff changeset
887 self.data *= n
b1cb6b44b798 Move UserList to collections.
Raymond Hettinger <python@rcn.com>
parents: 45284
diff changeset
888 return self
b1cb6b44b798 Move UserList to collections.
Raymond Hettinger <python@rcn.com>
parents: 45284
diff changeset
889 def append(self, item): self.data.append(item)
b1cb6b44b798 Move UserList to collections.
Raymond Hettinger <python@rcn.com>
parents: 45284
diff changeset
890 def insert(self, i, item): self.data.insert(i, item)
b1cb6b44b798 Move UserList to collections.
Raymond Hettinger <python@rcn.com>
parents: 45284
diff changeset
891 def pop(self, i=-1): return self.data.pop(i)
b1cb6b44b798 Move UserList to collections.
Raymond Hettinger <python@rcn.com>
parents: 45284
diff changeset
892 def remove(self, item): self.data.remove(item)
b1cb6b44b798 Move UserList to collections.
Raymond Hettinger <python@rcn.com>
parents: 45284
diff changeset
893 def count(self, item): return self.data.count(item)
b1cb6b44b798 Move UserList to collections.
Raymond Hettinger <python@rcn.com>
parents: 45284
diff changeset
894 def index(self, item, *args): return self.data.index(item, *args)
b1cb6b44b798 Move UserList to collections.
Raymond Hettinger <python@rcn.com>
parents: 45284
diff changeset
895 def reverse(self): self.data.reverse()
b1cb6b44b798 Move UserList to collections.
Raymond Hettinger <python@rcn.com>
parents: 45284
diff changeset
896 def sort(self, *args, **kwds): self.data.sort(*args, **kwds)
b1cb6b44b798 Move UserList to collections.
Raymond Hettinger <python@rcn.com>
parents: 45284
diff changeset
897 def extend(self, other):
b1cb6b44b798 Move UserList to collections.
Raymond Hettinger <python@rcn.com>
parents: 45284
diff changeset
898 if isinstance(other, UserList):
b1cb6b44b798 Move UserList to collections.
Raymond Hettinger <python@rcn.com>
parents: 45284
diff changeset
899 self.data.extend(other.data)
b1cb6b44b798 Move UserList to collections.
Raymond Hettinger <python@rcn.com>
parents: 45284
diff changeset
900 else:
b1cb6b44b798 Move UserList to collections.
Raymond Hettinger <python@rcn.com>
parents: 45284
diff changeset
901 self.data.extend(other)
b1cb6b44b798 Move UserList to collections.
Raymond Hettinger <python@rcn.com>
parents: 45284
diff changeset
902
b1cb6b44b798 Move UserList to collections.
Raymond Hettinger <python@rcn.com>
parents: 45284
diff changeset
903
b1cb6b44b798 Move UserList to collections.
Raymond Hettinger <python@rcn.com>
parents: 45284
diff changeset
904
b1cb6b44b798 Move UserList to collections.
Raymond Hettinger <python@rcn.com>
parents: 45284
diff changeset
905 ################################################################################
45424
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
906 ### UserString
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
907 ################################################################################
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
908
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
909 class UserString(Sequence):
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
910 def __init__(self, seq):
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
911 if isinstance(seq, str):
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
912 self.data = seq
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
913 elif isinstance(seq, UserString):
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
914 self.data = seq.data[:]
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
915 else:
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
916 self.data = str(seq)
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
917 def __str__(self): return str(self.data)
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
918 def __repr__(self): return repr(self.data)
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
919 def __int__(self): return int(self.data)
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
920 def __float__(self): return float(self.data)
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
921 def __complex__(self): return complex(self.data)
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
922 def __hash__(self): return hash(self.data)
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
923
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
924 def __eq__(self, string):
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
925 if isinstance(string, UserString):
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
926 return self.data == string.data
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
927 return self.data == string
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
928 def __ne__(self, string):
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
929 if isinstance(string, UserString):
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
930 return self.data != string.data
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
931 return self.data != string
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
932 def __lt__(self, string):
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
933 if isinstance(string, UserString):
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
934 return self.data < string.data
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
935 return self.data < string
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
936 def __le__(self, string):
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
937 if isinstance(string, UserString):
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
938 return self.data <= string.data
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
939 return self.data <= string
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
940 def __gt__(self, string):
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
941 if isinstance(string, UserString):
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
942 return self.data > string.data
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
943 return self.data > string
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
944 def __ge__(self, string):
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
945 if isinstance(string, UserString):
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
946 return self.data >= string.data
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
947 return self.data >= string
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
948
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
949 def __contains__(self, char):
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
950 if isinstance(char, UserString):
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
951 char = char.data
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
952 return char in self.data
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
953
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
954 def __len__(self): return len(self.data)
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
955 def __getitem__(self, index): return self.__class__(self.data[index])
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
956 def __add__(self, other):
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
957 if isinstance(other, UserString):
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
958 return self.__class__(self.data + other.data)
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
959 elif isinstance(other, str):
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
960 return self.__class__(self.data + other)
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
961 return self.__class__(self.data + str(other))
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
962 def __radd__(self, other):
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
963 if isinstance(other, str):
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
964 return self.__class__(other + self.data)
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
965 return self.__class__(str(other) + self.data)
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
966 def __mul__(self, n):
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
967 return self.__class__(self.data*n)
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
968 __rmul__ = __mul__
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
969 def __mod__(self, args):
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
970 return self.__class__(self.data % args)
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
971
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
972 # the following methods are defined in alphabetical order:
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
973 def capitalize(self): return self.__class__(self.data.capitalize())
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
974 def center(self, width, *args):
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
975 return self.__class__(self.data.center(width, *args))
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
976 def count(self, sub, start=0, end=_sys.maxsize):
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
977 if isinstance(sub, UserString):
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
978 sub = sub.data
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
979 return self.data.count(sub, start, end)
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
980 def encode(self, encoding=None, errors=None): # XXX improve this?
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
981 if encoding:
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
982 if errors:
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
983 return self.__class__(self.data.encode(encoding, errors))
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
984 return self.__class__(self.data.encode(encoding))
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
985 return self.__class__(self.data.encode())
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
986 def endswith(self, suffix, start=0, end=_sys.maxsize):
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
987 return self.data.endswith(suffix, start, end)
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
988 def expandtabs(self, tabsize=8):
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
989 return self.__class__(self.data.expandtabs(tabsize))
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
990 def find(self, sub, start=0, end=_sys.maxsize):
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
991 if isinstance(sub, UserString):
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
992 sub = sub.data
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
993 return self.data.find(sub, start, end)
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
994 def format(self, *args, **kwds):
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
995 return self.data.format(*args, **kwds)
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
996 def index(self, sub, start=0, end=_sys.maxsize):
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
997 return self.data.index(sub, start, end)
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
998 def isalpha(self): return self.data.isalpha()
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
999 def isalnum(self): return self.data.isalnum()
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
1000 def isdecimal(self): return self.data.isdecimal()
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
1001 def isdigit(self): return self.data.isdigit()
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
1002 def isidentifier(self): return self.data.isidentifier()
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
1003 def islower(self): return self.data.islower()
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
1004 def isnumeric(self): return self.data.isnumeric()
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
1005 def isspace(self): return self.data.isspace()
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
1006 def istitle(self): return self.data.istitle()
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
1007 def isupper(self): return self.data.isupper()
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
1008 def join(self, seq): return self.data.join(seq)
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
1009 def ljust(self, width, *args):
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
1010 return self.__class__(self.data.ljust(width, *args))
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
1011 def lower(self): return self.__class__(self.data.lower())
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
1012 def lstrip(self, chars=None): return self.__class__(self.data.lstrip(chars))
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
1013 def partition(self, sep):
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
1014 return self.data.partition(sep)
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
1015 def replace(self, old, new, maxsplit=-1):
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
1016 if isinstance(old, UserString):
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
1017 old = old.data
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
1018 if isinstance(new, UserString):
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
1019 new = new.data
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
1020 return self.__class__(self.data.replace(old, new, maxsplit))
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
1021 def rfind(self, sub, start=0, end=_sys.maxsize):
58095
a1035cb4a925 Merged revisions 77241 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54985
diff changeset
1022 if isinstance(sub, UserString):
a1035cb4a925 Merged revisions 77241 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54985
diff changeset
1023 sub = sub.data
45424
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
1024 return self.data.rfind(sub, start, end)
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
1025 def rindex(self, sub, start=0, end=_sys.maxsize):
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
1026 return self.data.rindex(sub, start, end)
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
1027 def rjust(self, width, *args):
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
1028 return self.__class__(self.data.rjust(width, *args))
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
1029 def rpartition(self, sep):
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
1030 return self.data.rpartition(sep)
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
1031 def rstrip(self, chars=None):
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
1032 return self.__class__(self.data.rstrip(chars))
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
1033 def split(self, sep=None, maxsplit=-1):
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
1034 return self.data.split(sep, maxsplit)
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
1035 def rsplit(self, sep=None, maxsplit=-1):
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
1036 return self.data.rsplit(sep, maxsplit)
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
1037 def splitlines(self, keepends=0): return self.data.splitlines(keepends)
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
1038 def startswith(self, prefix, start=0, end=_sys.maxsize):
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
1039 return self.data.startswith(prefix, start, end)
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
1040 def strip(self, chars=None): return self.__class__(self.data.strip(chars))
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
1041 def swapcase(self): return self.__class__(self.data.swapcase())
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
1042 def title(self): return self.__class__(self.data.title())
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
1043 def translate(self, *args):
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
1044 return self.__class__(self.data.translate(*args))
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
1045 def upper(self): return self.__class__(self.data.upper())
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
1046 def zfill(self, width): return self.__class__(self.data.zfill(width))
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
1047
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
1048
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
1049
a9e1fdaf413d Move UserString to collections.
Raymond Hettinger <python@rcn.com>
parents: 45302
diff changeset
1050 ################################################################################
45162
646347444faa Put an updated UserDict class in the collections module and
Raymond Hettinger <python@rcn.com>
parents: 44643
diff changeset
1051 ### Simple tests
646347444faa Put an updated UserDict class in the collections module and
Raymond Hettinger <python@rcn.com>
parents: 44643
diff changeset
1052 ################################################################################
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
diff changeset
1053
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
diff changeset
1054 if __name__ == '__main__':
43400
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 42322
diff changeset
1055 # verify that instances can be pickled
42322
3e33215c66ed Getting rid of cPickle. Mmm, feels good!
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
1056 from pickle import loads, dumps
43734
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43400
diff changeset
1057 Point = namedtuple('Point', 'x, y', True)
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
diff changeset
1058 p = Point(x=10, y=20)
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
diff changeset
1059 assert p == loads(dumps(p))
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
diff changeset
1060
43877
1165d261fb05 Merged revisions 58947-59004 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43734
diff changeset
1061 # test and demonstrate ability to override methods
44568
305d7d2ce047 Copied doc for reload() from trunk's function.rst to imp.rst
Christian Heimes <christian@cheimes.de>
parents: 44535
diff changeset
1062 class Point(namedtuple('Point', 'x y')):
44643
d8e4342c145e Merged revisions 59883-59920 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44604
diff changeset
1063 __slots__ = ()
44568
305d7d2ce047 Copied doc for reload() from trunk's function.rst to imp.rst
Christian Heimes <christian@cheimes.de>
parents: 44535
diff changeset
1064 @property
305d7d2ce047 Copied doc for reload() from trunk's function.rst to imp.rst
Christian Heimes <christian@cheimes.de>
parents: 44535
diff changeset
1065 def hypot(self):
305d7d2ce047 Copied doc for reload() from trunk's function.rst to imp.rst
Christian Heimes <christian@cheimes.de>
parents: 44535
diff changeset
1066 return (self.x ** 2 + self.y ** 2) ** 0.5
44587
fb5a6330f71b Merged revisions 59822-59841 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44568
diff changeset
1067 def __str__(self):
44643
d8e4342c145e Merged revisions 59883-59920 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44604
diff changeset
1068 return 'Point: x=%6.3f y=%6.3f hypot=%6.3f' % (self.x, self.y, self.hypot)
44568
305d7d2ce047 Copied doc for reload() from trunk's function.rst to imp.rst
Christian Heimes <christian@cheimes.de>
parents: 44535
diff changeset
1069
44643
d8e4342c145e Merged revisions 59883-59920 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44604
diff changeset
1070 for p in Point(3, 4), Point(14, 5/7.):
44587
fb5a6330f71b Merged revisions 59822-59841 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44568
diff changeset
1071 print (p)
44568
305d7d2ce047 Copied doc for reload() from trunk's function.rst to imp.rst
Christian Heimes <christian@cheimes.de>
parents: 44535
diff changeset
1072
305d7d2ce047 Copied doc for reload() from trunk's function.rst to imp.rst
Christian Heimes <christian@cheimes.de>
parents: 44535
diff changeset
1073 class Point(namedtuple('Point', 'x y')):
305d7d2ce047 Copied doc for reload() from trunk's function.rst to imp.rst
Christian Heimes <christian@cheimes.de>
parents: 44535
diff changeset
1074 'Point class with optimized _make() and _replace() without error-checking'
44643
d8e4342c145e Merged revisions 59883-59920 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44604
diff changeset
1075 __slots__ = ()
44568
305d7d2ce047 Copied doc for reload() from trunk's function.rst to imp.rst
Christian Heimes <christian@cheimes.de>
parents: 44535
diff changeset
1076 _make = classmethod(tuple.__new__)
305d7d2ce047 Copied doc for reload() from trunk's function.rst to imp.rst
Christian Heimes <christian@cheimes.de>
parents: 44535
diff changeset
1077 def _replace(self, _map=map, **kwds):
44604
94ab572ed6dd Merged revisions 59843-59863 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44587
diff changeset
1078 return self._make(_map(kwds.get, ('x', 'y'), self))
44568
305d7d2ce047 Copied doc for reload() from trunk's function.rst to imp.rst
Christian Heimes <christian@cheimes.de>
parents: 44535
diff changeset
1079
305d7d2ce047 Copied doc for reload() from trunk's function.rst to imp.rst
Christian Heimes <christian@cheimes.de>
parents: 44535
diff changeset
1080 print(Point(11, 22)._replace(x=100))
43877
1165d261fb05 Merged revisions 58947-59004 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43734
diff changeset
1081
44643
d8e4342c145e Merged revisions 59883-59920 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44604
diff changeset
1082 Point3D = namedtuple('Point3D', Point._fields + ('z',))
d8e4342c145e Merged revisions 59883-59920 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44604
diff changeset
1083 print(Point3D.__doc__)
d8e4342c145e Merged revisions 59883-59920 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44604
diff changeset
1084
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
diff changeset
1085 import doctest
43734
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43400
diff changeset
1086 TestResults = namedtuple('TestResults', 'failed attempted')
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
diff changeset
1087 print(TestResults(*doctest.testmod()))