blob: 24088183900a30f43c3242ddc33bd81ba88b6bf6 [file] [log] [blame]
Raymond Hettinger01a09572007-10-23 20:37:41 +00001__all__ = ['deque', 'defaultdict', 'namedtuple']
Raymond Hettinger88880b22007-12-18 00:13:45 +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__
Raymond Hettingereb979882007-02-28 18:37:52 +00007
8from _collections import deque, defaultdict
Raymond Hettingerc37e5e02007-03-01 06:16:43 +00009from operator import itemgetter as _itemgetter
Raymond Hettingerabfd8df2007-10-16 21:28:32 +000010from keyword import iskeyword as _iskeyword
Raymond Hettingerc37e5e02007-03-01 06:16:43 +000011import sys as _sys
12
Raymond Hettinger01a09572007-10-23 20:37:41 +000013def namedtuple(typename, field_names, verbose=False):
Raymond Hettingerc37e5e02007-03-01 06:16:43 +000014 """Returns a new subclass of tuple with named fields.
15
Raymond Hettinger01a09572007-10-23 20:37:41 +000016 >>> Point = namedtuple('Point', 'x y')
Raymond Hettingerd36a60e2007-09-17 00:55:00 +000017 >>> Point.__doc__ # docstring for the new class
Raymond Hettingerc37e5e02007-03-01 06:16:43 +000018 'Point(x, y)'
Raymond Hettingerd36a60e2007-09-17 00:55:00 +000019 >>> p = Point(11, y=22) # instantiate with positional args or keywords
Raymond Hettinger8777bca2007-12-18 22:21:27 +000020 >>> p[0] + p[1] # indexable like a plain tuple
Raymond Hettingerc37e5e02007-03-01 06:16:43 +000021 33
Raymond Hettinger88880b22007-12-18 00:13:45 +000022 >>> x, y = p # unpack like a regular tuple
Raymond Hettingerc37e5e02007-03-01 06:16:43 +000023 >>> x, y
24 (11, 22)
Raymond Hettingerd36a60e2007-09-17 00:55:00 +000025 >>> p.x + p.y # fields also accessable by name
Raymond Hettingerc37e5e02007-03-01 06:16:43 +000026 33
Raymond Hettinger42da8742007-12-14 02:49:47 +000027 >>> d = p._asdict() # convert to a dictionary
Raymond Hettingera7fc4b12007-10-05 02:47:07 +000028 >>> d['x']
29 11
30 >>> Point(**d) # convert from a dictionary
Raymond Hettingerc37e5e02007-03-01 06:16:43 +000031 Point(x=11, y=22)
Raymond Hettinger42da8742007-12-14 02:49:47 +000032 >>> p._replace(x=100) # _replace() is like str.replace() but targets named fields
Raymond Hettingerd36a60e2007-09-17 00:55:00 +000033 Point(x=100, y=22)
Raymond Hettingerc37e5e02007-03-01 06:16:43 +000034
35 """
36
Raymond Hettinger58167142008-01-08 02:02:05 +000037 # Parse and validate the field names. Validation serves two purposes,
38 # generating informative error messages and preventing template injection attacks.
Raymond Hettinger2115bbc2007-10-08 09:14:28 +000039 if isinstance(field_names, basestring):
Raymond Hettinger0e1d6062007-10-08 10:11:51 +000040 field_names = field_names.replace(',', ' ').split() # names separated by whitespace and/or commas
Raymond Hettinger2115bbc2007-10-08 09:14:28 +000041 field_names = tuple(field_names)
Raymond Hettinger050afbf2007-10-16 19:18:30 +000042 for name in (typename,) + field_names:
Raymond Hettinger2e1af252007-12-05 18:11:08 +000043 if not all(c.isalnum() or c=='_' for c in name):
Raymond Hettinger050afbf2007-10-16 19:18:30 +000044 raise ValueError('Type names and field names can only contain alphanumeric characters and underscores: %r' % name)
Raymond Hettingerabfd8df2007-10-16 21:28:32 +000045 if _iskeyword(name):
46 raise ValueError('Type names and field names cannot be a keyword: %r' % name)
Raymond Hettinger050afbf2007-10-16 19:18:30 +000047 if name[0].isdigit():
48 raise ValueError('Type names and field names cannot start with a number: %r' % name)
Raymond Hettinger163f6222007-10-09 01:36:23 +000049 seen_names = set()
50 for name in field_names:
Raymond Hettinger42da8742007-12-14 02:49:47 +000051 if name.startswith('_'):
52 raise ValueError('Field names cannot start with an underscore: %r' % name)
Raymond Hettinger163f6222007-10-09 01:36:23 +000053 if name in seen_names:
Raymond Hettinger050afbf2007-10-16 19:18:30 +000054 raise ValueError('Encountered duplicate field name: %r' % name)
Raymond Hettinger163f6222007-10-09 01:36:23 +000055 seen_names.add(name)
Raymond Hettinger2115bbc2007-10-08 09:14:28 +000056
57 # Create and fill-in the class template
Raymond Hettinger02740f72008-01-05 01:35:43 +000058 numfields = len(field_names)
Raymond Hettinger2b03d452007-09-18 03:33:19 +000059 argtxt = repr(field_names).replace("'", "")[1:-1] # tuple repr without parens or quotes
Raymond Hettinger5a41daf2007-05-19 01:11:16 +000060 reprtxt = ', '.join('%s=%%r' % name for name in field_names)
Raymond Hettinger8777bca2007-12-18 22:21:27 +000061 dicttxt = ', '.join('%r: t[%d]' % (name, pos) for pos, name in enumerate(field_names))
Raymond Hettinger5a41daf2007-05-19 01:11:16 +000062 template = '''class %(typename)s(tuple):
Raymond Hettinger48eca672007-12-14 18:08:20 +000063 '%(typename)s(%(argtxt)s)' \n
64 __slots__ = () \n
Raymond Hettingere0734e72008-01-04 03:22:53 +000065 _fields = %(field_names)r \n
Raymond Hettinger5a41daf2007-05-19 01:11:16 +000066 def __new__(cls, %(argtxt)s):
Raymond Hettinger48eca672007-12-14 18:08:20 +000067 return tuple.__new__(cls, (%(argtxt)s)) \n
Raymond Hettinger02740f72008-01-05 01:35:43 +000068 @classmethod
Raymond Hettinger844f71b2008-01-06 22:11:54 +000069 def _make(cls, iterable, new=tuple.__new__, len=len):
Raymond Hettinger02740f72008-01-05 01:35:43 +000070 'Make a new %(typename)s object from a sequence or iterable'
Raymond Hettinger844f71b2008-01-06 22:11:54 +000071 result = new(cls, iterable)
Raymond Hettinger02740f72008-01-05 01:35:43 +000072 if len(result) != %(numfields)d:
73 raise TypeError('Expected %(numfields)d arguments, got %%d' %% len(result))
74 return result \n
Raymond Hettinger5a41daf2007-05-19 01:11:16 +000075 def __repr__(self):
Raymond Hettinger48eca672007-12-14 18:08:20 +000076 return '%(typename)s(%(reprtxt)s)' %% self \n
Raymond Hettinger8777bca2007-12-18 22:21:27 +000077 def _asdict(t):
Raymond Hettinger48eca672007-12-14 18:08:20 +000078 'Return a new dict which maps field names to their values'
Raymond Hettinger8777bca2007-12-18 22:21:27 +000079 return {%(dicttxt)s} \n
Raymond Hettinger42da8742007-12-14 02:49:47 +000080 def _replace(self, **kwds):
Raymond Hettingereeeb9c42007-11-15 02:44:53 +000081 'Return a new %(typename)s object replacing specified fields with new values'
Raymond Hettinger11668722008-01-06 09:02:24 +000082 result = self._make(map(kwds.pop, %(field_names)r, self))
Raymond Hettinger1b50fd72008-01-05 02:17:24 +000083 if kwds:
84 raise ValueError('Got unexpected field names: %%r' %% kwds.keys())
Raymond Hettingere98839a2008-06-09 01:28:30 +000085 return result \n
86 def __getnewargs__(self):
87 return tuple(self) \n\n''' % locals()
Raymond Hettinger5a41daf2007-05-19 01:11:16 +000088 for i, name in enumerate(field_names):
Raymond Hettinger2b03d452007-09-18 03:33:19 +000089 template += ' %s = property(itemgetter(%d))\n' % (name, i)
90 if verbose:
91 print template
Raymond Hettinger2115bbc2007-10-08 09:14:28 +000092
Raymond Hettinger3c2523c2008-05-30 07:16:53 +000093 # Execute the template string in a temporary namespace and
94 # support tracing utilities by setting a value for frame.f_globals['__name__']
95 namespace = dict(itemgetter=_itemgetter, __name__='namedtuple_%s' % typename)
Raymond Hettinger2115bbc2007-10-08 09:14:28 +000096 try:
Raymond Hettinger0e1d6062007-10-08 10:11:51 +000097 exec template in namespace
Raymond Hettinger2115bbc2007-10-08 09:14:28 +000098 except SyntaxError, e:
99 raise SyntaxError(e.message + ':\n' + template)
Raymond Hettinger0e1d6062007-10-08 10:11:51 +0000100 result = namespace[typename]
Raymond Hettinger2115bbc2007-10-08 09:14:28 +0000101
102 # For pickling to work, the __module__ variable needs to be set to the frame
103 # where the named tuple is created. Bypass this step in enviroments where
104 # sys._getframe is not defined (Jython for example).
Raymond Hettinger5a41daf2007-05-19 01:11:16 +0000105 if hasattr(_sys, '_getframe'):
106 result.__module__ = _sys._getframe(1).f_globals['__name__']
Raymond Hettinger2115bbc2007-10-08 09:14:28 +0000107
Raymond Hettinger5a41daf2007-05-19 01:11:16 +0000108 return result
Raymond Hettingerc37e5e02007-03-01 06:16:43 +0000109
Raymond Hettingerc37e5e02007-03-01 06:16:43 +0000110
Raymond Hettingerc37e5e02007-03-01 06:16:43 +0000111
Raymond Hettingerc37e5e02007-03-01 06:16:43 +0000112
Raymond Hettingerc37e5e02007-03-01 06:16:43 +0000113
114
115if __name__ == '__main__':
Raymond Hettingerd36a60e2007-09-17 00:55:00 +0000116 # verify that instances can be pickled
Raymond Hettingerc37e5e02007-03-01 06:16:43 +0000117 from cPickle import loads, dumps
Raymond Hettinger01a09572007-10-23 20:37:41 +0000118 Point = namedtuple('Point', 'x, y', True)
Raymond Hettingerc37e5e02007-03-01 06:16:43 +0000119 p = Point(x=10, y=20)
120 assert p == loads(dumps(p))
121
Raymond Hettingereeeb9c42007-11-15 02:44:53 +0000122 # test and demonstrate ability to override methods
Raymond Hettingerb8e00722008-01-07 04:24:49 +0000123 class Point(namedtuple('Point', 'x y')):
Raymond Hettingere1655082008-01-10 19:15:10 +0000124 __slots__ = ()
Raymond Hettingerb8e00722008-01-07 04:24:49 +0000125 @property
126 def hypot(self):
127 return (self.x ** 2 + self.y ** 2) ** 0.5
Raymond Hettinger9a359212008-01-07 20:07:38 +0000128 def __str__(self):
Raymond Hettinger15b5e552008-01-10 23:00:01 +0000129 return 'Point: x=%6.3f y=%6.3f hypot=%6.3f' % (self.x, self.y, self.hypot)
Raymond Hettingerb8e00722008-01-07 04:24:49 +0000130
Raymond Hettingere1655082008-01-10 19:15:10 +0000131 for p in Point(3, 4), Point(14, 5/7.):
Raymond Hettinger9a359212008-01-07 20:07:38 +0000132 print p
Raymond Hettingereeeb9c42007-11-15 02:44:53 +0000133
Raymond Hettingerdc55f352008-01-07 09:03:49 +0000134 class Point(namedtuple('Point', 'x y')):
135 'Point class with optimized _make() and _replace() without error-checking'
Raymond Hettingere1655082008-01-10 19:15:10 +0000136 __slots__ = ()
Raymond Hettingerdc55f352008-01-07 09:03:49 +0000137 _make = classmethod(tuple.__new__)
138 def _replace(self, _map=map, **kwds):
Raymond Hettingerf5e8af12008-01-07 20:56:05 +0000139 return self._make(_map(kwds.get, ('x', 'y'), self))
Raymond Hettingerdc55f352008-01-07 09:03:49 +0000140
141 print Point(11, 22)._replace(x=100)
142
Raymond Hettingere850c462008-01-10 20:37:12 +0000143 Point3D = namedtuple('Point3D', Point._fields + ('z',))
144 print Point3D.__doc__
145
Raymond Hettingerc37e5e02007-03-01 06:16:43 +0000146 import doctest
Raymond Hettinger01a09572007-10-23 20:37:41 +0000147 TestResults = namedtuple('TestResults', 'failed attempted')
Raymond Hettingerc37e5e02007-03-01 06:16:43 +0000148 print TestResults(*doctest.testmod())