Index: Lib/collections.py =================================================================== --- Lib/collections.py (revision 66617) +++ Lib/collections.py (working copy) @@ -5,11 +5,62 @@ import _abcoll __all__ += _abcoll.__all__ +import sys as _sys from _collections import deque, defaultdict from operator import itemgetter as _itemgetter from keyword import iskeyword as _iskeyword -import sys as _sys +from itertools import imap as _imap +class _NamedTuple(tuple): + + __slots__ = () + + def __new__(cls, *args, **kwargs): + if not len(args) + len(kwargs) == cls._field_len: + raise TypeError("Invalid number of arguments!") + arguments = list(args) + if kwargs: + def name_to_position(pair): + return cls._field_names[pair[0]] + try: + sorted_args = sorted(kwargs.iteritems(), key=name_to_position) + # sorted by key, but we only need the value + arguments.extend(_imap(_itemgetter(1), sorted_args)) + except KeyError: + raise TypeError("Invalid arguments!") + return tuple.__new__(cls, arguments) + + def _asdict(self): + 'Return a new dict which maps field names to their values' + return dict((name, self[idx]) for name, idx in + self._field_names.iteritems()) + + def _replace(self, **kwds): + 'Return a new %s object replacing specified fields with new values' % ( + self.__class__.__name__) + result = self._make(map(kwds.pop, self._fields, self)) + if kwds: + raise ValueError('Got unexpected field names: %s' % kwds.keys()) + return result + + @classmethod + def _make(cls, iterable, new=tuple.__new__, len=len): + 'Make a new %(typename)s object from a sequence or iterable' + result = new(cls, iterable) + if len(result) != cls._field_len: + raise TypeError('Expected %d arguments, got %d' % + (cls._field_len, len(result))) + return result + + def __repr__(self): + return "%s(%s)" % (self.__class__.__name__, + ', '.join("%s=%s" % (name, self[self._field_names[name]]) + for name in sorted(self._asdict()))) + + def __getnewargs__(self): + return tuple(self) + + def namedtuple(typename, field_names, verbose=False): """Returns a new subclass of tuple with named fields. @@ -33,13 +84,13 @@ Point(x=100, y=22) """ - # Parse and validate the field names. Validation serves two purposes, # generating informative error messages and preventing template injection attacks. if isinstance(field_names, basestring): - field_names = field_names.replace(',', ' ').split() # names separated by whitespace and/or commas - field_names = tuple(field_names) - for name in (typename,) + field_names: + # names separated by whitespace and/or commas + field_names = field_names.replace(',', ' ').split() + field_names = list(field_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) if _iskeyword(name): @@ -54,50 +105,22 @@ raise ValueError('Encountered duplicate field name: %r' % name) seen_names.add(name) - # Create and fill-in the class template - numfields = len(field_names) - argtxt = repr(field_names).replace("'", "")[1:-1] # tuple repr without parens or quotes - reprtxt = ', '.join('%s=%%r' % name for name in field_names) - dicttxt = ', '.join('%r: t[%d]' % (name, pos) for pos, name in enumerate(field_names)) - template = '''class %(typename)s(tuple): - '%(typename)s(%(argtxt)s)' \n - __slots__ = () \n - _fields = %(field_names)r \n - def __new__(cls, %(argtxt)s): - return tuple.__new__(cls, (%(argtxt)s)) \n - @classmethod - def _make(cls, iterable, new=tuple.__new__, len=len): - 'Make a new %(typename)s object from a sequence or iterable' - result = new(cls, iterable) - if len(result) != %(numfields)d: - raise TypeError('Expected %(numfields)d arguments, got %%d' %% len(result)) - return result \n - def __repr__(self): - return '%(typename)s(%(reprtxt)s)' %% self \n - def _asdict(t): - 'Return a new dict which maps field names to their values' - return {%(dicttxt)s} \n - def _replace(self, **kwds): - 'Return a new %(typename)s object replacing specified fields with new values' - result = self._make(map(kwds.pop, %(field_names)r, self)) - if kwds: - raise ValueError('Got unexpected field names: %%r' %% kwds.keys()) - return result \n - def __getnewargs__(self): - return tuple(self) \n\n''' % locals() - for i, name in enumerate(field_names): - template += ' %s = property(itemgetter(%d))\n' % (name, i) + name_to_field = dict((v, idx) for idx, v in (enumerate(field_names))) + attributes = {'_field_names' : name_to_field, + '_fields' : tuple(field_names), + # immutable and we need it often + '_field_len' : len(field_names), + '__slots__' : (), + '__doc__': "%s(%s)" % (typename, ', '.join(field_names))} + + # create properties + for attr, idx in name_to_field.iteritems(): + attributes[attr] = property(_itemgetter(idx)) if verbose: - print template + print attributes - # Execute the template string in a temporary namespace and - # support tracing utilities by setting a value for frame.f_globals['__name__'] - namespace = dict(itemgetter=_itemgetter, __name__='namedtuple_%s' % typename) - try: - exec template in namespace - except SyntaxError, e: - raise SyntaxError(e.message + ':\n' + template) - result = namespace[typename] + # create the new class, deriving from _NamedTuple + result = type(typename, (_NamedTuple,), attributes) # 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 @@ -107,11 +130,6 @@ return result - - - - - if __name__ == '__main__': # verify that instances can be pickled from cPickle import loads, dumps