]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
adding _fields, _asdict() to KeyedTuple #2601
authorDiana Clarke <diana.joan.clarke@gmail.com>
Sat, 17 Nov 2012 19:55:26 +0000 (14:55 -0500)
committerDiana Clarke <diana.joan.clarke@gmail.com>
Sat, 17 Nov 2012 19:55:26 +0000 (14:55 -0500)
lib/sqlalchemy/util/_collections.py
test/base/test_utils.py

index 0c879468e9a0179836321c4bb7e35fb60929b30c..cefbb6d7f770a5332c679ea203e0be4fd3d03a42 100644 (file)
@@ -41,6 +41,13 @@ class KeyedTuple(tuple):
     def keys(self):
         return [l for l in self._labels if l is not None]
 
+    @property
+    def _fields(self):
+        return tuple(self.keys())
+
+    def _asdict(self):
+        return dict((key, self.__dict__[key]) for key in self.keys())
+
 class ImmutableContainer(object):
     def _immutable(self, *arg, **kw):
         raise TypeError("%s object is immutable" % self.__class__.__name__)
index 14da4768de0a74373a74d9585c07cc3d3d10c9b6..fe46377a27b0d334f82265d30a9a3aded48d15c0 100644 (file)
@@ -15,21 +15,43 @@ class KeyedTupleTest():
         eq_(str(keyed_tuple), '()')
         eq_(keyed_tuple.__dict__, {})
 
-        # consider returning an empty [] rather than raising
+        # TODO: consider returning an empty [] rather than raising
         assert_raises(AttributeError, keyed_tuple.keys)
 
+        # TODO: consider returning an empty {} rather than raising
+        assert_raises(AttributeError, keyed_tuple._asdict)
+
+        # TODO: consider returning an empty () rather than raising
+        def should_raise():
+            keyed_tuple._fields
+        assert_raises(AttributeError, should_raise)
+
     def test_values_but_no_labels(self):
         keyed_tuple = util.KeyedTuple([1, 2])
         eq_(type(keyed_tuple), util.KeyedTuple)
         eq_(str(keyed_tuple), '(1, 2)')
         eq_(keyed_tuple.__dict__, {})
 
-        # consider returning an empty [] rather than raising
+        # TODO: consider returning an empty [] rather than raising
         assert_raises(AttributeError, keyed_tuple.keys)
 
-    def test_basic_index_access(self):
+        # TODO: consider returning an empty {} rather than raising
+        assert_raises(AttributeError, keyed_tuple._asdict)
+
+        # TODO: consider returning an empty () rather than raising
+        def should_raise():
+            keyed_tuple._fields
+        assert_raises(AttributeError, should_raise)
+
+    def test_basic_creation(self):
         keyed_tuple = util.KeyedTuple([1, 2], ['a', 'b'])
         eq_(str(keyed_tuple), '(1, 2)')
+        eq_(keyed_tuple.keys(), ['a', 'b'])
+        eq_(keyed_tuple._fields, ('a', 'b'))
+        eq_(keyed_tuple._asdict(), {'a': 1, 'b': 2})
+
+    def test_basic_index_access(self):
+        keyed_tuple = util.KeyedTuple([1, 2], ['a', 'b'])
         eq_(keyed_tuple[0], 1)
         eq_(keyed_tuple[1], 2)
 
@@ -39,7 +61,6 @@ class KeyedTupleTest():
 
     def test_basic_attribute_access(self):
         keyed_tuple = util.KeyedTuple([1, 2], ['a', 'b'])
-        eq_(str(keyed_tuple), '(1, 2)')
         eq_(keyed_tuple.a, 1)
         eq_(keyed_tuple.b, 2)
 
@@ -50,9 +71,13 @@ class KeyedTupleTest():
     def test_none_label(self):
         keyed_tuple = util.KeyedTuple([1, 2, 3], ['a', None, 'b'])
         eq_(str(keyed_tuple), '(1, 2, 3)')
+
+        # TODO: consider not allowing None labels
         expected = {'a': 1, None: 2, 'b': 3, '_labels': ['a', None, 'b']}
         eq_(keyed_tuple.__dict__, expected)
         eq_(keyed_tuple.keys(), ['a', 'b'])
+        eq_(keyed_tuple._fields, ('a', 'b'))
+        eq_(keyed_tuple._asdict(), {'a': 1, 'b': 3})
 
         # attribute access: can't get at value 2
         eq_(keyed_tuple.a, 1)
@@ -66,9 +91,13 @@ class KeyedTupleTest():
     def test_duplicate_labels(self):
         keyed_tuple = util.KeyedTuple([1, 2, 3], ['a', 'b', 'b'])
         eq_(str(keyed_tuple), '(1, 2, 3)')
+
+        # TODO: consider not allowing duplicate labels
         expected = {'a': 1, 'b': 3, '_labels': ['a', 'b', 'b']}
         eq_(keyed_tuple.__dict__, expected)
         eq_(keyed_tuple.keys(), ['a', 'b', 'b'])
+        eq_(keyed_tuple._fields, ('a', 'b', 'b'))
+        eq_(keyed_tuple._asdict(), {'a': 1, 'b': 3})
 
         # attribute access: can't get at value 2
         eq_(keyed_tuple.a, 1)