diff options
author | Raymond Hettinger <python@rcn.com> | 2007-10-08 09:14:28 (GMT) |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2007-10-08 09:14:28 (GMT) |
commit | 2115bbc4da683e6878b361a80c74203a2559e61d (patch) | |
tree | 6336a03ba5981375fed9fb7d88f287535a0fcf57 /Lib/collections.py | |
parent | a970c2216645a86b532f68bfb93b8ec9da858b0d (diff) | |
download | cpython-2115bbc4da683e6878b361a80c74203a2559e61d.zip cpython-2115bbc4da683e6878b361a80c74203a2559e61d.tar.gz cpython-2115bbc4da683e6878b361a80c74203a2559e61d.tar.bz2 |
Add comments to NamedTuple code.
Let the field spec be either a string or a non-string sequence (suggested by Martin Blais with use cases).
Improve the error message in the case of a SyntaxError (caused by a duplicate field name).
Diffstat (limited to 'Lib/collections.py')
-rw-r--r-- | Lib/collections.py | 21 |
1 files changed, 18 insertions, 3 deletions
diff --git a/Lib/collections.py b/Lib/collections.py index 7b10712..c5eb79c 100644 --- a/Lib/collections.py +++ b/Lib/collections.py @@ -4,7 +4,7 @@ from _collections import deque, defaultdict from operator import itemgetter as _itemgetter import sys as _sys -def NamedTuple(typename, s, verbose=False): +def NamedTuple(typename, field_names, verbose=False): """Returns a new subclass of tuple with named fields. >>> Point = NamedTuple('Point', 'x y') @@ -28,11 +28,16 @@ def NamedTuple(typename, s, verbose=False): """ - field_names = tuple(s.replace(',', ' ').split()) # names separated by spaces and/or commas + # Parse and validate the field names + if isinstance(field_names, basestring): + field_names = s.replace(',', ' ').split() # names separated by spaces and/or commas + field_names = tuple(field_names) if not ''.join((typename,) + field_names).replace('_', '').isalnum(): raise ValueError('Type names and field names can only contain alphanumeric characters and underscores') if any(name.startswith('__') and name.endswith('__') for name in field_names): raise ValueError('Field names cannot start and end with double underscores') + + # Create and fill-in the class template argtxt = repr(field_names).replace("'", "")[1:-1] # tuple repr without parens or quotes reprtxt = ', '.join('%s=%%r' % name for name in field_names) template = '''class %(typename)s(tuple): @@ -53,11 +58,21 @@ def NamedTuple(typename, s, verbose=False): template += ' %s = property(itemgetter(%d))\n' % (name, i) if verbose: print template + + # Execute the template string in a temporary namespace m = dict(itemgetter=_itemgetter) - exec template in m + try: + exec template in m + except SyntaxError, e: + raise SyntaxError(e.message + ':\n' + template) result = m[typename] + + # For pickling to work, the __module__ variable needs to be set to the frame + # where the named tuple is created. Bypass this step in enviroments where + # sys._getframe is not defined (Jython for example). if hasattr(_sys, '_getframe'): result.__module__ = _sys._getframe(1).f_globals['__name__'] + return result |