blob: 5fb0e05d12050e5a766194284a53df4debc2d319 [file] [log] [blame]
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001__all__ = ['deque', 'defaultdict', 'namedtuple']
Guido van Rossumd8faa362007-04-27 19:54:29 +00002
3from _collections import deque, defaultdict
4from operator import itemgetter as _itemgetter
Christian Heimes38053212007-12-14 01:24:44 +00005from itertools import izip as _izip
Guido van Rossum8ce8a782007-11-01 19:42:39 +00006from keyword import iskeyword as _iskeyword
Guido van Rossumd8faa362007-04-27 19:54:29 +00007import sys as _sys
8
Guido van Rossumcd16bf62007-06-13 18:07:49 +00009# For bootstrapping reasons, the collection ABCs are defined in _abcoll.py.
10# They should however be considered an integral part of collections.py.
11from _abcoll import *
12import _abcoll
13__all__ += _abcoll.__all__
14
Guido van Rossum8ce8a782007-11-01 19:42:39 +000015def namedtuple(typename, field_names, verbose=False):
Guido van Rossumd8faa362007-04-27 19:54:29 +000016 """Returns a new subclass of tuple with named fields.
17
Guido van Rossum8ce8a782007-11-01 19:42:39 +000018 >>> Point = namedtuple('Point', 'x y')
Thomas Wouters1b7f8912007-09-19 03:06:30 +000019 >>> Point.__doc__ # docstring for the new class
Guido van Rossumd8faa362007-04-27 19:54:29 +000020 'Point(x, y)'
Thomas Wouters1b7f8912007-09-19 03:06:30 +000021 >>> p = Point(11, y=22) # instantiate with positional args or keywords
22 >>> p[0] + p[1] # works just like the tuple (11, 22)
Guido van Rossumd8faa362007-04-27 19:54:29 +000023 33
Thomas Wouters1b7f8912007-09-19 03:06:30 +000024 >>> x, y = p # unpacks just like a tuple
Guido van Rossumd8faa362007-04-27 19:54:29 +000025 >>> x, y
26 (11, 22)
Thomas Wouters1b7f8912007-09-19 03:06:30 +000027 >>> p.x + p.y # fields also accessable by name
Guido van Rossumd8faa362007-04-27 19:54:29 +000028 33
Guido van Rossum8ce8a782007-11-01 19:42:39 +000029 >>> d = p.__asdict__() # convert to a dictionary
30 >>> d['x']
31 11
32 >>> Point(**d) # convert from a dictionary
Guido van Rossumd8faa362007-04-27 19:54:29 +000033 Point(x=11, y=22)
Guido van Rossum3d392eb2007-11-16 00:35:22 +000034 >>> p.__replace__(x=100) # __replace__() is like str.replace() but targets named fields
Thomas Wouters1b7f8912007-09-19 03:06:30 +000035 Point(x=100, y=22)
Guido van Rossumd8faa362007-04-27 19:54:29 +000036
37 """
38
Guido van Rossum8ce8a782007-11-01 19:42:39 +000039 # Parse and validate the field names
40 if isinstance(field_names, str):
41 field_names = field_names.replace(',', ' ').split() # names separated by whitespace and/or commas
42 field_names = tuple(field_names)
43 for name in (typename,) + field_names:
Christian Heimesb9eccbf2007-12-05 20:18:38 +000044 if not all(c.isalnum() or c=='_' for c in name):
Guido van Rossum8ce8a782007-11-01 19:42:39 +000045 raise ValueError('Type names and field names can only contain alphanumeric characters and underscores: %r' % name)
46 if _iskeyword(name):
47 raise ValueError('Type names and field names cannot be a keyword: %r' % name)
48 if name[0].isdigit():
49 raise ValueError('Type names and field names cannot start with a number: %r' % name)
50 seen_names = set()
51 for name in field_names:
Christian Heimesb9eccbf2007-12-05 20:18:38 +000052 if name.startswith('__') and name.endswith('__') and len(name) > 3:
Guido van Rossum8ce8a782007-11-01 19:42:39 +000053 raise ValueError('Field names cannot start and end with double underscores: %r' % name)
54 if name in seen_names:
55 raise ValueError('Encountered duplicate field name: %r' % name)
56 seen_names.add(name)
57
58 # Create and fill-in the class template
Thomas Wouters1b7f8912007-09-19 03:06:30 +000059 argtxt = repr(field_names).replace("'", "")[1:-1] # tuple repr without parens or quotes
Guido van Rossumd59da4b2007-05-22 18:11:13 +000060 reprtxt = ', '.join('%s=%%r' % name for name in field_names)
61 template = '''class %(typename)s(tuple):
62 '%(typename)s(%(argtxt)s)'
63 __slots__ = ()
Guido van Rossum3d392eb2007-11-16 00:35:22 +000064 __fields__ = property(lambda self: %(field_names)r)
Guido van Rossumd59da4b2007-05-22 18:11:13 +000065 def __new__(cls, %(argtxt)s):
Thomas Wouters1b7f8912007-09-19 03:06:30 +000066 return tuple.__new__(cls, (%(argtxt)s))
Guido van Rossumd59da4b2007-05-22 18:11:13 +000067 def __repr__(self):
68 return '%(typename)s(%(reprtxt)s)' %% self
Guido van Rossum8ce8a782007-11-01 19:42:39 +000069 def __asdict__(self, dict=dict, zip=zip):
70 'Return a new dict mapping field names to their values'
71 return dict(zip(%(field_names)r, self))
Guido van Rossum3d392eb2007-11-16 00:35:22 +000072 def __replace__(self, **kwds):
73 'Return a new %(typename)s object replacing specified fields with new values'
Christian Heimes38053212007-12-14 01:24:44 +000074 return %(typename)s(**dict(zip(%(field_names)r, self), **kwds)) \n''' % locals()
Guido van Rossumd59da4b2007-05-22 18:11:13 +000075 for i, name in enumerate(field_names):
Thomas Wouters1b7f8912007-09-19 03:06:30 +000076 template += ' %s = property(itemgetter(%d))\n' % (name, i)
77 if verbose:
78 print(template)
Guido van Rossum8ce8a782007-11-01 19:42:39 +000079
80 # Execute the template string in a temporary namespace
Christian Heimes38053212007-12-14 01:24:44 +000081 namespace = dict(itemgetter=_itemgetter, zip=_izip)
Guido van Rossum8ce8a782007-11-01 19:42:39 +000082 try:
83 exec(template, namespace)
84 except SyntaxError as e:
85 raise SyntaxError(e.message + ':\n' + template)
86 result = namespace[typename]
87
88 # For pickling to work, the __module__ variable needs to be set to the frame
89 # where the named tuple is created. Bypass this step in enviroments where
90 # sys._getframe is not defined (Jython for example).
Guido van Rossumd59da4b2007-05-22 18:11:13 +000091 if hasattr(_sys, '_getframe'):
92 result.__module__ = _sys._getframe(1).f_globals['__name__']
Guido van Rossum8ce8a782007-11-01 19:42:39 +000093
Guido van Rossumd59da4b2007-05-22 18:11:13 +000094 return result
Guido van Rossumd8faa362007-04-27 19:54:29 +000095
Guido van Rossumd8faa362007-04-27 19:54:29 +000096
Guido van Rossumd8faa362007-04-27 19:54:29 +000097
Guido van Rossumd8faa362007-04-27 19:54:29 +000098
99
100if __name__ == '__main__':
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000101 # verify that instances can be pickled
Guido van Rossum99603b02007-07-20 00:22:32 +0000102 from pickle import loads, dumps
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000103 Point = namedtuple('Point', 'x, y', True)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000104 p = Point(x=10, y=20)
105 assert p == loads(dumps(p))
106
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000107 # test and demonstrate ability to override methods
108 Point.__repr__ = lambda self: 'Point(%.3f, %.3f)' % self
Guido van Rossum053b4f32007-11-16 00:48:13 +0000109 print(p)
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000110
Guido van Rossumd8faa362007-04-27 19:54:29 +0000111 import doctest
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000112 TestResults = namedtuple('TestResults', 'failed attempted')
Guido van Rossumd8faa362007-04-27 19:54:29 +0000113 print(TestResults(*doctest.testmod()))