Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 1 | __all__ = ['deque', 'defaultdict', 'namedtuple'] |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2 | |
| 3 | from _collections import deque, defaultdict |
| 4 | from operator import itemgetter as _itemgetter |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5 | from keyword import iskeyword as _iskeyword |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 6 | import sys as _sys |
| 7 | |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 8 | # For bootstrapping reasons, the collection ABCs are defined in _abcoll.py. |
| 9 | # They should however be considered an integral part of collections.py. |
| 10 | from _abcoll import * |
| 11 | import _abcoll |
| 12 | __all__ += _abcoll.__all__ |
| 13 | |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 14 | def namedtuple(typename, field_names, verbose=False): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 15 | """Returns a new subclass of tuple with named fields. |
| 16 | |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 17 | >>> Point = namedtuple('Point', 'x y') |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 18 | >>> Point.__doc__ # docstring for the new class |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 19 | 'Point(x, y)' |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 20 | >>> p = Point(11, y=22) # instantiate with positional args or keywords |
| 21 | >>> p[0] + p[1] # works just like the tuple (11, 22) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 22 | 33 |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 23 | >>> x, y = p # unpacks just like a tuple |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 24 | >>> x, y |
| 25 | (11, 22) |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 26 | >>> p.x + p.y # fields also accessable by name |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 27 | 33 |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 28 | >>> d = p.__asdict__() # convert to a dictionary |
| 29 | >>> d['x'] |
| 30 | 11 |
| 31 | >>> Point(**d) # convert from a dictionary |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 32 | Point(x=11, y=22) |
Guido van Rossum | 3d392eb | 2007-11-16 00:35:22 +0000 | [diff] [blame] | 33 | >>> p.__replace__(x=100) # __replace__() is like str.replace() but targets named fields |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 34 | Point(x=100, y=22) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 35 | |
| 36 | """ |
| 37 | |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 38 | # Parse and validate the field names |
| 39 | if isinstance(field_names, str): |
| 40 | field_names = field_names.replace(',', ' ').split() # names separated by whitespace and/or commas |
| 41 | field_names = tuple(field_names) |
| 42 | for name in (typename,) + field_names: |
Christian Heimes | b9eccbf | 2007-12-05 20:18:38 +0000 | [diff] [blame] | 43 | if not all(c.isalnum() or c=='_' for c in name): |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 44 | raise ValueError('Type names and field names can only contain alphanumeric characters and underscores: %r' % name) |
| 45 | if _iskeyword(name): |
| 46 | raise ValueError('Type names and field names cannot be a keyword: %r' % name) |
| 47 | if name[0].isdigit(): |
| 48 | raise ValueError('Type names and field names cannot start with a number: %r' % name) |
| 49 | seen_names = set() |
| 50 | for name in field_names: |
Christian Heimes | b9eccbf | 2007-12-05 20:18:38 +0000 | [diff] [blame] | 51 | if name.startswith('__') and name.endswith('__') and len(name) > 3: |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 52 | raise ValueError('Field names cannot start and end with double underscores: %r' % name) |
| 53 | if name in seen_names: |
| 54 | raise ValueError('Encountered duplicate field name: %r' % name) |
| 55 | seen_names.add(name) |
| 56 | |
| 57 | # Create and fill-in the class template |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 58 | argtxt = repr(field_names).replace("'", "")[1:-1] # tuple repr without parens or quotes |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 59 | reprtxt = ', '.join('%s=%%r' % name for name in field_names) |
| 60 | template = '''class %(typename)s(tuple): |
| 61 | '%(typename)s(%(argtxt)s)' |
| 62 | __slots__ = () |
Guido van Rossum | 3d392eb | 2007-11-16 00:35:22 +0000 | [diff] [blame] | 63 | __fields__ = property(lambda self: %(field_names)r) |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 64 | def __new__(cls, %(argtxt)s): |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 65 | return tuple.__new__(cls, (%(argtxt)s)) |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 66 | def __repr__(self): |
| 67 | return '%(typename)s(%(reprtxt)s)' %% self |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 68 | def __asdict__(self, dict=dict, zip=zip): |
| 69 | 'Return a new dict mapping field names to their values' |
| 70 | return dict(zip(%(field_names)r, self)) |
Guido van Rossum | 3d392eb | 2007-11-16 00:35:22 +0000 | [diff] [blame] | 71 | def __replace__(self, **kwds): |
| 72 | 'Return a new %(typename)s object replacing specified fields with new values' |
Guido van Rossum | 053b4f3 | 2007-11-16 00:48:13 +0000 | [diff] [blame] | 73 | return %(typename)s(**dict(list(zip(%(field_names)r, self)) + list(kwds.items()))) \n''' % locals() |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 74 | for i, name in enumerate(field_names): |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 75 | template += ' %s = property(itemgetter(%d))\n' % (name, i) |
| 76 | if verbose: |
| 77 | print(template) |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 78 | |
| 79 | # Execute the template string in a temporary namespace |
| 80 | namespace = dict(itemgetter=_itemgetter) |
| 81 | try: |
| 82 | exec(template, namespace) |
| 83 | except SyntaxError as e: |
| 84 | raise SyntaxError(e.message + ':\n' + template) |
| 85 | result = namespace[typename] |
| 86 | |
| 87 | # For pickling to work, the __module__ variable needs to be set to the frame |
| 88 | # where the named tuple is created. Bypass this step in enviroments where |
| 89 | # sys._getframe is not defined (Jython for example). |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 90 | if hasattr(_sys, '_getframe'): |
| 91 | result.__module__ = _sys._getframe(1).f_globals['__name__'] |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 92 | |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 93 | return result |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 94 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 95 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 96 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 97 | |
| 98 | |
| 99 | if __name__ == '__main__': |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 100 | # verify that instances can be pickled |
Guido van Rossum | 99603b0 | 2007-07-20 00:22:32 +0000 | [diff] [blame] | 101 | from pickle import loads, dumps |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 102 | Point = namedtuple('Point', 'x, y', True) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 103 | p = Point(x=10, y=20) |
| 104 | assert p == loads(dumps(p)) |
| 105 | |
Guido van Rossum | 3d392eb | 2007-11-16 00:35:22 +0000 | [diff] [blame] | 106 | # test and demonstrate ability to override methods |
| 107 | Point.__repr__ = lambda self: 'Point(%.3f, %.3f)' % self |
Guido van Rossum | 053b4f3 | 2007-11-16 00:48:13 +0000 | [diff] [blame] | 108 | print(p) |
Guido van Rossum | 3d392eb | 2007-11-16 00:35:22 +0000 | [diff] [blame] | 109 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 110 | import doctest |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 111 | TestResults = namedtuple('TestResults', 'failed attempted') |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 112 | print(TestResults(*doctest.testmod())) |