mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
Simplify the named tuple template by using the new string format syntax.
This commit is contained in:
parent
50105d3840
commit
9a3f4cbfc3
1 changed files with 15 additions and 15 deletions
|
@ -290,46 +290,46 @@ def namedtuple(typename, field_names, verbose=False, rename=False):
|
||||||
# Create and fill-in the class template
|
# Create and fill-in the class template
|
||||||
numfields = len(field_names)
|
numfields = len(field_names)
|
||||||
argtxt = repr(field_names).replace("'", "")[1:-1] # tuple repr without parens or quotes
|
argtxt = repr(field_names).replace("'", "")[1:-1] # tuple repr without parens or quotes
|
||||||
reprtxt = ', '.join('%s=%%r' % name for name in field_names)
|
reprtxt = ', '.join('{}=%r'.format(name) for name in field_names)
|
||||||
template = '''class %(typename)s(tuple):
|
template = '''class {typename}(tuple):
|
||||||
'%(typename)s(%(argtxt)s)'
|
'{typename}({argtxt})'
|
||||||
|
|
||||||
__slots__ = ()
|
__slots__ = ()
|
||||||
|
|
||||||
_fields = %(field_names)r
|
_fields = {field_names!r}
|
||||||
|
|
||||||
def __new__(_cls, %(argtxt)s):
|
def __new__(_cls, {argtxt}):
|
||||||
'Create new instance of %(typename)s(%(argtxt)s)'
|
'Create new instance of {typename}({argtxt})'
|
||||||
return _tuple.__new__(_cls, (%(argtxt)s))
|
return _tuple.__new__(_cls, ({argtxt}))
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def _make(cls, iterable, new=tuple.__new__, len=len):
|
def _make(cls, iterable, new=tuple.__new__, len=len):
|
||||||
'Make a new %(typename)s object from a sequence or iterable'
|
'Make a new {typename} object from a sequence or iterable'
|
||||||
result = new(cls, iterable)
|
result = new(cls, iterable)
|
||||||
if len(result) != %(numfields)d:
|
if len(result) != {numfields:d}:
|
||||||
raise TypeError('Expected %(numfields)d arguments, got %%d' %% len(result))
|
raise TypeError('Expected {numfields:d} arguments, got %d' % len(result))
|
||||||
return result
|
return result
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
'Return a nicely formatted representation string'
|
'Return a nicely formatted representation string'
|
||||||
return self.__class__.__name__ + '(%(reprtxt)s)' %% self
|
return self.__class__.__name__ + '({reprtxt})' % self
|
||||||
|
|
||||||
def _asdict(self):
|
def _asdict(self):
|
||||||
'Return a new OrderedDict which maps field names to their values'
|
'Return a new OrderedDict which maps field names to their values'
|
||||||
return OrderedDict(zip(self._fields, self))
|
return OrderedDict(zip(self._fields, self))
|
||||||
|
|
||||||
def _replace(_self, **kwds):
|
def _replace(_self, **kwds):
|
||||||
'Return a new %(typename)s object replacing specified fields with new values'
|
'Return a new {typename} object replacing specified fields with new values'
|
||||||
result = _self._make(map(kwds.pop, %(field_names)r, _self))
|
result = _self._make(map(kwds.pop, {field_names!r}, _self))
|
||||||
if kwds:
|
if kwds:
|
||||||
raise ValueError('Got unexpected field names: %%r' %% kwds.keys())
|
raise ValueError('Got unexpected field names: %r' % kwds.keys())
|
||||||
return result
|
return result
|
||||||
|
|
||||||
def __getnewargs__(self):
|
def __getnewargs__(self):
|
||||||
'Return self as a plain tuple. Used by copy and pickle.'
|
'Return self as a plain tuple. Used by copy and pickle.'
|
||||||
return tuple(self)
|
return tuple(self)
|
||||||
|
|
||||||
''' % locals()
|
'''.format(**locals())
|
||||||
for i, name in enumerate(field_names):
|
for i, name in enumerate(field_names):
|
||||||
template += " %s = _property(_itemgetter(%d), doc='Alias for field number %d')\n" % (name, i, i)
|
template += " %s = _property(_itemgetter(%d), doc='Alias for field number %d')\n" % (name, i, i)
|
||||||
if verbose:
|
if verbose:
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue