blob: e0782d03ef57942642c6648dce6fa2520a2d2ff7 [file] [log] [blame]
Raymond Hettinger01a09572007-10-23 20:37:41 +00001__all__ = ['deque', 'defaultdict', 'namedtuple']
Raymond Hettingereb979882007-02-28 18:37:52 +00002
3from _collections import deque, defaultdict
Raymond Hettingerc37e5e02007-03-01 06:16:43 +00004from operator import itemgetter as _itemgetter
Raymond Hettingerabfd8df2007-10-16 21:28:32 +00005from keyword import iskeyword as _iskeyword
Raymond Hettingerc37e5e02007-03-01 06:16:43 +00006import sys as _sys
7
Raymond Hettinger01a09572007-10-23 20:37:41 +00008def namedtuple(typename, field_names, verbose=False):
Raymond Hettingerc37e5e02007-03-01 06:16:43 +00009 """Returns a new subclass of tuple with named fields.
10
Raymond Hettinger01a09572007-10-23 20:37:41 +000011 >>> Point = namedtuple('Point', 'x y')
Raymond Hettingerd36a60e2007-09-17 00:55:00 +000012 >>> Point.__doc__ # docstring for the new class
Raymond Hettingerc37e5e02007-03-01 06:16:43 +000013 'Point(x, y)'
Raymond Hettingerd36a60e2007-09-17 00:55:00 +000014 >>> p = Point(11, y=22) # instantiate with positional args or keywords
15 >>> p[0] + p[1] # works just like the tuple (11, 22)
Raymond Hettingerc37e5e02007-03-01 06:16:43 +000016 33
Raymond Hettingerd36a60e2007-09-17 00:55:00 +000017 >>> x, y = p # unpacks just like a tuple
Raymond Hettingerc37e5e02007-03-01 06:16:43 +000018 >>> x, y
19 (11, 22)
Raymond Hettingerd36a60e2007-09-17 00:55:00 +000020 >>> p.x + p.y # fields also accessable by name
Raymond Hettingerc37e5e02007-03-01 06:16:43 +000021 33
Raymond Hettingera7fc4b12007-10-05 02:47:07 +000022 >>> d = p.__asdict__() # convert to a dictionary
23 >>> d['x']
24 11
25 >>> Point(**d) # convert from a dictionary
Raymond Hettingerc37e5e02007-03-01 06:16:43 +000026 Point(x=11, y=22)
Raymond Hettingerd36a60e2007-09-17 00:55:00 +000027 >>> p.__replace__('x', 100) # __replace__() is like str.replace() but targets a named field
28 Point(x=100, y=22)
Raymond Hettingerc37e5e02007-03-01 06:16:43 +000029
30 """
31
Raymond Hettinger2115bbc2007-10-08 09:14:28 +000032 # Parse and validate the field names
33 if isinstance(field_names, basestring):
Raymond Hettinger0e1d6062007-10-08 10:11:51 +000034 field_names = field_names.replace(',', ' ').split() # names separated by whitespace and/or commas
Raymond Hettinger2115bbc2007-10-08 09:14:28 +000035 field_names = tuple(field_names)
Raymond Hettinger050afbf2007-10-16 19:18:30 +000036 for name in (typename,) + field_names:
37 if not name.replace('_', '').isalnum():
38 raise ValueError('Type names and field names can only contain alphanumeric characters and underscores: %r' % name)
Raymond Hettingerabfd8df2007-10-16 21:28:32 +000039 if _iskeyword(name):
40 raise ValueError('Type names and field names cannot be a keyword: %r' % name)
Raymond Hettinger050afbf2007-10-16 19:18:30 +000041 if name[0].isdigit():
42 raise ValueError('Type names and field names cannot start with a number: %r' % name)
Raymond Hettinger163f6222007-10-09 01:36:23 +000043 seen_names = set()
44 for name in field_names:
45 if name.startswith('__') and name.endswith('__'):
Raymond Hettinger050afbf2007-10-16 19:18:30 +000046 raise ValueError('Field names cannot start and end with double underscores: %r' % name)
Raymond Hettinger163f6222007-10-09 01:36:23 +000047 if name in seen_names:
Raymond Hettinger050afbf2007-10-16 19:18:30 +000048 raise ValueError('Encountered duplicate field name: %r' % name)
Raymond Hettinger163f6222007-10-09 01:36:23 +000049 seen_names.add(name)
Raymond Hettinger2115bbc2007-10-08 09:14:28 +000050
51 # Create and fill-in the class template
Raymond Hettinger2b03d452007-09-18 03:33:19 +000052 argtxt = repr(field_names).replace("'", "")[1:-1] # tuple repr without parens or quotes
Raymond Hettinger5a41daf2007-05-19 01:11:16 +000053 reprtxt = ', '.join('%s=%%r' % name for name in field_names)
54 template = '''class %(typename)s(tuple):
55 '%(typename)s(%(argtxt)s)'
56 __slots__ = ()
Raymond Hettingerd36a60e2007-09-17 00:55:00 +000057 __fields__ = %(field_names)r
Raymond Hettinger5a41daf2007-05-19 01:11:16 +000058 def __new__(cls, %(argtxt)s):
Raymond Hettinger2b03d452007-09-18 03:33:19 +000059 return tuple.__new__(cls, (%(argtxt)s))
Raymond Hettinger5a41daf2007-05-19 01:11:16 +000060 def __repr__(self):
61 return '%(typename)s(%(reprtxt)s)' %% self
Raymond Hettingera7fc4b12007-10-05 02:47:07 +000062 def __asdict__(self, dict=dict, zip=zip):
63 'Return a new dict mapping field names to their values'
64 return dict(zip(%(field_names)r, self))
65 def __replace__(self, field, value, dict=dict, zip=zip):
Raymond Hettingerd36a60e2007-09-17 00:55:00 +000066 'Return a new %(typename)s object replacing one field with a new value'
Raymond Hettinger2b03d452007-09-18 03:33:19 +000067 return %(typename)s(**dict(zip(%(field_names)r, self) + [(field, value)])) \n''' % locals()
Raymond Hettinger5a41daf2007-05-19 01:11:16 +000068 for i, name in enumerate(field_names):
Raymond Hettinger2b03d452007-09-18 03:33:19 +000069 template += ' %s = property(itemgetter(%d))\n' % (name, i)
70 if verbose:
71 print template
Raymond Hettinger2115bbc2007-10-08 09:14:28 +000072
73 # Execute the template string in a temporary namespace
Raymond Hettinger0e1d6062007-10-08 10:11:51 +000074 namespace = dict(itemgetter=_itemgetter)
Raymond Hettinger2115bbc2007-10-08 09:14:28 +000075 try:
Raymond Hettinger0e1d6062007-10-08 10:11:51 +000076 exec template in namespace
Raymond Hettinger2115bbc2007-10-08 09:14:28 +000077 except SyntaxError, e:
78 raise SyntaxError(e.message + ':\n' + template)
Raymond Hettinger0e1d6062007-10-08 10:11:51 +000079 result = namespace[typename]
Raymond Hettinger2115bbc2007-10-08 09:14:28 +000080
81 # For pickling to work, the __module__ variable needs to be set to the frame
82 # where the named tuple is created. Bypass this step in enviroments where
83 # sys._getframe is not defined (Jython for example).
Raymond Hettinger5a41daf2007-05-19 01:11:16 +000084 if hasattr(_sys, '_getframe'):
85 result.__module__ = _sys._getframe(1).f_globals['__name__']
Raymond Hettinger2115bbc2007-10-08 09:14:28 +000086
Raymond Hettinger5a41daf2007-05-19 01:11:16 +000087 return result
Raymond Hettingerc37e5e02007-03-01 06:16:43 +000088
Raymond Hettingerc37e5e02007-03-01 06:16:43 +000089
Raymond Hettingerc37e5e02007-03-01 06:16:43 +000090
Raymond Hettingerc37e5e02007-03-01 06:16:43 +000091
Raymond Hettingerc37e5e02007-03-01 06:16:43 +000092
93
94if __name__ == '__main__':
Raymond Hettingerd36a60e2007-09-17 00:55:00 +000095 # verify that instances can be pickled
Raymond Hettingerc37e5e02007-03-01 06:16:43 +000096 from cPickle import loads, dumps
Raymond Hettinger01a09572007-10-23 20:37:41 +000097 Point = namedtuple('Point', 'x, y', True)
Raymond Hettingerc37e5e02007-03-01 06:16:43 +000098 p = Point(x=10, y=20)
99 assert p == loads(dumps(p))
100
101 import doctest
Raymond Hettinger01a09572007-10-23 20:37:41 +0000102 TestResults = namedtuple('TestResults', 'failed attempted')
Raymond Hettingerc37e5e02007-03-01 06:16:43 +0000103 print TestResults(*doctest.testmod())