blob: 73fc150e9abfd90b7a08bae251825c6b5188af7d [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
46 field_names = tuple(field_names)
47 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())
90 return result \n\n''' % locals()
Guido van Rossumd59da4b2007-05-22 18:11:13 +000091 for i, name in enumerate(field_names):
Thomas Wouters1b7f8912007-09-19 03:06:30 +000092 template += ' %s = property(itemgetter(%d))\n' % (name, i)
93 if verbose:
94 print(template)
Guido van Rossum8ce8a782007-11-01 19:42:39 +000095
Georg Brandlf08a9dd2008-06-10 16:57:31 +000096 # Execute the template string in a temporary namespace and
97 # support tracing utilities by setting a value for frame.f_globals['__name__']
98 namespace = dict(itemgetter=_itemgetter, __name__='namedtuple_%s' % typename)
Guido van Rossum8ce8a782007-11-01 19:42:39 +000099 try:
100 exec(template, namespace)
101 except SyntaxError as e:
Christian Heimes99170a52007-12-19 02:07:34 +0000102 raise SyntaxError(e.msg + ':\n' + template) from e
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000103 result = namespace[typename]
104
105 # For pickling to work, the __module__ variable needs to be set to the frame
106 # where the named tuple is created. Bypass this step in enviroments where
107 # sys._getframe is not defined (Jython for example).
Guido van Rossumd59da4b2007-05-22 18:11:13 +0000108 if hasattr(_sys, '_getframe'):
109 result.__module__ = _sys._getframe(1).f_globals['__name__']
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000110
Guido van Rossumd59da4b2007-05-22 18:11:13 +0000111 return result
Guido van Rossumd8faa362007-04-27 19:54:29 +0000112
Guido van Rossumd8faa362007-04-27 19:54:29 +0000113
Guido van Rossumd8faa362007-04-27 19:54:29 +0000114
Raymond Hettinger48b8b662008-02-05 01:53:00 +0000115################################################################################
116### UserDict
117################################################################################
Guido van Rossumd8faa362007-04-27 19:54:29 +0000118
Raymond Hettinger48b8b662008-02-05 01:53:00 +0000119class UserDict(MutableMapping):
120
121 # Start by filling-out the abstract methods
122 def __init__(self, dict=None, **kwargs):
123 self.data = {}
124 if dict is not None:
125 self.update(dict)
126 if len(kwargs):
127 self.update(kwargs)
128 def __len__(self): return len(self.data)
129 def __getitem__(self, key):
130 if key in self.data:
131 return self.data[key]
132 if hasattr(self.__class__, "__missing__"):
133 return self.__class__.__missing__(self, key)
134 raise KeyError(key)
135 def __setitem__(self, key, item): self.data[key] = item
136 def __delitem__(self, key): del self.data[key]
137 def __iter__(self):
138 return iter(self.data)
139
Raymond Hettinger554c8b82008-02-05 22:54:43 +0000140 # Modify __contains__ to work correctly when __missing__ is present
141 def __contains__(self, key):
142 return key in self.data
Raymond Hettinger48b8b662008-02-05 01:53:00 +0000143
144 # Now, add the methods in dicts but not in MutableMapping
145 def __repr__(self): return repr(self.data)
146 def copy(self):
147 if self.__class__ is UserDict:
148 return UserDict(self.data.copy())
149 import copy
150 data = self.data
151 try:
152 self.data = {}
153 c = copy.copy(self)
154 finally:
155 self.data = data
156 c.update(self)
157 return c
158 @classmethod
159 def fromkeys(cls, iterable, value=None):
160 d = cls()
161 for key in iterable:
162 d[key] = value
163 return d
164
Raymond Hettinger48b8b662008-02-05 01:53:00 +0000165
166
167################################################################################
Raymond Hettinger53dbe392008-02-12 20:03:09 +0000168### UserList
169################################################################################
170
171class UserList(MutableSequence):
172 """A more or less complete user-defined wrapper around list objects."""
173 def __init__(self, initlist=None):
174 self.data = []
175 if initlist is not None:
176 # XXX should this accept an arbitrary sequence?
177 if type(initlist) == type(self.data):
178 self.data[:] = initlist
179 elif isinstance(initlist, UserList):
180 self.data[:] = initlist.data[:]
181 else:
182 self.data = list(initlist)
183 def __repr__(self): return repr(self.data)
184 def __lt__(self, other): return self.data < self.__cast(other)
185 def __le__(self, other): return self.data <= self.__cast(other)
186 def __eq__(self, other): return self.data == self.__cast(other)
187 def __ne__(self, other): return self.data != self.__cast(other)
188 def __gt__(self, other): return self.data > self.__cast(other)
189 def __ge__(self, other): return self.data >= self.__cast(other)
190 def __cast(self, other):
191 return other.data if isinstance(other, UserList) else other
192 def __cmp__(self, other):
193 return cmp(self.data, self.__cast(other))
194 def __contains__(self, item): return item in self.data
195 def __len__(self): return len(self.data)
196 def __getitem__(self, i): return self.data[i]
197 def __setitem__(self, i, item): self.data[i] = item
198 def __delitem__(self, i): del self.data[i]
199 def __add__(self, other):
200 if isinstance(other, UserList):
201 return self.__class__(self.data + other.data)
202 elif isinstance(other, type(self.data)):
203 return self.__class__(self.data + other)
204 return self.__class__(self.data + list(other))
205 def __radd__(self, other):
206 if isinstance(other, UserList):
207 return self.__class__(other.data + self.data)
208 elif isinstance(other, type(self.data)):
209 return self.__class__(other + self.data)
210 return self.__class__(list(other) + self.data)
211 def __iadd__(self, other):
212 if isinstance(other, UserList):
213 self.data += other.data
214 elif isinstance(other, type(self.data)):
215 self.data += other
216 else:
217 self.data += list(other)
218 return self
219 def __mul__(self, n):
220 return self.__class__(self.data*n)
221 __rmul__ = __mul__
222 def __imul__(self, n):
223 self.data *= n
224 return self
225 def append(self, item): self.data.append(item)
226 def insert(self, i, item): self.data.insert(i, item)
227 def pop(self, i=-1): return self.data.pop(i)
228 def remove(self, item): self.data.remove(item)
229 def count(self, item): return self.data.count(item)
230 def index(self, item, *args): return self.data.index(item, *args)
231 def reverse(self): self.data.reverse()
232 def sort(self, *args, **kwds): self.data.sort(*args, **kwds)
233 def extend(self, other):
234 if isinstance(other, UserList):
235 self.data.extend(other.data)
236 else:
237 self.data.extend(other)
238
239
240
241################################################################################
Raymond Hettingerb3a65f82008-02-21 22:11:37 +0000242### UserString
243################################################################################
244
245class UserString(Sequence):
246 def __init__(self, seq):
247 if isinstance(seq, str):
248 self.data = seq
249 elif isinstance(seq, UserString):
250 self.data = seq.data[:]
251 else:
252 self.data = str(seq)
253 def __str__(self): return str(self.data)
254 def __repr__(self): return repr(self.data)
255 def __int__(self): return int(self.data)
256 def __long__(self): return int(self.data)
257 def __float__(self): return float(self.data)
258 def __complex__(self): return complex(self.data)
259 def __hash__(self): return hash(self.data)
260
261 def __eq__(self, string):
262 if isinstance(string, UserString):
263 return self.data == string.data
264 return self.data == string
265 def __ne__(self, string):
266 if isinstance(string, UserString):
267 return self.data != string.data
268 return self.data != string
269 def __lt__(self, string):
270 if isinstance(string, UserString):
271 return self.data < string.data
272 return self.data < string
273 def __le__(self, string):
274 if isinstance(string, UserString):
275 return self.data <= string.data
276 return self.data <= string
277 def __gt__(self, string):
278 if isinstance(string, UserString):
279 return self.data > string.data
280 return self.data > string
281 def __ge__(self, string):
282 if isinstance(string, UserString):
283 return self.data >= string.data
284 return self.data >= string
285
286 def __contains__(self, char):
287 if isinstance(char, UserString):
288 char = char.data
289 return char in self.data
290
291 def __len__(self): return len(self.data)
292 def __getitem__(self, index): return self.__class__(self.data[index])
293 def __add__(self, other):
294 if isinstance(other, UserString):
295 return self.__class__(self.data + other.data)
296 elif isinstance(other, str):
297 return self.__class__(self.data + other)
298 return self.__class__(self.data + str(other))
299 def __radd__(self, other):
300 if isinstance(other, str):
301 return self.__class__(other + self.data)
302 return self.__class__(str(other) + self.data)
303 def __mul__(self, n):
304 return self.__class__(self.data*n)
305 __rmul__ = __mul__
306 def __mod__(self, args):
307 return self.__class__(self.data % args)
308
309 # the following methods are defined in alphabetical order:
310 def capitalize(self): return self.__class__(self.data.capitalize())
311 def center(self, width, *args):
312 return self.__class__(self.data.center(width, *args))
313 def count(self, sub, start=0, end=_sys.maxsize):
314 if isinstance(sub, UserString):
315 sub = sub.data
316 return self.data.count(sub, start, end)
317 def encode(self, encoding=None, errors=None): # XXX improve this?
318 if encoding:
319 if errors:
320 return self.__class__(self.data.encode(encoding, errors))
321 return self.__class__(self.data.encode(encoding))
322 return self.__class__(self.data.encode())
323 def endswith(self, suffix, start=0, end=_sys.maxsize):
324 return self.data.endswith(suffix, start, end)
325 def expandtabs(self, tabsize=8):
326 return self.__class__(self.data.expandtabs(tabsize))
327 def find(self, sub, start=0, end=_sys.maxsize):
328 if isinstance(sub, UserString):
329 sub = sub.data
330 return self.data.find(sub, start, end)
331 def format(self, *args, **kwds):
332 return self.data.format(*args, **kwds)
333 def index(self, sub, start=0, end=_sys.maxsize):
334 return self.data.index(sub, start, end)
335 def isalpha(self): return self.data.isalpha()
336 def isalnum(self): return self.data.isalnum()
337 def isdecimal(self): return self.data.isdecimal()
338 def isdigit(self): return self.data.isdigit()
339 def isidentifier(self): return self.data.isidentifier()
340 def islower(self): return self.data.islower()
341 def isnumeric(self): return self.data.isnumeric()
342 def isspace(self): return self.data.isspace()
343 def istitle(self): return self.data.istitle()
344 def isupper(self): return self.data.isupper()
345 def join(self, seq): return self.data.join(seq)
346 def ljust(self, width, *args):
347 return self.__class__(self.data.ljust(width, *args))
348 def lower(self): return self.__class__(self.data.lower())
349 def lstrip(self, chars=None): return self.__class__(self.data.lstrip(chars))
350 def partition(self, sep):
351 return self.data.partition(sep)
352 def replace(self, old, new, maxsplit=-1):
353 if isinstance(old, UserString):
354 old = old.data
355 if isinstance(new, UserString):
356 new = new.data
357 return self.__class__(self.data.replace(old, new, maxsplit))
358 def rfind(self, sub, start=0, end=_sys.maxsize):
359 return self.data.rfind(sub, start, end)
360 def rindex(self, sub, start=0, end=_sys.maxsize):
361 return self.data.rindex(sub, start, end)
362 def rjust(self, width, *args):
363 return self.__class__(self.data.rjust(width, *args))
364 def rpartition(self, sep):
365 return self.data.rpartition(sep)
366 def rstrip(self, chars=None):
367 return self.__class__(self.data.rstrip(chars))
368 def split(self, sep=None, maxsplit=-1):
369 return self.data.split(sep, maxsplit)
370 def rsplit(self, sep=None, maxsplit=-1):
371 return self.data.rsplit(sep, maxsplit)
372 def splitlines(self, keepends=0): return self.data.splitlines(keepends)
373 def startswith(self, prefix, start=0, end=_sys.maxsize):
374 return self.data.startswith(prefix, start, end)
375 def strip(self, chars=None): return self.__class__(self.data.strip(chars))
376 def swapcase(self): return self.__class__(self.data.swapcase())
377 def title(self): return self.__class__(self.data.title())
378 def translate(self, *args):
379 return self.__class__(self.data.translate(*args))
380 def upper(self): return self.__class__(self.data.upper())
381 def zfill(self, width): return self.__class__(self.data.zfill(width))
382
383
384
385################################################################################
Raymond Hettinger48b8b662008-02-05 01:53:00 +0000386### Simple tests
387################################################################################
Guido van Rossumd8faa362007-04-27 19:54:29 +0000388
389if __name__ == '__main__':
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000390 # verify that instances can be pickled
Guido van Rossum99603b02007-07-20 00:22:32 +0000391 from pickle import loads, dumps
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000392 Point = namedtuple('Point', 'x, y', True)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000393 p = Point(x=10, y=20)
394 assert p == loads(dumps(p))
395
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000396 # test and demonstrate ability to override methods
Christian Heimes043d6f62008-01-07 17:19:16 +0000397 class Point(namedtuple('Point', 'x y')):
Christian Heimes25bb7832008-01-11 16:17:00 +0000398 __slots__ = ()
Christian Heimes043d6f62008-01-07 17:19:16 +0000399 @property
400 def hypot(self):
401 return (self.x ** 2 + self.y ** 2) ** 0.5
Christian Heimes790c8232008-01-07 21:14:23 +0000402 def __str__(self):
Christian Heimes25bb7832008-01-11 16:17:00 +0000403 return 'Point: x=%6.3f y=%6.3f hypot=%6.3f' % (self.x, self.y, self.hypot)
Christian Heimes043d6f62008-01-07 17:19:16 +0000404
Christian Heimes25bb7832008-01-11 16:17:00 +0000405 for p in Point(3, 4), Point(14, 5/7.):
Christian Heimes790c8232008-01-07 21:14:23 +0000406 print (p)
Christian Heimes043d6f62008-01-07 17:19:16 +0000407
408 class Point(namedtuple('Point', 'x y')):
409 'Point class with optimized _make() and _replace() without error-checking'
Christian Heimes25bb7832008-01-11 16:17:00 +0000410 __slots__ = ()
Christian Heimes043d6f62008-01-07 17:19:16 +0000411 _make = classmethod(tuple.__new__)
412 def _replace(self, _map=map, **kwds):
Christian Heimes2380ac72008-01-09 00:17:24 +0000413 return self._make(_map(kwds.get, ('x', 'y'), self))
Christian Heimes043d6f62008-01-07 17:19:16 +0000414
415 print(Point(11, 22)._replace(x=100))
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000416
Christian Heimes25bb7832008-01-11 16:17:00 +0000417 Point3D = namedtuple('Point3D', Point._fields + ('z',))
418 print(Point3D.__doc__)
419
Guido van Rossumd8faa362007-04-27 19:54:29 +0000420 import doctest
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000421 TestResults = namedtuple('TestResults', 'failed attempted')
Guido van Rossumd8faa362007-04-27 19:54:29 +0000422 print(TestResults(*doctest.testmod()))