diff options
author | Raymond Hettinger <python@rcn.com> | 2009-02-10 01:24:05 (GMT) |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2009-02-10 01:24:05 (GMT) |
commit | 322daea7c3ba7fa73b09ebd50a78078d23d318a5 (patch) | |
tree | 6b760626e3024d3f5af361c25eb72d0adac8e3c0 /Lib/collections.py | |
parent | c5eba1e4f72e36368c52f56ebf7a5dde00ca3ee0 (diff) | |
download | cpython-322daea7c3ba7fa73b09ebd50a78078d23d318a5.zip cpython-322daea7c3ba7fa73b09ebd50a78078d23d318a5.tar.gz cpython-322daea7c3ba7fa73b09ebd50a78078d23d318a5.tar.bz2 |
Issue 1818: collections.namedtuple() to support automatic renaming of invalid fieldnames.
Diffstat (limited to 'Lib/collections.py')
-rw-r--r-- | Lib/collections.py | 14 |
1 files changed, 12 insertions, 2 deletions
diff --git a/Lib/collections.py b/Lib/collections.py index 0fddb97..8d49cd5 100644 --- a/Lib/collections.py +++ b/Lib/collections.py @@ -16,7 +16,7 @@ from itertools import repeat as _repeat, chain as _chain, starmap as _starmap, i ### namedtuple ################################################################################ -def namedtuple(typename, field_names, verbose=False): +def namedtuple(typename, field_names, verbose=False, rename=False): """Returns a new subclass of tuple with named fields. >>> Point = namedtuple('Point', 'x y') @@ -45,6 +45,16 @@ def namedtuple(typename, field_names, verbose=False): if isinstance(field_names, basestring): field_names = field_names.replace(',', ' ').split() # names separated by whitespace and/or commas field_names = tuple(map(str, field_names)) + if rename: + names = list(field_names) + seen = set() + for i, name in enumerate(names): + if (not all(c.isalnum() or c=='_' for c in name) or _iskeyword(name) + or not name or name[0].isdigit() or name.startswith('_') + or name in seen): + names[i] = '_%d' % (i+1) + seen.add(name) + field_names = tuple(names) for name in (typename,) + field_names: if not all(c.isalnum() or c=='_' for c in name): raise ValueError('Type names and field names can only contain alphanumeric characters and underscores: %r' % name) @@ -54,7 +64,7 @@ def namedtuple(typename, field_names, verbose=False): raise ValueError('Type names and field names cannot start with a number: %r' % name) seen_names = set() for name in field_names: - if name.startswith('_'): + if name.startswith('_') and not rename: raise ValueError('Field names cannot start with an underscore: %r' % name) if name in seen_names: raise ValueError('Encountered duplicate field name: %r' % name) |