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