Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9a3f4cb

Browse files
committed
Simplify the named tuple template by using the new string format syntax.
1 parent 50105d3 commit 9a3f4cb

1 file changed

Lines changed: 15 additions & 15 deletions

File tree

Lib/collections/__init__.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -290,46 +290,46 @@ def namedtuple(typename, field_names, verbose=False, rename=False):
290290
# Create and fill-in the class template
291291
numfields = len(field_names)
292292
argtxt = repr(field_names).replace("'", "")[1:-1] # tuple repr without parens or quotes
293-
reprtxt = ', '.join('%s=%%r' % name for name in field_names)
294-
template = '''class %(typename)s(tuple):
295-
'%(typename)s(%(argtxt)s)'
293+
reprtxt = ', '.join('{}=%r'.format(name) for name in field_names)
294+
template = '''class {typename}(tuple):
295+
'{typename}({argtxt})'
296296
297297
__slots__ = ()
298298
299-
_fields = %(field_names)r
299+
_fields = {field_names!r}
300300
301-
def __new__(_cls, %(argtxt)s):
302-
'Create new instance of %(typename)s(%(argtxt)s)'
303-
return _tuple.__new__(_cls, (%(argtxt)s))
301+
def __new__(_cls, {argtxt}):
302+
'Create new instance of {typename}({argtxt})'
303+
return _tuple.__new__(_cls, ({argtxt}))
304304
305305
@classmethod
306306
def _make(cls, iterable, new=tuple.__new__, len=len):
307-
'Make a new %(typename)s object from a sequence or iterable'
307+
'Make a new {typename} object from a sequence or iterable'
308308
result = new(cls, iterable)
309-
if len(result) != %(numfields)d:
310-
raise TypeError('Expected %(numfields)d arguments, got %%d' %% len(result))
309+
if len(result) != {numfields:d}:
310+
raise TypeError('Expected {numfields:d} arguments, got %d' % len(result))
311311
return result
312312
313313
def __repr__(self):
314314
'Return a nicely formatted representation string'
315-
return self.__class__.__name__ + '(%(reprtxt)s)' %% self
315+
return self.__class__.__name__ + '({reprtxt})' % self
316316
317317
def _asdict(self):
318318
'Return a new OrderedDict which maps field names to their values'
319319
return OrderedDict(zip(self._fields, self))
320320
321321
def _replace(_self, **kwds):
322-
'Return a new %(typename)s object replacing specified fields with new values'
323-
result = _self._make(map(kwds.pop, %(field_names)r, _self))
322+
'Return a new {typename} object replacing specified fields with new values'
323+
result = _self._make(map(kwds.pop, {field_names!r}, _self))
324324
if kwds:
325-
raise ValueError('Got unexpected field names: %%r' %% kwds.keys())
325+
raise ValueError('Got unexpected field names: %r' % kwds.keys())
326326
return result
327327
328328
def __getnewargs__(self):
329329
'Return self as a plain tuple. Used by copy and pickle.'
330330
return tuple(self)
331331
332-
''' % locals()
332+
'''.format(**locals())
333333
for i, name in enumerate(field_names):
334334
template += " %s = _property(_itemgetter(%d), doc='Alias for field number %d')\n" % (name, i, i)
335335
if verbose:

0 commit comments

Comments
 (0)