blob: 458cbefdf6be943c1b2cffc90d8538dcb9196866 [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)
258 def __long__(self): return int(self.data)
259 def __float__(self): return float(self.data)
260 def __complex__(self): return complex(self.data)
261 def __hash__(self): return hash(self.data)
262
263 def __eq__(self, string):
264 if isinstance(string, UserString):
265 return self.data == string.data
266 return self.data == string
267 def __ne__(self, string):
268 if isinstance(string, UserString):
269 return self.data != string.data
270 return self.data != string
271 def __lt__(self, string):
272 if isinstance(string, UserString):
273 return self.data < string.data
274 return self.data < string
275 def __le__(self, string):
276 if isinstance(string, UserString):
277 return self.data <= string.data
278 return self.data <= string
279 def __gt__(self, string):
280 if isinstance(string, UserString):
281 return self.data > string.data
282 return self.data > string
283 def __ge__(self, string):
284 if isinstance(string, UserString):
285 return self.data >= string.data
286 return self.data >= string
287
288 def __contains__(self, char):
289 if isinstance(char, UserString):
290 char = char.data
291 return char in self.data
292
293 def __len__(self): return len(self.data)
294 def __getitem__(self, index): return self.__class__(self.data[index])
295 def __add__(self, other):
296 if isinstance(other, UserString):
297 return self.__class__(self.data + other.data)
298 elif isinstance(other, str):
299 return self.__class__(self.data + other)
300 return self.__class__(self.data + str(other))
301 def __radd__(self, other):
302 if isinstance(other, str):
303 return self.__class__(other + self.data)
304 return self.__class__(str(other) + self.data)
305 def __mul__(self, n):
306 return self.__class__(self.data*n)
307 __rmul__ = __mul__
308 def __mod__(self, args):
309 return self.__class__(self.data % args)
310
311 # the following methods are defined in alphabetical order:
312 def capitalize(self): return self.__class__(self.data.capitalize())
313 def center(self, width, *args):
314 return self.__class__(self.data.center(width, *args))
315 def count(self, sub, start=0, end=_sys.maxsize):
316 if isinstance(sub, UserString):
317 sub = sub.data
318 return self.data.count(sub, start, end)
319 def encode(self, encoding=None, errors=None): # XXX improve this?
320 if encoding:
321 if errors:
322 return self.__class__(self.data.encode(encoding, errors))
323 return self.__class__(self.data.encode(encoding))
324 return self.__class__(self.data.encode())
325 def endswith(self, suffix, start=0, end=_sys.maxsize):
326 return self.data.endswith(suffix, start, end)
327 def expandtabs(self, tabsize=8):
328 return self.__class__(self.data.expandtabs(tabsize))
329 def find(self, sub, start=0, end=_sys.maxsize):
330 if isinstance(sub, UserString):
331 sub = sub.data
332 return self.data.find(sub, start, end)
333 def format(self, *args, **kwds):
334 return self.data.format(*args, **kwds)
335 def index(self, sub, start=0, end=_sys.maxsize):
336 return self.data.index(sub, start, end)
337 def isalpha(self): return self.data.isalpha()
338 def isalnum(self): return self.data.isalnum()
339 def isdecimal(self): return self.data.isdecimal()
340 def isdigit(self): return self.data.isdigit()
341 def isidentifier(self): return self.data.isidentifier()
342 def islower(self): return self.data.islower()
343 def isnumeric(self): return self.data.isnumeric()
344 def isspace(self): return self.data.isspace()
345 def istitle(self): return self.data.istitle()
346 def isupper(self): return self.data.isupper()
347 def join(self, seq): return self.data.join(seq)
348 def ljust(self, width, *args):
349 return self.__class__(self.data.ljust(width, *args))
350 def lower(self): return self.__class__(self.data.lower())
351 def lstrip(self, chars=None): return self.__class__(self.data.lstrip(chars))
352 def partition(self, sep):
353 return self.data.partition(sep)
354 def replace(self, old, new, maxsplit=-1):
355 if isinstance(old, UserString):
356 old = old.data
357 if isinstance(new, UserString):
358 new = new.data
359 return self.__class__(self.data.replace(old, new, maxsplit))
360 def rfind(self, sub, start=0, end=_sys.maxsize):
361 return self.data.rfind(sub, start, end)
362 def rindex(self, sub, start=0, end=_sys.maxsize):
363 return self.data.rindex(sub, start, end)
364 def rjust(self, width, *args):
365 return self.__class__(self.data.rjust(width, *args))
366 def rpartition(self, sep):
367 return self.data.rpartition(sep)
368 def rstrip(self, chars=None):
369 return self.__class__(self.data.rstrip(chars))
370 def split(self, sep=None, maxsplit=-1):
371 return self.data.split(sep, maxsplit)
372 def rsplit(self, sep=None, maxsplit=-1):
373 return self.data.rsplit(sep, maxsplit)
374 def splitlines(self, keepends=0): return self.data.splitlines(keepends)
375 def startswith(self, prefix, start=0, end=_sys.maxsize):
376 return self.data.startswith(prefix, start, end)
377 def strip(self, chars=None): return self.__class__(self.data.strip(chars))
378 def swapcase(self): return self.__class__(self.data.swapcase())
379 def title(self): return self.__class__(self.data.title())
380 def translate(self, *args):
381 return self.__class__(self.data.translate(*args))
382 def upper(self): return self.__class__(self.data.upper())
383 def zfill(self, width): return self.__class__(self.data.zfill(width))
384
385
386
387################################################################################
Raymond Hettinger48b8b662008-02-05 01:53:00 +0000388### Simple tests
389################################################################################
Guido van Rossumd8faa362007-04-27 19:54:29 +0000390
391if __name__ == '__main__':
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000392 # verify that instances can be pickled
Guido van Rossum99603b02007-07-20 00:22:32 +0000393 from pickle import loads, dumps
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000394 Point = namedtuple('Point', 'x, y', True)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000395 p = Point(x=10, y=20)
396 assert p == loads(dumps(p))
397
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000398 # test and demonstrate ability to override methods
Christian Heimes043d6f62008-01-07 17:19:16 +0000399 class Point(namedtuple('Point', 'x y')):
Christian Heimes25bb7832008-01-11 16:17:00 +0000400 __slots__ = ()
Christian Heimes043d6f62008-01-07 17:19:16 +0000401 @property
402 def hypot(self):
403 return (self.x ** 2 + self.y ** 2) ** 0.5
Christian Heimes790c8232008-01-07 21:14:23 +0000404 def __str__(self):
Christian Heimes25bb7832008-01-11 16:17:00 +0000405 return 'Point: x=%6.3f y=%6.3f hypot=%6.3f' % (self.x, self.y, self.hypot)
Christian Heimes043d6f62008-01-07 17:19:16 +0000406
Christian Heimes25bb7832008-01-11 16:17:00 +0000407 for p in Point(3, 4), Point(14, 5/7.):
Christian Heimes790c8232008-01-07 21:14:23 +0000408 print (p)
Christian Heimes043d6f62008-01-07 17:19:16 +0000409
410 class Point(namedtuple('Point', 'x y')):
411 'Point class with optimized _make() and _replace() without error-checking'
Christian Heimes25bb7832008-01-11 16:17:00 +0000412 __slots__ = ()
Christian Heimes043d6f62008-01-07 17:19:16 +0000413 _make = classmethod(tuple.__new__)
414 def _replace(self, _map=map, **kwds):
Christian Heimes2380ac72008-01-09 00:17:24 +0000415 return self._make(_map(kwds.get, ('x', 'y'), self))
Christian Heimes043d6f62008-01-07 17:19:16 +0000416
417 print(Point(11, 22)._replace(x=100))
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000418
Christian Heimes25bb7832008-01-11 16:17:00 +0000419 Point3D = namedtuple('Point3D', Point._fields + ('z',))
420 print(Point3D.__doc__)
421
Guido van Rossumd8faa362007-04-27 19:54:29 +0000422 import doctest
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000423 TestResults = namedtuple('TestResults', 'failed attempted')
Guido van Rossumd8faa362007-04-27 19:54:29 +0000424 print(TestResults(*doctest.testmod()))