]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- The :class:`.RowProxy` object is now sortable in Python as a regular
authorMike Bayer <mike_mp@zzzcomputing.com>
Wed, 20 Nov 2013 00:29:18 +0000 (19:29 -0500)
committerMike Bayer <mike_mp@zzzcomputing.com>
Wed, 20 Nov 2013 00:29:18 +0000 (19:29 -0500)
tuple is; this is accomplished via ensuring tuple() conversion on
both sides within the ``__eq__()`` method as well as
the addition of a ``__lt__()`` method. [ticket:2848]

doc/build/changelog/changelog_09.rst
doc/build/changelog/migration_09.rst
lib/sqlalchemy/engine/result.py
test/aaa_profiling/test_resultset.py
test/sql/test_query.py

index 6b7eea2ebac962dfcafa19062112b590cd180a41..6081951ecbf28a4ad1ee42d78c26b94db4ba5ca1 100644 (file)
 .. changelog::
     :version: 0.9.0b2
 
+    .. change::
+        :tags: bug, engine
+        :tickets: 2848
+
+        The :class:`.RowProxy` object is now sortable in Python as a regular
+        tuple is; this is accomplished via ensuring tuple() conversion on
+        both sides within the ``__eq__()`` method as well as
+        the addition of a ``__lt__()`` method.
+
+        .. seealso::
+
+            :ref:`migration_2848`
+
     .. change::
         :tags: bug, orm
         :tickets: 2833
index 72968a8f54fc387015f29d5ad6ddb077e58cc60e..a63c33430427c7f812dea44f1eae748f40357ce7 100644 (file)
@@ -287,6 +287,30 @@ The change is illustrated as follows::
 
 :ticket:`2833`
 
+.. _migration_2848:
+
+``RowProxy`` now has tuple-sorting behavior
+-------------------------------------------
+
+The :class:`.RowProxy` object acts much like a tuple, but up until now
+would not sort as a tuple if a list of them were sorted using ``sorted()``.
+The ``__eq__()`` method now compares both sides as a tuple and also
+an ``__lt__()`` method has been added::
+
+    users.insert().execute(
+            dict(user_id=1, user_name='foo'),
+            dict(user_id=2, user_name='bar'),
+            dict(user_id=3, user_name='def'),
+        )
+
+    rows = users.select().order_by(users.c.user_name).execute().fetchall()
+
+    eq_(rows, [(2, 'bar'), (3, 'def'), (1, 'foo')])
+
+    eq_(sorted(rows), [(1, 'foo'), (2, 'bar'), (3, 'def')])
+
+:ticket:`2848`
+
 .. _migration_2751:
 
 Association Proxy SQL Expression Improvements and Fixes
index 0e231657344306b0e1f09a18fb7f8caf46d2c5db..93c7d3b6bfa812ec68c6b9f33b9addb3bbd9576e 100644 (file)
@@ -125,8 +125,11 @@ class RowProxy(BaseRowProxy):
 
     __hash__ = None
 
+    def __lt__(self, other):
+        return tuple(self) < tuple(other)
+
     def __eq__(self, other):
-        return other is self or other == tuple(self)
+        return other is self or tuple(other) == tuple(self)
 
     def __ne__(self, other):
         return not self.__eq__(other)
index bbd8c4dba2d06ffb07b9c245a1cd892f735f1e5a..d2f8c22566df96cd0df0695872bbfc9de39e577f 100644 (file)
@@ -53,6 +53,7 @@ class ResultSetTest(fixtures.TestBase, AssertsExecutionResults):
             c1 in row
         go()
 
+
 class ExecutionTest(fixtures.TestBase):
 
     def test_minimal_connection_execute(self):
index 8e619fe7456d2156750d57aa2e589868b9b952f5..8c5e1db4d97cdf707ab6b0136eef352abcb53593 100644 (file)
@@ -1090,6 +1090,19 @@ class QueryTest(fixtures.TestBase):
         eq_(len(r), 1)
 
 
+    def test_sorting_in_python(self):
+        users.insert().execute(
+                dict(user_id=1, user_name='foo'),
+                dict(user_id=2, user_name='bar'),
+                dict(user_id=3, user_name='def'),
+            )
+
+        rows = users.select().order_by(users.c.user_name).execute().fetchall()
+
+        eq_(rows, [(2, 'bar'), (3, 'def'), (1, 'foo')])
+
+        eq_(sorted(rows), [(1, 'foo'), (2, 'bar'), (3, 'def')])
+
     def test_column_order_with_simple_query(self):
         # should return values in column definition order
         users.insert().execute(user_id=1, user_name='foo')