blob: c731a9aef478b73b8623453462db19f77de26cb3 [file] [log] [blame]
Brett Cannon23a4a7b2008-05-12 00:56:28 +00001__all__ = ['deque', 'defaultdict', 'namedtuple', 'UserDict', 'UserList',
Raymond Hettinger2d32f632009-03-02 21:24:57 +00002 'UserString', 'Counter', 'OrderedDict']
Guido van Rossumcd16bf62007-06-13 18:07:49 +00003# For bootstrapping reasons, the collection ABCs are defined in _abcoll.py.
4# They should however be considered an integral part of collections.py.
5from _abcoll import *
6import _abcoll
7__all__ += _abcoll.__all__
8
Christian Heimes99170a52007-12-19 02:07:34 +00009from _collections import deque, defaultdict
10from operator import itemgetter as _itemgetter
11from keyword import iskeyword as _iskeyword
12import sys as _sys
Raymond Hettingerb8baf632009-01-14 02:20:07 +000013import heapq as _heapq
Raymond Hettingerea9f8db2009-03-02 21:28:41 +000014from itertools import repeat as _repeat, chain as _chain, starmap as _starmap
Raymond Hettinger2d32f632009-03-02 21:24:57 +000015
16################################################################################
17### OrderedDict
18################################################################################
19
20class OrderedDict(dict, MutableMapping):
21
22 def __init__(self, *args, **kwds):
23 if len(args) > 1:
24 raise TypeError('expected at most 1 arguments, got %d' % len(args))
25 if not hasattr(self, '_keys'):
26 self._keys = []
27 self.update(*args, **kwds)
28
29 def clear(self):
30 del self._keys[:]
31 dict.clear(self)
32
33 def __setitem__(self, key, value):
34 if key not in self:
35 self._keys.append(key)
36 dict.__setitem__(self, key, value)
37
38 def __delitem__(self, key):
39 dict.__delitem__(self, key)
40 self._keys.remove(key)
41
42 def __iter__(self):
43 return iter(self._keys)
44
45 def __reversed__(self):
46 return reversed(self._keys)
47
48 def popitem(self):
49 if not self:
50 raise KeyError('dictionary is empty')
51 key = self._keys.pop()
52 value = dict.pop(self, key)
53 return key, value
54
55 def __reduce__(self):
56 items = [[k, self[k]] for k in self]
57 inst_dict = vars(self).copy()
58 inst_dict.pop('_keys', None)
59 return (self.__class__, (items,), inst_dict)
60
61 setdefault = MutableMapping.setdefault
62 update = MutableMapping.update
63 pop = MutableMapping.pop
64 keys = MutableMapping.keys
65 values = MutableMapping.values
66 items = MutableMapping.items
67
68 def __repr__(self):
69 if not self:
70 return '%s()' % (self.__class__.__name__,)
71 return '%s(%r)' % (self.__class__.__name__, list(self.items()))
72
73 def copy(self):
74 return self.__class__(self)
75
76 @classmethod
77 def fromkeys(cls, iterable, value=None):
78 d = cls()
79 for key in iterable:
80 d[key] = value
81 return d
82
83 def __eq__(self, other):
84 if isinstance(other, OrderedDict):
Raymond Hettingerea9f8db2009-03-02 21:28:41 +000085 return len(self)==len(other) and all(p==q for p, q in zip(self.items(), other.items()))
Raymond Hettinger2d32f632009-03-02 21:24:57 +000086 return dict.__eq__(self, other)
87
Raymond Hettingerb8baf632009-01-14 02:20:07 +000088
Christian Heimes99170a52007-12-19 02:07:34 +000089
Raymond Hettinger48b8b662008-02-05 01:53:00 +000090################################################################################
91### namedtuple
92################################################################################
93
Benjamin Petersona86f2c02009-02-10 02:41:10 +000094def namedtuple(typename, field_names, verbose=False, rename=False):
Guido van Rossumd8faa362007-04-27 19:54:29 +000095 """Returns a new subclass of tuple with named fields.
96
Guido van Rossum8ce8a782007-11-01 19:42:39 +000097 >>> Point = namedtuple('Point', 'x y')
Thomas Wouters1b7f8912007-09-19 03:06:30 +000098 >>> Point.__doc__ # docstring for the new class
Guido van Rossumd8faa362007-04-27 19:54:29 +000099 'Point(x, y)'
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000100 >>> p = Point(11, y=22) # instantiate with positional args or keywords
Christian Heimes99170a52007-12-19 02:07:34 +0000101 >>> p[0] + p[1] # indexable like a plain tuple
Guido van Rossumd8faa362007-04-27 19:54:29 +0000102 33
Christian Heimes99170a52007-12-19 02:07:34 +0000103 >>> x, y = p # unpack like a regular tuple
Guido van Rossumd8faa362007-04-27 19:54:29 +0000104 >>> x, y
105 (11, 22)
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000106 >>> p.x + p.y # fields also accessable by name
Guido van Rossumd8faa362007-04-27 19:54:29 +0000107 33
Christian Heimes0449f632007-12-15 01:27:15 +0000108 >>> d = p._asdict() # convert to a dictionary
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000109 >>> d['x']
110 11
111 >>> Point(**d) # convert from a dictionary
Guido van Rossumd8faa362007-04-27 19:54:29 +0000112 Point(x=11, y=22)
Christian Heimes0449f632007-12-15 01:27:15 +0000113 >>> p._replace(x=100) # _replace() is like str.replace() but targets named fields
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000114 Point(x=100, y=22)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000115
116 """
117
Christian Heimes2380ac72008-01-09 00:17:24 +0000118 # Parse and validate the field names. Validation serves two purposes,
119 # generating informative error messages and preventing template injection attacks.
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000120 if isinstance(field_names, str):
121 field_names = field_names.replace(',', ' ').split() # names separated by whitespace and/or commas
Benjamin Petersone9bbc8b2008-09-28 02:06:32 +0000122 field_names = tuple(map(str, field_names))
Benjamin Petersona86f2c02009-02-10 02:41:10 +0000123 if rename:
124 names = list(field_names)
125 seen = set()
126 for i, name in enumerate(names):
127 if (not all(c.isalnum() or c=='_' for c in name) or _iskeyword(name)
128 or not name or name[0].isdigit() or name.startswith('_')
129 or name in seen):
130 names[i] = '_%d' % (i+1)
131 seen.add(name)
132 field_names = tuple(names)
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000133 for name in (typename,) + field_names:
Christian Heimesb9eccbf2007-12-05 20:18:38 +0000134 if not all(c.isalnum() or c=='_' for c in name):
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000135 raise ValueError('Type names and field names can only contain alphanumeric characters and underscores: %r' % name)
136 if _iskeyword(name):
137 raise ValueError('Type names and field names cannot be a keyword: %r' % name)
138 if name[0].isdigit():
139 raise ValueError('Type names and field names cannot start with a number: %r' % name)
140 seen_names = set()
141 for name in field_names:
Benjamin Petersona86f2c02009-02-10 02:41:10 +0000142 if name.startswith('_') and not rename:
Christian Heimes0449f632007-12-15 01:27:15 +0000143 raise ValueError('Field names cannot start with an underscore: %r' % name)
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000144 if name in seen_names:
145 raise ValueError('Encountered duplicate field name: %r' % name)
146 seen_names.add(name)
147
148 # Create and fill-in the class template
Christian Heimesfaf2f632008-01-06 16:59:19 +0000149 numfields = len(field_names)
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000150 argtxt = repr(field_names).replace("'", "")[1:-1] # tuple repr without parens or quotes
Guido van Rossumd59da4b2007-05-22 18:11:13 +0000151 reprtxt = ', '.join('%s=%%r' % name for name in field_names)
Christian Heimes99170a52007-12-19 02:07:34 +0000152 dicttxt = ', '.join('%r: t[%d]' % (name, pos) for pos, name in enumerate(field_names))
Guido van Rossumd59da4b2007-05-22 18:11:13 +0000153 template = '''class %(typename)s(tuple):
Christian Heimes0449f632007-12-15 01:27:15 +0000154 '%(typename)s(%(argtxt)s)' \n
155 __slots__ = () \n
Christian Heimesfaf2f632008-01-06 16:59:19 +0000156 _fields = %(field_names)r \n
Guido van Rossumd59da4b2007-05-22 18:11:13 +0000157 def __new__(cls, %(argtxt)s):
Christian Heimes0449f632007-12-15 01:27:15 +0000158 return tuple.__new__(cls, (%(argtxt)s)) \n
Christian Heimesfaf2f632008-01-06 16:59:19 +0000159 @classmethod
Christian Heimes043d6f62008-01-07 17:19:16 +0000160 def _make(cls, iterable, new=tuple.__new__, len=len):
Christian Heimesfaf2f632008-01-06 16:59:19 +0000161 'Make a new %(typename)s object from a sequence or iterable'
Christian Heimes043d6f62008-01-07 17:19:16 +0000162 result = new(cls, iterable)
Christian Heimesfaf2f632008-01-06 16:59:19 +0000163 if len(result) != %(numfields)d:
164 raise TypeError('Expected %(numfields)d arguments, got %%d' %% len(result))
165 return result \n
Guido van Rossumd59da4b2007-05-22 18:11:13 +0000166 def __repr__(self):
Christian Heimes0449f632007-12-15 01:27:15 +0000167 return '%(typename)s(%(reprtxt)s)' %% self \n
Christian Heimes99170a52007-12-19 02:07:34 +0000168 def _asdict(t):
Christian Heimes0449f632007-12-15 01:27:15 +0000169 'Return a new dict which maps field names to their values'
Christian Heimes99170a52007-12-19 02:07:34 +0000170 return {%(dicttxt)s} \n
Christian Heimes0449f632007-12-15 01:27:15 +0000171 def _replace(self, **kwds):
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000172 'Return a new %(typename)s object replacing specified fields with new values'
Christian Heimesfaf2f632008-01-06 16:59:19 +0000173 result = self._make(map(kwds.pop, %(field_names)r, self))
174 if kwds:
175 raise ValueError('Got unexpected field names: %%r' %% kwds.keys())
Georg Brandlc28e1fa2008-06-10 19:20:26 +0000176 return result \n
177 def __getnewargs__(self):
178 return tuple(self) \n\n''' % locals()
Guido van Rossumd59da4b2007-05-22 18:11:13 +0000179 for i, name in enumerate(field_names):
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000180 template += ' %s = property(itemgetter(%d))\n' % (name, i)
181 if verbose:
182 print(template)
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000183
Georg Brandlf08a9dd2008-06-10 16:57:31 +0000184 # Execute the template string in a temporary namespace and
185 # support tracing utilities by setting a value for frame.f_globals['__name__']
186 namespace = dict(itemgetter=_itemgetter, __name__='namedtuple_%s' % typename)
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000187 try:
188 exec(template, namespace)
189 except SyntaxError as e:
Christian Heimes99170a52007-12-19 02:07:34 +0000190 raise SyntaxError(e.msg + ':\n' + template) from e
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000191 result = namespace[typename]
192
193 # For pickling to work, the __module__ variable needs to be set to the frame
194 # where the named tuple is created. Bypass this step in enviroments where
195 # sys._getframe is not defined (Jython for example).
Guido van Rossumd59da4b2007-05-22 18:11:13 +0000196 if hasattr(_sys, '_getframe'):
Raymond Hettinger0f055172009-01-27 10:06:09 +0000197 result.__module__ = _sys._getframe(1).f_globals.get('__name__', '__main__')
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000198
Guido van Rossumd59da4b2007-05-22 18:11:13 +0000199 return result
Guido van Rossumd8faa362007-04-27 19:54:29 +0000200
Guido van Rossumd8faa362007-04-27 19:54:29 +0000201
Raymond Hettingerb8baf632009-01-14 02:20:07 +0000202########################################################################
203### Counter
204########################################################################
205
206class Counter(dict):
207 '''Dict subclass for counting hashable items. Sometimes called a bag
208 or multiset. Elements are stored as dictionary keys and their counts
209 are stored as dictionary values.
210
211 >>> c = Counter('abracadabra') # count elements from a string
212
213 >>> c.most_common(3) # three most common elements
214 [('a', 5), ('r', 2), ('b', 2)]
215 >>> sorted(c) # list all unique elements
216 ['a', 'b', 'c', 'd', 'r']
217 >>> ''.join(sorted(c.elements())) # list elements with repetitions
218 'aaaaabbcdrr'
219 >>> sum(c.values()) # total of all counts
220 11
221
222 >>> c['a'] # count of letter 'a'
223 5
224 >>> for elem in 'shazam': # update counts from an iterable
225 ... c[elem] += 1 # by adding 1 to each element's count
226 >>> c['a'] # now there are seven 'a'
227 7
228 >>> del c['r'] # remove all 'r'
229 >>> c['r'] # now there are zero 'r'
230 0
231
232 >>> d = Counter('simsalabim') # make another counter
233 >>> c.update(d) # add in the second counter
234 >>> c['a'] # now there are nine 'a'
235 9
236
237 >>> c.clear() # empty the counter
238 >>> c
239 Counter()
240
241 Note: If a count is set to zero or reduced to zero, it will remain
242 in the counter until the entry is deleted or the counter is cleared:
243
244 >>> c = Counter('aaabbc')
245 >>> c['b'] -= 2 # reduce the count of 'b' by two
246 >>> c.most_common() # 'b' is still in, but its count is zero
247 [('a', 3), ('c', 1), ('b', 0)]
248
249 '''
250 # References:
251 # http://en.wikipedia.org/wiki/Multiset
252 # http://www.gnu.org/software/smalltalk/manual-base/html_node/Bag.html
253 # http://www.demo2s.com/Tutorial/Cpp/0380__set-multiset/Catalog0380__set-multiset.htm
254 # http://code.activestate.com/recipes/259174/
255 # Knuth, TAOCP Vol. II section 4.6.3
256
Raymond Hettinger4d2073a2009-01-20 03:41:22 +0000257 def __init__(self, iterable=None, **kwds):
Raymond Hettingerb8baf632009-01-14 02:20:07 +0000258 '''Create a new, empty Counter object. And if given, count elements
259 from an input iterable. Or, initialize the count from another mapping
260 of elements to their counts.
261
262 >>> c = Counter() # a new, empty counter
263 >>> c = Counter('gallahad') # a new counter from an iterable
264 >>> c = Counter({'a': 4, 'b': 2}) # a new counter from a mapping
Raymond Hettinger4d2073a2009-01-20 03:41:22 +0000265 >>> c = Counter(a=4, b=2) # a new counter from keyword args
Raymond Hettingerb8baf632009-01-14 02:20:07 +0000266
267 '''
Raymond Hettinger4d2073a2009-01-20 03:41:22 +0000268 self.update(iterable, **kwds)
Raymond Hettingerb8baf632009-01-14 02:20:07 +0000269
270 def __missing__(self, key):
271 'The count of elements not in the Counter is zero.'
272 # Needed so that self[missing_item] does not raise KeyError
273 return 0
274
275 def most_common(self, n=None):
276 '''List the n most common elements and their counts from the most
277 common to the least. If n is None, then list all element counts.
278
279 >>> Counter('abracadabra').most_common(3)
280 [('a', 5), ('r', 2), ('b', 2)]
281
282 '''
283 # Emulate Bag.sortedByCount from Smalltalk
284 if n is None:
285 return sorted(self.items(), key=_itemgetter(1), reverse=True)
286 return _heapq.nlargest(n, self.items(), key=_itemgetter(1))
287
288 def elements(self):
289 '''Iterator over elements repeating each as many times as its count.
290
291 >>> c = Counter('ABCABC')
292 >>> sorted(c.elements())
293 ['A', 'A', 'B', 'B', 'C', 'C']
294
295 # Knuth's example for prime factors of 1836: 2**2 * 3**3 * 17**1
296 >>> prime_factors = Counter({2: 2, 3: 3, 17: 1})
297 >>> product = 1
298 >>> for factor in prime_factors.elements(): # loop over factors
299 ... product *= factor # and multiply them
300 >>> product
301 1836
302
303 Note, if an element's count has been set to zero or is a negative
304 number, elements() will ignore it.
305
306 '''
307 # Emulate Bag.do from Smalltalk and Multiset.begin from C++.
308 return _chain.from_iterable(_starmap(_repeat, self.items()))
309
310 # Override dict methods where necessary
311
312 @classmethod
313 def fromkeys(cls, iterable, v=None):
314 # There is no equivalent method for counters because setting v=1
315 # means that no element can have a count greater than one.
316 raise NotImplementedError(
317 'Counter.fromkeys() is undefined. Use Counter(iterable) instead.')
318
Raymond Hettinger4d2073a2009-01-20 03:41:22 +0000319 def update(self, iterable=None, **kwds):
Raymond Hettingerb8baf632009-01-14 02:20:07 +0000320 '''Like dict.update() but add counts instead of replacing them.
321
322 Source can be an iterable, a dictionary, or another Counter instance.
323
324 >>> c = Counter('which')
325 >>> c.update('witch') # add elements from another iterable
326 >>> d = Counter('watch')
327 >>> c.update(d) # add elements from another counter
328 >>> c['h'] # four 'h' in which, witch, and watch
329 4
330
331 '''
332 # The regular dict.update() operation makes no sense here because the
333 # replace behavior results in the some of original untouched counts
334 # being mixed-in with all of the other counts for a mismash that
335 # doesn't have a straight-forward interpretation in most counting
Raymond Hettinger4d2073a2009-01-20 03:41:22 +0000336 # contexts. Instead, we implement straight-addition. Both the inputs
337 # and outputs are allowed to contain zero and negative counts.
Raymond Hettingerb8baf632009-01-14 02:20:07 +0000338
339 if iterable is not None:
340 if isinstance(iterable, Mapping):
Raymond Hettingerdd01f8f2009-01-22 09:09:55 +0000341 if self:
342 for elem, count in iterable.items():
343 self[elem] += count
344 else:
345 dict.update(self, iterable) # fast path when counter is empty
Raymond Hettingerb8baf632009-01-14 02:20:07 +0000346 else:
347 for elem in iterable:
348 self[elem] += 1
Raymond Hettinger4d2073a2009-01-20 03:41:22 +0000349 if kwds:
350 self.update(kwds)
Raymond Hettingerb8baf632009-01-14 02:20:07 +0000351
352 def copy(self):
353 'Like dict.copy() but returns a Counter instance instead of a dict.'
354 return Counter(self)
355
Raymond Hettinger4d2073a2009-01-20 03:41:22 +0000356 def __delitem__(self, elem):
357 'Like dict.__delitem__() but does not raise KeyError for missing values.'
358 if elem in self:
359 dict.__delitem__(self, elem)
360
Raymond Hettingerb8baf632009-01-14 02:20:07 +0000361 def __repr__(self):
362 if not self:
363 return '%s()' % self.__class__.__name__
364 items = ', '.join(map('%r: %r'.__mod__, self.most_common()))
365 return '%s({%s})' % (self.__class__.__name__, items)
366
Raymond Hettinger4d2073a2009-01-20 03:41:22 +0000367 # Multiset-style mathematical operations discussed in:
368 # Knuth TAOCP Volume II section 4.6.3 exercise 19
369 # and at http://en.wikipedia.org/wiki/Multiset
370 #
Raymond Hettinger4d2073a2009-01-20 03:41:22 +0000371 # Outputs guaranteed to only include positive counts.
372 #
373 # To strip negative and zero counts, add-in an empty counter:
374 # c += Counter()
375
376 def __add__(self, other):
377 '''Add counts from two counters.
378
379 >>> Counter('abbb') + Counter('bcc')
380 Counter({'b': 4, 'c': 2, 'a': 1})
381
382 '''
383 if not isinstance(other, Counter):
384 return NotImplemented
385 result = Counter()
386 for elem in set(self) | set(other):
387 newcount = self[elem] + other[elem]
388 if newcount > 0:
389 result[elem] = newcount
390 return result
391
392 def __sub__(self, other):
393 ''' Subtract count, but keep only results with positive counts.
394
395 >>> Counter('abbbc') - Counter('bccd')
396 Counter({'b': 2, 'a': 1})
397
398 '''
399 if not isinstance(other, Counter):
400 return NotImplemented
401 result = Counter()
Raymond Hettingere0d1b9f2009-01-21 20:36:27 +0000402 for elem in set(self) | set(other):
403 newcount = self[elem] - other[elem]
Raymond Hettinger4d2073a2009-01-20 03:41:22 +0000404 if newcount > 0:
405 result[elem] = newcount
406 return result
407
408 def __or__(self, other):
409 '''Union is the maximum of value in either of the input counters.
410
411 >>> Counter('abbb') | Counter('bcc')
412 Counter({'b': 3, 'c': 2, 'a': 1})
413
414 '''
415 if not isinstance(other, Counter):
416 return NotImplemented
417 _max = max
418 result = Counter()
419 for elem in set(self) | set(other):
420 newcount = _max(self[elem], other[elem])
421 if newcount > 0:
422 result[elem] = newcount
423 return result
424
425 def __and__(self, other):
426 ''' Intersection is the minimum of corresponding counts.
427
428 >>> Counter('abbb') & Counter('bcc')
429 Counter({'b': 1})
430
431 '''
432 if not isinstance(other, Counter):
433 return NotImplemented
434 _min = min
435 result = Counter()
436 if len(self) < len(other):
437 self, other = other, self
438 for elem in filter(self.__contains__, other):
439 newcount = _min(self[elem], other[elem])
440 if newcount > 0:
441 result[elem] = newcount
442 return result
443
Guido van Rossumd8faa362007-04-27 19:54:29 +0000444
Raymond Hettinger48b8b662008-02-05 01:53:00 +0000445################################################################################
446### UserDict
447################################################################################
Guido van Rossumd8faa362007-04-27 19:54:29 +0000448
Raymond Hettinger48b8b662008-02-05 01:53:00 +0000449class UserDict(MutableMapping):
450
451 # Start by filling-out the abstract methods
452 def __init__(self, dict=None, **kwargs):
453 self.data = {}
454 if dict is not None:
455 self.update(dict)
456 if len(kwargs):
457 self.update(kwargs)
458 def __len__(self): return len(self.data)
459 def __getitem__(self, key):
460 if key in self.data:
461 return self.data[key]
462 if hasattr(self.__class__, "__missing__"):
463 return self.__class__.__missing__(self, key)
464 raise KeyError(key)
465 def __setitem__(self, key, item): self.data[key] = item
466 def __delitem__(self, key): del self.data[key]
467 def __iter__(self):
468 return iter(self.data)
469
Raymond Hettinger554c8b82008-02-05 22:54:43 +0000470 # Modify __contains__ to work correctly when __missing__ is present
471 def __contains__(self, key):
472 return key in self.data
Raymond Hettinger48b8b662008-02-05 01:53:00 +0000473
474 # Now, add the methods in dicts but not in MutableMapping
475 def __repr__(self): return repr(self.data)
476 def copy(self):
477 if self.__class__ is UserDict:
478 return UserDict(self.data.copy())
479 import copy
480 data = self.data
481 try:
482 self.data = {}
483 c = copy.copy(self)
484 finally:
485 self.data = data
486 c.update(self)
487 return c
488 @classmethod
489 def fromkeys(cls, iterable, value=None):
490 d = cls()
491 for key in iterable:
492 d[key] = value
493 return d
494
Raymond Hettinger48b8b662008-02-05 01:53:00 +0000495
496
497################################################################################
Raymond Hettinger53dbe392008-02-12 20:03:09 +0000498### UserList
499################################################################################
500
501class UserList(MutableSequence):
502 """A more or less complete user-defined wrapper around list objects."""
503 def __init__(self, initlist=None):
504 self.data = []
505 if initlist is not None:
506 # XXX should this accept an arbitrary sequence?
507 if type(initlist) == type(self.data):
508 self.data[:] = initlist
509 elif isinstance(initlist, UserList):
510 self.data[:] = initlist.data[:]
511 else:
512 self.data = list(initlist)
513 def __repr__(self): return repr(self.data)
514 def __lt__(self, other): return self.data < self.__cast(other)
515 def __le__(self, other): return self.data <= self.__cast(other)
516 def __eq__(self, other): return self.data == self.__cast(other)
517 def __ne__(self, other): return self.data != self.__cast(other)
518 def __gt__(self, other): return self.data > self.__cast(other)
519 def __ge__(self, other): return self.data >= self.__cast(other)
520 def __cast(self, other):
521 return other.data if isinstance(other, UserList) else other
Raymond Hettinger53dbe392008-02-12 20:03:09 +0000522 def __contains__(self, item): return item in self.data
523 def __len__(self): return len(self.data)
524 def __getitem__(self, i): return self.data[i]
525 def __setitem__(self, i, item): self.data[i] = item
526 def __delitem__(self, i): del self.data[i]
527 def __add__(self, other):
528 if isinstance(other, UserList):
529 return self.__class__(self.data + other.data)
530 elif isinstance(other, type(self.data)):
531 return self.__class__(self.data + other)
532 return self.__class__(self.data + list(other))
533 def __radd__(self, other):
534 if isinstance(other, UserList):
535 return self.__class__(other.data + self.data)
536 elif isinstance(other, type(self.data)):
537 return self.__class__(other + self.data)
538 return self.__class__(list(other) + self.data)
539 def __iadd__(self, other):
540 if isinstance(other, UserList):
541 self.data += other.data
542 elif isinstance(other, type(self.data)):
543 self.data += other
544 else:
545 self.data += list(other)
546 return self
547 def __mul__(self, n):
548 return self.__class__(self.data*n)
549 __rmul__ = __mul__
550 def __imul__(self, n):
551 self.data *= n
552 return self
553 def append(self, item): self.data.append(item)
554 def insert(self, i, item): self.data.insert(i, item)
555 def pop(self, i=-1): return self.data.pop(i)
556 def remove(self, item): self.data.remove(item)
557 def count(self, item): return self.data.count(item)
558 def index(self, item, *args): return self.data.index(item, *args)
559 def reverse(self): self.data.reverse()
560 def sort(self, *args, **kwds): self.data.sort(*args, **kwds)
561 def extend(self, other):
562 if isinstance(other, UserList):
563 self.data.extend(other.data)
564 else:
565 self.data.extend(other)
566
567
568
569################################################################################
Raymond Hettingerb3a65f82008-02-21 22:11:37 +0000570### UserString
571################################################################################
572
573class UserString(Sequence):
574 def __init__(self, seq):
575 if isinstance(seq, str):
576 self.data = seq
577 elif isinstance(seq, UserString):
578 self.data = seq.data[:]
579 else:
580 self.data = str(seq)
581 def __str__(self): return str(self.data)
582 def __repr__(self): return repr(self.data)
583 def __int__(self): return int(self.data)
Raymond Hettingerb3a65f82008-02-21 22:11:37 +0000584 def __float__(self): return float(self.data)
585 def __complex__(self): return complex(self.data)
586 def __hash__(self): return hash(self.data)
587
588 def __eq__(self, string):
589 if isinstance(string, UserString):
590 return self.data == string.data
591 return self.data == string
592 def __ne__(self, string):
593 if isinstance(string, UserString):
594 return self.data != string.data
595 return self.data != string
596 def __lt__(self, string):
597 if isinstance(string, UserString):
598 return self.data < string.data
599 return self.data < string
600 def __le__(self, string):
601 if isinstance(string, UserString):
602 return self.data <= string.data
603 return self.data <= string
604 def __gt__(self, string):
605 if isinstance(string, UserString):
606 return self.data > string.data
607 return self.data > string
608 def __ge__(self, string):
609 if isinstance(string, UserString):
610 return self.data >= string.data
611 return self.data >= string
612
613 def __contains__(self, char):
614 if isinstance(char, UserString):
615 char = char.data
616 return char in self.data
617
618 def __len__(self): return len(self.data)
619 def __getitem__(self, index): return self.__class__(self.data[index])
620 def __add__(self, other):
621 if isinstance(other, UserString):
622 return self.__class__(self.data + other.data)
623 elif isinstance(other, str):
624 return self.__class__(self.data + other)
625 return self.__class__(self.data + str(other))
626 def __radd__(self, other):
627 if isinstance(other, str):
628 return self.__class__(other + self.data)
629 return self.__class__(str(other) + self.data)
630 def __mul__(self, n):
631 return self.__class__(self.data*n)
632 __rmul__ = __mul__
633 def __mod__(self, args):
634 return self.__class__(self.data % args)
635
636 # the following methods are defined in alphabetical order:
637 def capitalize(self): return self.__class__(self.data.capitalize())
638 def center(self, width, *args):
639 return self.__class__(self.data.center(width, *args))
640 def count(self, sub, start=0, end=_sys.maxsize):
641 if isinstance(sub, UserString):
642 sub = sub.data
643 return self.data.count(sub, start, end)
644 def encode(self, encoding=None, errors=None): # XXX improve this?
645 if encoding:
646 if errors:
647 return self.__class__(self.data.encode(encoding, errors))
648 return self.__class__(self.data.encode(encoding))
649 return self.__class__(self.data.encode())
650 def endswith(self, suffix, start=0, end=_sys.maxsize):
651 return self.data.endswith(suffix, start, end)
652 def expandtabs(self, tabsize=8):
653 return self.__class__(self.data.expandtabs(tabsize))
654 def find(self, sub, start=0, end=_sys.maxsize):
655 if isinstance(sub, UserString):
656 sub = sub.data
657 return self.data.find(sub, start, end)
658 def format(self, *args, **kwds):
659 return self.data.format(*args, **kwds)
660 def index(self, sub, start=0, end=_sys.maxsize):
661 return self.data.index(sub, start, end)
662 def isalpha(self): return self.data.isalpha()
663 def isalnum(self): return self.data.isalnum()
664 def isdecimal(self): return self.data.isdecimal()
665 def isdigit(self): return self.data.isdigit()
666 def isidentifier(self): return self.data.isidentifier()
667 def islower(self): return self.data.islower()
668 def isnumeric(self): return self.data.isnumeric()
669 def isspace(self): return self.data.isspace()
670 def istitle(self): return self.data.istitle()
671 def isupper(self): return self.data.isupper()
672 def join(self, seq): return self.data.join(seq)
673 def ljust(self, width, *args):
674 return self.__class__(self.data.ljust(width, *args))
675 def lower(self): return self.__class__(self.data.lower())
676 def lstrip(self, chars=None): return self.__class__(self.data.lstrip(chars))
677 def partition(self, sep):
678 return self.data.partition(sep)
679 def replace(self, old, new, maxsplit=-1):
680 if isinstance(old, UserString):
681 old = old.data
682 if isinstance(new, UserString):
683 new = new.data
684 return self.__class__(self.data.replace(old, new, maxsplit))
685 def rfind(self, sub, start=0, end=_sys.maxsize):
686 return self.data.rfind(sub, start, end)
687 def rindex(self, sub, start=0, end=_sys.maxsize):
688 return self.data.rindex(sub, start, end)
689 def rjust(self, width, *args):
690 return self.__class__(self.data.rjust(width, *args))
691 def rpartition(self, sep):
692 return self.data.rpartition(sep)
693 def rstrip(self, chars=None):
694 return self.__class__(self.data.rstrip(chars))
695 def split(self, sep=None, maxsplit=-1):
696 return self.data.split(sep, maxsplit)
697 def rsplit(self, sep=None, maxsplit=-1):
698 return self.data.rsplit(sep, maxsplit)
699 def splitlines(self, keepends=0): return self.data.splitlines(keepends)
700 def startswith(self, prefix, start=0, end=_sys.maxsize):
701 return self.data.startswith(prefix, start, end)
702 def strip(self, chars=None): return self.__class__(self.data.strip(chars))
703 def swapcase(self): return self.__class__(self.data.swapcase())
704 def title(self): return self.__class__(self.data.title())
705 def translate(self, *args):
706 return self.__class__(self.data.translate(*args))
707 def upper(self): return self.__class__(self.data.upper())
708 def zfill(self, width): return self.__class__(self.data.zfill(width))
709
710
711
712################################################################################
Raymond Hettinger48b8b662008-02-05 01:53:00 +0000713### Simple tests
714################################################################################
Guido van Rossumd8faa362007-04-27 19:54:29 +0000715
716if __name__ == '__main__':
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000717 # verify that instances can be pickled
Guido van Rossum99603b02007-07-20 00:22:32 +0000718 from pickle import loads, dumps
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000719 Point = namedtuple('Point', 'x, y', True)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000720 p = Point(x=10, y=20)
721 assert p == loads(dumps(p))
722
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000723 # test and demonstrate ability to override methods
Christian Heimes043d6f62008-01-07 17:19:16 +0000724 class Point(namedtuple('Point', 'x y')):
Christian Heimes25bb7832008-01-11 16:17:00 +0000725 __slots__ = ()
Christian Heimes043d6f62008-01-07 17:19:16 +0000726 @property
727 def hypot(self):
728 return (self.x ** 2 + self.y ** 2) ** 0.5
Christian Heimes790c8232008-01-07 21:14:23 +0000729 def __str__(self):
Christian Heimes25bb7832008-01-11 16:17:00 +0000730 return 'Point: x=%6.3f y=%6.3f hypot=%6.3f' % (self.x, self.y, self.hypot)
Christian Heimes043d6f62008-01-07 17:19:16 +0000731
Christian Heimes25bb7832008-01-11 16:17:00 +0000732 for p in Point(3, 4), Point(14, 5/7.):
Christian Heimes790c8232008-01-07 21:14:23 +0000733 print (p)
Christian Heimes043d6f62008-01-07 17:19:16 +0000734
735 class Point(namedtuple('Point', 'x y')):
736 'Point class with optimized _make() and _replace() without error-checking'
Christian Heimes25bb7832008-01-11 16:17:00 +0000737 __slots__ = ()
Christian Heimes043d6f62008-01-07 17:19:16 +0000738 _make = classmethod(tuple.__new__)
739 def _replace(self, _map=map, **kwds):
Christian Heimes2380ac72008-01-09 00:17:24 +0000740 return self._make(_map(kwds.get, ('x', 'y'), self))
Christian Heimes043d6f62008-01-07 17:19:16 +0000741
742 print(Point(11, 22)._replace(x=100))
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000743
Christian Heimes25bb7832008-01-11 16:17:00 +0000744 Point3D = namedtuple('Point3D', Point._fields + ('z',))
745 print(Point3D.__doc__)
746
Guido van Rossumd8faa362007-04-27 19:54:29 +0000747 import doctest
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000748 TestResults = namedtuple('TestResults', 'failed attempted')
Guido van Rossumd8faa362007-04-27 19:54:29 +0000749 print(TestResults(*doctest.testmod()))