Shorter name for namedtuple()

This commit is contained in:
Raymond Hettinger 2007-10-23 20:37:41 +00:00
parent 910ab50091
commit 01a0957f06
3 changed files with 24 additions and 22 deletions

View File

@ -1,14 +1,14 @@
__all__ = ['deque', 'defaultdict', 'named_tuple']
__all__ = ['deque', 'defaultdict', 'namedtuple']
from _collections import deque, defaultdict
from operator import itemgetter as _itemgetter
from keyword import iskeyword as _iskeyword
import sys as _sys
def named_tuple(typename, field_names, verbose=False):
def namedtuple(typename, field_names, verbose=False):
"""Returns a new subclass of tuple with named fields.
>>> Point = named_tuple('Point', 'x y')
>>> Point = namedtuple('Point', 'x y')
>>> Point.__doc__ # docstring for the new class
'Point(x, y)'
>>> p = Point(11, y=22) # instantiate with positional args or keywords
@ -94,10 +94,10 @@ def named_tuple(typename, field_names, verbose=False):
if __name__ == '__main__':
# verify that instances can be pickled
from cPickle import loads, dumps
Point = named_tuple('Point', 'x, y', True)
Point = namedtuple('Point', 'x, y', True)
p = Point(x=10, y=20)
assert p == loads(dumps(p))
import doctest
TestResults = named_tuple('TestResults', 'failed attempted')
TestResults = namedtuple('TestResults', 'failed attempted')
print TestResults(*doctest.testmod())

View File

@ -1,31 +1,31 @@
import unittest
from test import test_support
from collections import named_tuple
from collections import namedtuple
class TestNamedTuple(unittest.TestCase):
def test_factory(self):
Point = named_tuple('Point', 'x y')
Point = namedtuple('Point', 'x y')
self.assertEqual(Point.__name__, 'Point')
self.assertEqual(Point.__doc__, 'Point(x, y)')
self.assertEqual(Point.__slots__, ())
self.assertEqual(Point.__module__, __name__)
self.assertEqual(Point.__getitem__, tuple.__getitem__)
self.assertRaises(ValueError, named_tuple, 'abc%', 'efg ghi') # type has non-alpha char
self.assertRaises(ValueError, named_tuple, 'class', 'efg ghi') # type has keyword
self.assertRaises(ValueError, named_tuple, '9abc', 'efg ghi') # type starts with digit
self.assertRaises(ValueError, namedtuple, 'abc%', 'efg ghi') # type has non-alpha char
self.assertRaises(ValueError, namedtuple, 'class', 'efg ghi') # type has keyword
self.assertRaises(ValueError, namedtuple, '9abc', 'efg ghi') # type starts with digit
self.assertRaises(ValueError, named_tuple, 'abc', 'efg g%hi') # field with non-alpha char
self.assertRaises(ValueError, named_tuple, 'abc', 'abc class') # field has keyword
self.assertRaises(ValueError, named_tuple, 'abc', '8efg 9ghi') # field starts with digit
self.assertRaises(ValueError, named_tuple, 'abc', '__efg__ ghi') # field with double underscores
self.assertRaises(ValueError, named_tuple, 'abc', 'efg efg ghi') # duplicate field
self.assertRaises(ValueError, namedtuple, 'abc', 'efg g%hi') # field with non-alpha char
self.assertRaises(ValueError, namedtuple, 'abc', 'abc class') # field has keyword
self.assertRaises(ValueError, namedtuple, 'abc', '8efg 9ghi') # field starts with digit
self.assertRaises(ValueError, namedtuple, 'abc', '__efg__ ghi') # field with double underscores
self.assertRaises(ValueError, namedtuple, 'abc', 'efg efg ghi') # duplicate field
named_tuple('Point0', 'x1 y2') # Verify that numbers are allowed in names
namedtuple('Point0', 'x1 y2') # Verify that numbers are allowed in names
def test_instance(self):
Point = named_tuple('Point', 'x y')
Point = namedtuple('Point', 'x y')
p = Point(11, 22)
self.assertEqual(p, Point(x=11, y=22))
self.assertEqual(p, Point(11, y=22))
@ -44,17 +44,17 @@ class TestNamedTuple(unittest.TestCase):
self.assertEqual(p.__asdict__(), dict(x=11, y=22)) # test __dict__ method
# verify that field string can have commas
Point = named_tuple('Point', 'x, y')
Point = namedtuple('Point', 'x, y')
p = Point(x=11, y=22)
self.assertEqual(repr(p), 'Point(x=11, y=22)')
# verify that fieldspec can be a non-string sequence
Point = named_tuple('Point', ('x', 'y'))
Point = namedtuple('Point', ('x', 'y'))
p = Point(x=11, y=22)
self.assertEqual(repr(p), 'Point(x=11, y=22)')
def test_tupleness(self):
Point = named_tuple('Point', 'x y')
Point = namedtuple('Point', 'x y')
p = Point(11, 22)
self.assert_(isinstance(p, tuple))
@ -73,9 +73,9 @@ class TestNamedTuple(unittest.TestCase):
self.assertRaises(AttributeError, eval, 'p.z', locals())
def test_odd_sizes(self):
Zero = named_tuple('Zero', '')
Zero = namedtuple('Zero', '')
self.assertEqual(Zero(), ())
Dot = named_tuple('Dot', 'd')
Dot = namedtuple('Dot', 'd')
self.assertEqual(Dot(1), (1,))
def test_main(verbose=None):

View File

@ -798,6 +798,8 @@ Library
Extension Modules
-----------------
- collections.defaultdict now has a repr() function that can be run through eval()
- Patch #1388440: Add set_completion_display_matches_hook and
get_completion_type to readline.