/Lib/collections.py

http://unladen-swallow.googlecode.com/ · Python · 147 lines · 141 code · 2 blank · 4 comment · 6 complexity · e8c73288ab14216356a1e5966b0f8503 MD5 · raw file

  1. __all__ = ['deque', 'defaultdict', 'namedtuple']
  2. # For bootstrapping reasons, the collection ABCs are defined in _abcoll.py.
  3. # They should however be considered an integral part of collections.py.
  4. from _abcoll import *
  5. import _abcoll
  6. __all__ += _abcoll.__all__
  7. from _collections import deque, defaultdict
  8. from operator import itemgetter as _itemgetter
  9. from keyword import iskeyword as _iskeyword
  10. import sys as _sys
  11. def namedtuple(typename, field_names, verbose=False):
  12. """Returns a new subclass of tuple with named fields.
  13. >>> Point = namedtuple('Point', 'x y')
  14. >>> p = Point(11, y=22) # instantiate with positional args or keywords
  15. >>> p[0] + p[1] # indexable like a plain tuple
  16. 33
  17. >>> x, y = p # unpack like a regular tuple
  18. >>> x, y
  19. (11, 22)
  20. >>> p.x + p.y # fields also accessable by name
  21. 33
  22. >>> d = p._asdict() # convert to a dictionary
  23. >>> d['x']
  24. 11
  25. >>> Point(**d) # convert from a dictionary
  26. Point(x=11, y=22)
  27. >>> p._replace(x=100) # _replace() is like str.replace() but targets named fields
  28. Point(x=100, y=22)
  29. """
  30. # Parse and validate the field names. Validation serves two purposes,
  31. # generating informative error messages and preventing template injection attacks.
  32. if isinstance(field_names, basestring):
  33. field_names = field_names.replace(',', ' ').split() # names separated by whitespace and/or commas
  34. field_names = tuple(map(str, field_names))
  35. for name in (typename,) + field_names:
  36. if not all(c.isalnum() or c=='_' for c in name):
  37. raise ValueError('Type names and field names can only contain alphanumeric characters and underscores: %r' % name)
  38. if _iskeyword(name):
  39. raise ValueError('Type names and field names cannot be a keyword: %r' % name)
  40. if name[0].isdigit():
  41. raise ValueError('Type names and field names cannot start with a number: %r' % name)
  42. seen_names = set()
  43. for name in field_names:
  44. if name.startswith('_'):
  45. raise ValueError('Field names cannot start with an underscore: %r' % name)
  46. if name in seen_names:
  47. raise ValueError('Encountered duplicate field name: %r' % name)
  48. seen_names.add(name)
  49. # Create and fill-in the class template
  50. numfields = len(field_names)
  51. argtxt = repr(field_names).replace("'", "")[1:-1] # tuple repr without parens or quotes
  52. reprtxt = ', '.join('%s=%%r' % name for name in field_names)
  53. dicttxt = ', '.join('%r: t[%d]' % (name, pos) for pos, name in enumerate(field_names))
  54. template = '''class %(typename)s(tuple):
  55. '%(typename)s(%(argtxt)s)' \n
  56. __slots__ = () \n
  57. _fields = %(field_names)r \n
  58. def __new__(_cls, %(argtxt)s):
  59. return _tuple.__new__(_cls, (%(argtxt)s)) \n
  60. @classmethod
  61. def _make(cls, iterable, new=tuple.__new__, len=len):
  62. 'Make a new %(typename)s object from a sequence or iterable'
  63. result = new(cls, iterable)
  64. if len(result) != %(numfields)d:
  65. raise TypeError('Expected %(numfields)d arguments, got %%d' %% len(result))
  66. return result \n
  67. def __repr__(self):
  68. return '%(typename)s(%(reprtxt)s)' %% self \n
  69. def _asdict(t):
  70. 'Return a new dict which maps field names to their values'
  71. return {%(dicttxt)s} \n
  72. def _replace(_self, **kwds):
  73. 'Return a new %(typename)s object replacing specified fields with new values'
  74. result = _self._make(map(kwds.pop, %(field_names)r, _self))
  75. if kwds:
  76. raise ValueError('Got unexpected field names: %%r' %% kwds.keys())
  77. return result \n
  78. def __getnewargs__(self):
  79. return tuple(self) \n\n''' % locals()
  80. for i, name in enumerate(field_names):
  81. template += ' %s = _property(_itemgetter(%d))\n' % (name, i)
  82. if verbose:
  83. print template
  84. # Execute the template string in a temporary namespace and
  85. # support tracing utilities by setting a value for frame.f_globals['__name__']
  86. namespace = dict(_itemgetter=_itemgetter, __name__='namedtuple_%s' % typename,
  87. _property=property, _tuple=tuple)
  88. try:
  89. exec template in namespace
  90. except SyntaxError, e:
  91. raise SyntaxError(e.message + ':\n' + template)
  92. result = namespace[typename]
  93. # For pickling to work, the __module__ variable needs to be set to the frame
  94. # where the named tuple is created. Bypass this step in enviroments where
  95. # sys._getframe is not defined (Jython for example).
  96. if hasattr(_sys, '_getframe'):
  97. result.__module__ = _sys._getframe(1).f_globals.get('__name__', '__main__')
  98. return result
  99. if __name__ == '__main__':
  100. # verify that instances can be pickled
  101. from cPickle import loads, dumps
  102. Point = namedtuple('Point', 'x, y', True)
  103. p = Point(x=10, y=20)
  104. assert p == loads(dumps(p))
  105. # test and demonstrate ability to override methods
  106. class Point(namedtuple('Point', 'x y')):
  107. __slots__ = ()
  108. @property
  109. def hypot(self):
  110. return (self.x ** 2 + self.y ** 2) ** 0.5
  111. def __str__(self):
  112. return 'Point: x=%6.3f y=%6.3f hypot=%6.3f' % (self.x, self.y, self.hypot)
  113. for p in Point(3, 4), Point(14, 5/7.):
  114. print p
  115. class Point(namedtuple('Point', 'x y')):
  116. 'Point class with optimized _make() and _replace() without error-checking'
  117. __slots__ = ()
  118. _make = classmethod(tuple.__new__)
  119. def _replace(self, _map=map, **kwds):
  120. return self._make(_map(kwds.get, ('x', 'y'), self))
  121. print Point(11, 22)._replace(x=100)
  122. Point3D = namedtuple('Point3D', Point._fields + ('z',))
  123. print Point3D.__doc__
  124. import doctest
  125. TestResults = namedtuple('TestResults', 'failed attempted')
  126. print TestResults(*doctest.testmod())