Warning: Can't synchronize with repository "(default)" (Unsupported version control system "svn": No module named svn). Look in the Trac log for more information.

Ticket #1648 (closed enhancement: fixed)

Opened 11 years ago

Last modified 10 years ago

Paginate decorator for arbitrary list-like objects

Reported by: lilspikey Owned by: anonymous
Priority: normal Milestone: 1.1b2
Component: TurboGears Version: 1.0.4b3
Severity: normal Keywords: needs tests


Currently the paginate decorator accepts list objects for pagination, but does not accept "list-like" objects.

When deciding how to handle pagination of vardata currently isinstance(vardata,list) to determine if the object can be treated like a list to calculate the number of elements via a a call to len(vardata).

If instead the check was for whether the object had the __len__ (e.g. via hasattr(vardata,'__len__')) method then any object that supports len() and slicing could be paginated using the decorator.

The code in question currently looks like:

194 	            elif isinstance(var_data, list) or (sqlalchemy and isinstance(
195 	                    var_data, sqlalchemy.orm.attributes.InstrumentedList)):
196 	                row_count = len(var_data)
198 	            else:
199 	                raise StandardError(
200 	                    'Variable is not a list or SelectResults or Query (%s)' % type(
201 	                            var_data))

and could possibly be changed to:

194 	            elif hasattr(var_data, '__len__'):
196 	                row_count = len(var_data)
198 	            else:
199 	                raise StandardError(
200 	                    'Variable is not a list or SelectResults or Query (%s)' % type(
201 	                            var_data))

One potential downside however is that objects that do have a __len__ method, but do not support slicing will may produce mis-leading errors as they will not trigger the StandardError exception.

The original discussion of this enhancement is here:


Change History

comment:1 Changed 11 years ago by roger.demetrescu

Regarding the slicing support, we can identify the minimum slice methods and raise an exception when those are not found (I believe getitem (or getslice) are enough).

comment:2 Changed 11 years ago by Chris Arndt

  • Milestone changed from 1.0.4 to 1.1

comment:3 Changed 11 years ago by streawkceur

Additionally, sqlalchemy.orm.attributes.InstrumentedList doesn't even exist since version 4.0, raising an AttributeError when using any list-like object that's not yet supported.

The error message should also be adjusted to something like:

"Variable is neither a SelectResults, nor a Query, " \
"nor a list-like object (doesn't implement __len__). Type: %s"

+1 for this patch!

comment:4 Changed 11 years ago by faide

  • Milestone changed from 1.5 to 1.1

comment:5 Changed 11 years ago by Chris Arndt

  • Keywords needs tests added
  • Milestone changed from 1.1 to 1.1 beta 2

comment:6 Changed 10 years ago by faide

+1 for this patch if someone adds tests and adds a control to make sure the object also supports slicing.

Tests should be straight forward to write... anyone?

comment:7 Changed 10 years ago by chrisz

  • Status changed from new to closed
  • Resolution set to fixed

This works already, since I've implemented something similar already in r4315 which is available since TG 1.0.5. I only added a simple regression test now in r5674.

Note: See TracTickets for help on using tickets.