From: Mike Bayer Date: Wed, 20 Nov 2013 00:29:18 +0000 (-0500) Subject: - The :class:`.RowProxy` object is now sortable in Python as a regular X-Git-Tag: rel_0_9_0~114 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=02f21ffcf366da406795334d2fc6908a2f3c9b2f;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git - 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. [ticket:2848] --- diff --git a/doc/build/changelog/changelog_09.rst b/doc/build/changelog/changelog_09.rst index 6b7eea2eba..6081951ecb 100644 --- a/doc/build/changelog/changelog_09.rst +++ b/doc/build/changelog/changelog_09.rst @@ -14,6 +14,19 @@ .. 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 diff --git a/doc/build/changelog/migration_09.rst b/doc/build/changelog/migration_09.rst index 72968a8f54..a63c334304 100644 --- a/doc/build/changelog/migration_09.rst +++ b/doc/build/changelog/migration_09.rst @@ -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 diff --git a/lib/sqlalchemy/engine/result.py b/lib/sqlalchemy/engine/result.py index 0e23165734..93c7d3b6bf 100644 --- a/lib/sqlalchemy/engine/result.py +++ b/lib/sqlalchemy/engine/result.py @@ -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) diff --git a/test/aaa_profiling/test_resultset.py b/test/aaa_profiling/test_resultset.py index bbd8c4dba2..d2f8c22566 100644 --- a/test/aaa_profiling/test_resultset.py +++ b/test/aaa_profiling/test_resultset.py @@ -53,6 +53,7 @@ class ResultSetTest(fixtures.TestBase, AssertsExecutionResults): c1 in row go() + class ExecutionTest(fixtures.TestBase): def test_minimal_connection_execute(self): diff --git a/test/sql/test_query.py b/test/sql/test_query.py index 8e619fe745..8c5e1db4d9 100644 --- a/test/sql/test_query.py +++ b/test/sql/test_query.py @@ -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')