blob: c3faa9a44dffce5ff38c72b2934d116d6195ddb9 [file] [log] [blame]
Brett Cannon23a4a7b2008-05-12 00:56:28 +00001__all__ = ['deque', 'defaultdict', 'namedtuple', 'UserDict', 'UserList',
2 'UserString']
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
13
Raymond Hettinger48b8b662008-02-05 01:53:00 +000014################################################################################
15### namedtuple
16################################################################################
17
Guido van Rossum8ce8a782007-11-01 19:42:39 +000018def namedtuple(typename, field_names, verbose=False):
Guido van Rossumd8faa362007-04-27 19:54:29 +000019 """Returns a new subclass of tuple with named fields.
20
Guido van Rossum8ce8a782007-11-01 19:42:39 +000021 >>> Point = namedtuple('Point', 'x y')
Thomas Wouters1b7f8912007-09-19 03:06:30 +000022 >>> Point.__doc__ # docstring for the new class
Guido van Rossumd8faa362007-04-27 19:54:29 +000023 'Point(x, y)'
Thomas Wouters1b7f8912007-09-19 03:06:30 +000024 >>> p = Point(11, y=22) # instantiate with positional args or keywords
Christian Heimes99170a52007-12-19 02:07:34 +000025 >>> p[0] + p[1] # indexable like a plain tuple
Guido van Rossumd8faa362007-04-27 19:54:29 +000026 33
Christian Heimes99170a52007-12-19 02:07:34 +000027 >>> x, y = p # unpack like a regular tuple
Guido van Rossumd8faa362007-04-27 19:54:29 +000028 >>> x, y
29 (11, 22)
Thomas Wouters1b7f8912007-09-19 03:06:30 +000030 >>> p.x + p.y # fields also accessable by name
Guido van Rossumd8faa362007-04-27 19:54:29 +000031 33
Christian Heimes0449f632007-12-15 01:27:15 +000032 >>> d = p._asdict() # convert to a dictionary
Guido van Rossum8ce8a782007-11-01 19:42:39 +000033 >>> d['x']
34 11
35 >>> Point(**d) # convert from a dictionary
Guido van Rossumd8faa362007-04-27 19:54:29 +000036 Point(x=11, y=22)
Christian Heimes0449f632007-12-15 01:27:15 +000037 >>> p._replace(x=100) # _replace() is like str.replace() but targets named fields
Thomas Wouters1b7f8912007-09-19 03:06:30 +000038 Point(x=100, y=22)
Guido van Rossumd8faa362007-04-27 19:54:29 +000039
40 """
41
Christian Heimes2380ac72008-01-09 00:17:24 +000042 # Parse and validate the field names. Validation serves two purposes,
43 # generating informative error messages and preventing template injection attacks.
Guido van Rossum8ce8a782007-11-01 19:42:39 +000044 if isinstance(field_names, str):
45 field_names = field_names.replace(',', ' ').split() # names separated by whitespace and/or commas
Benjamin Petersone9bbc8b2008-09-28 02:06:32 +000046 field_names = tuple(map(str, field_names))
Guido van Rossum8ce8a782007-11-01 19:42:39 +000047 for name in (typename,) + field_names:
Christian Heimesb9eccbf2007-12-05 20:18:38 +000048 if not all(c.isalnum() or c=='_' for c in name):
Guido van Rossum8ce8a782007-11-01 19:42:39 +000049 raise ValueError('Type names and field names can only contain alphanumeric characters and underscores: %r' % name)
50 if _iskeyword(name):
51 raise ValueError('Type names and field names cannot be a keyword: %r' % name)
52 if name[0].isdigit():
53 raise ValueError('Type names and field names cannot start with a number: %r' % name)
54 seen_names = set()
55 for name in field_names:
Christian Heimes0449f632007-12-15 01:27:15 +000056 if name.startswith('_'):
57 raise ValueError('Field names cannot start with an underscore: %r' % name)
Guido van Rossum8ce8a782007-11-01 19:42:39 +000058 if name in seen_names:
59 raise ValueError('Encountered duplicate field name: %r' % name)
60 seen_names.add(name)
61
62 # Create and fill-in the class template
Christian Heimesfaf2f632008-01-06 16:59:19 +000063 numfields = len(field_names)
Thomas Wouters1b7f8912007-09-19 03:06:30 +000064 argtxt = repr(field_names).replace("'", "")[1:-1] # tuple repr without parens or quotes
Guido van Rossumd59da4b2007-05-22 18:11:13 +000065 reprtxt = ', '.join('%s=%%r' % name for name in field_names)
Christian Heimes99170a52007-12-19 02:07:34 +000066 dicttxt = ', '.join('%r: t[%d]' % (name, pos) for pos, name in enumerate(field_names))
Guido van Rossumd59da4b2007-05-22 18:11:13 +000067 template = '''class %(typename)s(tuple):
Christian Heimes0449f632007-12-15 01:27:15 +000068 '%(typename)s(%(argtxt)s)' \n
69 __slots__ = () \n
Christian Heimesfaf2f632008-01-06 16:59:19 +000070 _fields = %(field_names)r \n
Guido van Rossumd59da4b2007-05-22 18:11:13 +000071 def __new__(cls, %(argtxt)s):
Christian Heimes0449f632007-12-15 01:27:15 +000072 return tuple.__new__(cls, (%(argtxt)s)) \n
Christian Heimesfaf2f632008-01-06 16:59:19 +000073 @classmethod
Christian Heimes043d6f62008-01-07 17:19:16 +000074 def _make(cls, iterable, new=tuple.__new__, len=len):
Christian Heimesfaf2f632008-01-06 16:59:19 +000075 'Make a new %(typename)s object from a sequence or iterable'
Christian Heimes043d6f62008-01-07 17:19:16 +000076 result = new(cls, iterable)
Christian Heimesfaf2f632008-01-06 16:59:19 +000077 if len(result) != %(numfields)d:
78 raise TypeError('Expected %(numfields)d arguments, got %%d' %% len(result))
79 return result \n
Guido van Rossumd59da4b2007-05-22 18:11:13 +000080 def __repr__(self):
Christian Heimes0449f632007-12-15 01:27:15 +000081 return '%(typename)s(%(reprtxt)s)' %% self \n
Christian Heimes99170a52007-12-19 02:07:34 +000082 def _asdict(t):
Christian Heimes0449f632007-12-15 01:27:15 +000083 'Return a new dict which maps field names to their values'
Christian Heimes99170a52007-12-19 02:07:34 +000084 return {%(dicttxt)s} \n
Christian Heimes0449f632007-12-15 01:27:15 +000085 def _replace(self, **kwds):
Guido van Rossum3d392eb2007-11-16 00:35:22 +000086 'Return a new %(typename)s object replacing specified fields with new values'
Christian Heimesfaf2f632008-01-06 16:59:19 +000087 result = self._make(map(kwds.pop, %(field_names)r, self))
88 if kwds:
89 raise ValueError('Got unexpected field names: %%r' %% kwds.keys())
Georg Brandlc28e1fa2008-06-10 19:20:26 +000090 return result \n
91 def __getnewargs__(self):
92 return tuple(self) \n\n''' % locals()
Guido van Rossumd59da4b2007-05-22 18:11:13 +000093 for i, name in enumerate(field_names):
Thomas Wouters1b7f8912007-09-19 03:06:30 +000094 template += ' %s = property(itemgetter(%d))\n' % (name, i)
95 if verbose:
96 print(template)
Guido van Rossum8ce8a782007-11-01 19:42:39 +000097
Georg Brandlf08a9dd2008-06-10 16:57:31 +000098 # Execute the template string in a temporary namespace and
99 # support tracing utilities by setting a value for frame.f_globals['__name__']
100 namespace = dict(itemgetter=_itemgetter, __name__='namedtuple_%s' % typename)
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000101 try:
102 exec(template, namespace)
103 except SyntaxError as e:
Christian Heimes99170a52007-12-19 02:07:34 +0000104 raise SyntaxError(e.msg + ':\n' + template) from e
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000105 result = namespace[typename]
106
107 # For pickling to work, the __module__ variable needs to be set to the frame
108 # where the named tuple is created. Bypass this step in enviroments where
109 # sys._getframe is not defined (Jython for example).
Guido van Rossumd59da4b2007-05-22 18:11:13 +0000110 if hasattr(_sys, '_getframe'):
111 result.__module__ = _sys._getframe(1).f_globals['__name__']
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000112
Guido van Rossumd59da4b2007-05-22 18:11:13 +0000113 return result
Guido van Rossumd8faa362007-04-27 19:54:29 +0000114
Guido van Rossumd8faa362007-04-27 19:54:29 +0000115
Guido van Rossumd8faa362007-04-27 19:54:29 +0000116
Raymond Hettinger48b8b662008-02-05 01:53:00 +0000117################################################################################
118### UserDict
119################################################################################
Guido van Rossumd8faa362007-04-27 19:54:29 +0000120
Raymond Hettinger48b8b662008-02-05 01:53:00 +0000121class UserDict(MutableMapping):
122
123 # Start by filling-out the abstract methods
124 def __init__(self, dict=None, **kwargs):
125 self.data = {}
126 if dict is not None:
127 self.update(dict)
128 if len(kwargs):
129 self.update(kwargs)
130 def __len__(self): return len(self.data)
131 def __getitem__(self, key):
132 if key in self.data:
133 return self.data[key]
134 if hasattr(self.__class__, "__missing__"):
135 return self.__class__.__missing__(self, key)
136 raise KeyError(key)
137 def __setitem__(self, key, item): self.data[key] = item
138 def __delitem__(self, key): del self.data[key]
139 def __iter__(self):
140 return iter(self.data)
141
Raymond Hettinger554c8b82008-02-05 22:54:43 +0000142 # Modify __contains__ to work correctly when __missing__ is present
143 def __contains__(self, key):
144 return key in self.data
Raymond Hettinger48b8b662008-02-05 01:53:00 +0000145
146 # Now, add the methods in dicts but not in MutableMapping
147 def __repr__(self): return repr(self.data)
148 def copy(self):
149 if self.__class__ is UserDict:
150 return UserDict(self.data.copy())
151 import copy
152 data = self.data
153 try:
154 self.data = {}
155 c = copy.copy(self)
156 finally:
157 self.data = data
158 c.update(self)
159 return c
160 @classmethod
161 def fromkeys(cls, iterable, value=None):
162 d = cls()
163 for key in iterable:
164 d[key] = value
165 return d
166
Raymond Hettinger48b8b662008-02-05 01:53:00 +0000167
168
169################################################################################
Raymond Hettinger53dbe392008-02-12 20:03:09 +0000170### UserList
171################################################################################
172
173class UserList(MutableSequence):
174 """A more or less complete user-defined wrapper around list objects."""
175 def __init__(self, initlist=None):
176 self.data = []
177 if initlist is not None:
178 # XXX should this accept an arbitrary sequence?
179 if type(initlist) == type(self.data):
180 self.data[:] = initlist
181 elif isinstance(initlist, UserList):
182 self.data[:] = initlist.data[:]
183 else:
184 self.data = list(initlist)
185 def __repr__(self): return repr(self.data)
186 def __lt__(self, other): return self.data < self.__cast(other)
187 def __le__(self, other): return self.data <= self.__cast(other)
188 def __eq__(self, other): return self.data == self.__cast(other)
189 def __ne__(self, other): return self.data != self.__cast(other)
190 def __gt__(self, other): return self.data > self.__cast(other)
191 def __ge__(self, other): return self.data >= self.__cast(other)
192 def __cast(self, other):
193 return other.data if isinstance(other, UserList) else other
194 def __cmp__(self, other):
195 return cmp(self.data, self.__cast(other))
196 def __contains__(self, item): return item in self.data
197 def __len__(self): return len(self.data)
198 def __getitem__(self, i): return self.data[i]
199 def __setitem__(self, i, item): self.data[i] = item
200 def __delitem__(self, i): del self.data[i]
201 def __add__(self, other):
202 if isinstance(other, UserList):
203 return self.__class__(self.data + other.data)
204 elif isinstance(other, type(self.data)):
205 return self.__class__(self.data + other)
206 return self.__class__(self.data + list(other))
207 def __radd__(self, other):
208 if isinstance(other, UserList):
209 return self.__class__(other.data + self.data)
210 elif isinstance(other, type(self.data)):
211 return self.__class__(other + self.data)
212 return self.__class__(list(other) + self.data)
213 def __iadd__(self, other):
214 if isinstance(other, UserList):
215 self.data += other.data
216 elif isinstance(other, type(self.data)):
217 self.data += other
218 else:
219 self.data += list(other)
220 return self
221 def __mul__(self, n):
222 return self.__class__(self.data*n)
223 __rmul__ = __mul__
224 def __imul__(self, n):
225 self.data *= n
226 return self
227 def append(self, item): self.data.append(item)
228 def insert(self, i, item): self.data.insert(i, item)
229 def pop(self, i=-1): return self.data.pop(i)
230 def remove(self, item): self.data.remove(item)
231 def count(self, item): return self.data.count(item)
232 def index(self, item, *args): return self.data.index(item, *args)
233 def reverse(self): self.data.reverse()
234 def sort(self, *args, **kwds): self.data.sort(*args, **kwds)
235 def extend(self, other):
236 if isinstance(other, UserList):
237 self.data.extend(other.data)
238 else:
239 self.data.extend(other)
240
241
242
243################################################################################
Raymond Hettingerb3a65f82008-02-21 22:11:37 +0000244### UserString
245################################################################################
246
247class UserString(Sequence):
248 def __init__(self, seq):
249 if isinstance(seq, str):
250 self.data = seq
251 elif isinstance(seq, UserString):
252 self.data = seq.data[:]
253 else:
254 self.data = str(seq)
255 def __str__(self): return str(self.data)
256 def __repr__(self): return repr(self.data)
257 def __int__(self): return int(self.data)
Raymond Hettingerb3a65f82008-02-21 22:11:37 +0000258 def __float__(self): return float(self.data)
259 def __complex__(self): return complex(self.data)
260 def __hash__(self): return hash(self.data)
261
262 def __eq__(self, string):
263 if isinstance(string, UserString):
264 return self.data == string.data
265 return self.data == string
266 def __ne__(self, string):
267 if isinstance(string, UserString):
268 return self.data != string.data
269 return self.data != string
270 def __lt__(self, string):
271 if isinstance(string, UserString):
272 return self.data < string.data
273 return self.data < string
274 def __le__(self, string):
275 if isinstance(string, UserString):
276 return self.data <= string.data
277 return self.data <= string
278 def __gt__(self, string):
279 if isinstance(string, UserString):
280 return self.data > string.data
281 return self.data > string
282 def __ge__(self, string):
283 if isinstance(string, UserString):
284 return self.data >= string.data
285 return self.data >= string
286
287 def __contains__(self, char):
288 if isinstance(char, UserString):
289 char = char.data
290 return char in self.data
291
292 def __len__(self): return len(self.data)
293 def __getitem__(self, index): return self.__class__(self.data[index])
294 def __add__(self, other):
295 if isinstance(other, UserString):
296 return self.__class__(self.data + other.data)
297 elif isinstance(other, str):
298 return self.__class__(self.data + other)
299 return self.__class__(self.data + str(other))
300 def __radd__(self, other):
301 if isinstance(other, str):
302 return self.__class__(other + self.data)
303 return self.__class__(str(other) + self.data)
304 def __mul__(self, n):
305 return self.__class__(self.data*n)
306 __rmul__ = __mul__
307 def __mod__(self, args):
308 return self.__class__(self.data % args)
309
310 # the following methods are defined in alphabetical order:
311 def capitalize(self): return self.__class__(self.data.capitalize())
312 def center(self, width, *args):
313 return self.__class__(self.data.center(width, *args))
314 def count(self, sub, start=0, end=_sys.maxsize):
315 if isinstance(sub, UserString):
316 sub = sub.data
317 return self.data.count(sub, start, end)
318 def encode(self, encoding=None, errors=None): # XXX improve this?
319 if encoding:
320 if errors:
321 return self.__class__(self.data.encode(encoding, errors))
322 return self.__class__(self.data.encode(encoding))
323 return self.__class__(self.data.encode())
324 def endswith(self, suffix, start=0, end=_sys.maxsize):
325 return self.data.endswith(suffix, start, end)
326 def expandtabs(self, tabsize=8):
327 return self.__class__(self.data.expandtabs(tabsize))
328 def find(self, sub, start=0, end=_sys.maxsize):
329 if isinstance(sub, UserString):
330 sub = sub.data
331 return self.data.find(sub, start, end)
332 def format(self, *args, **kwds):
333 return self.data.format(*args, **kwds)
334 def index(self, sub, start=0, end=_sys.maxsize):
335 return self.data.index(sub, start, end)
336 def isalpha(self): return self.data.isalpha()
337 def isalnum(self): return self.data.isalnum()
338 def isdecimal(self): return self.data.isdecimal()
339 def isdigit(self): return self.data.isdigit()
340 def isidentifier(self): return self.data.isidentifier()
341 def islower(self): return self.data.islower()
342 def isnumeric(self): return self.data.isnumeric()
343 def isspace(self): return self.data.isspace()
344 def istitle(self): return self.data.istitle()
345 def isupper(self): return self.data.isupper()
346 def join(self, seq): return self.data.join(seq)
347 def ljust(self, width, *args):
348 return self.__class__(self.data.ljust(width, *args))
349 def lower(self): return self.__class__(self.data.lower())
350 def lstrip(self, chars=None): return self.__class__(self.data.lstrip(chars))
351 def partition(self, sep):
352 return self.data.partition(sep)
353 def replace(self, old, new, maxsplit=-1):
354 if isinstance(old, UserString):
355 old = old.data
356 if isinstance(new, UserString):
357 new = new.data
358 return self.__class__(self.data.replace(old, new, maxsplit))
359 def rfind(self, sub, start=0, end=_sys.maxsize):
360 return self.data.rfind(sub, start, end)
361 def rindex(self, sub, start=0, end=_sys.maxsize):
362 return self.data.rindex(sub, start, end)
363 def rjust(self, width, *args):
364 return self.__class__(self.data.rjust(width, *args))
365 def rpartition(self, sep):
366 return self.data.rpartition(sep)
367 def rstrip(self, chars=None):
368 return self.__class__(self.data.rstrip(chars))
369 def split(self, sep=None, maxsplit=-1):
370 return self.data.split(sep, maxsplit)
371 def rsplit(self, sep=None, maxsplit=-1):
372 return self.data.rsplit(sep, maxsplit)
373 def splitlines(self, keepends=0): return self.data.splitlines(keepends)
374 def startswith(self, prefix, start=0, end=_sys.maxsize):
375 return self.data.startswith(prefix, start, end)
376 def strip(self, chars=None): return self.__class__(self.data.strip(chars))
377 def swapcase(self): return self.__class__(self.data.swapcase())
378 def title(self): return self.__class__(self.data.title())
379 def translate(self, *args):
380 return self.__class__(self.data.translate(*args))
381 def upper(self): return self.__class__(self.data.upper())
382 def zfill(self, width): return self.__class__(self.data.zfill(width))
383
384
385
386################################################################################
Raymond Hettinger48b8b662008-02-05 01:53:00 +0000387### Simple tests
388################################################################################
Guido van Rossumd8faa362007-04-27 19:54:29 +0000389
390if __name__ == '__main__':
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000391 # verify that instances can be pickled
Guido van Rossum99603b02007-07-20 00:22:32 +0000392 from pickle import loads, dumps
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000393 Point = namedtuple('Point', 'x, y', True)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000394 p = Point(x=10, y=20)
395 assert p == loads(dumps(p))
396
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000397 # test and demonstrate ability to override methods
Christian Heimes043d6f62008-01-07 17:19:16 +0000398 class Point(namedtuple('Point', 'x y')):
Christian Heimes25bb7832008-01-11 16:17:00 +0000399 __slots__ = ()
Christian Heimes043d6f62008-01-07 17:19:16 +0000400 @property
401 def hypot(self):
402 return (self.x ** 2 + self.y ** 2) ** 0.5
Christian Heimes790c8232008-01-07 21:14:23 +0000403 def __str__(self):
Christian Heimes25bb7832008-01-11 16:17:00 +0000404 return 'Point: x=%6.3f y=%6.3f hypot=%6.3f' % (self.x, self.y, self.hypot)
Christian Heimes043d6f62008-01-07 17:19:16 +0000405
Christian Heimes25bb7832008-01-11 16:17:00 +0000406 for p in Point(3, 4), Point(14, 5/7.):
Christian Heimes790c8232008-01-07 21:14:23 +0000407 print (p)
Christian Heimes043d6f62008-01-07 17:19:16 +0000408
409 class Point(namedtuple('Point', 'x y')):
410 'Point class with optimized _make() and _replace() without error-checking'
Christian Heimes25bb7832008-01-11 16:17:00 +0000411 __slots__ = ()
Christian Heimes043d6f62008-01-07 17:19:16 +0000412 _make = classmethod(tuple.__new__)
413 def _replace(self, _map=map, **kwds):
Christian Heimes2380ac72008-01-09 00:17:24 +0000414 return self._make(_map(kwds.get, ('x', 'y'), self))
Christian Heimes043d6f62008-01-07 17:19:16 +0000415
416 print(Point(11, 22)._replace(x=100))
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000417
Christian Heimes25bb7832008-01-11 16:17:00 +0000418 Point3D = namedtuple('Point3D', Point._fields + ('z',))
419 print(Point3D.__doc__)
420
Guido van Rossumd8faa362007-04-27 19:54:29 +0000421 import doctest
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000422 TestResults = namedtuple('TestResults', 'failed attempted')
Guido van Rossumd8faa362007-04-27 19:54:29 +0000423 print(TestResults(*doctest.testmod()))