[Cubicweb] Proposal: new method ResultSet.one()

Christophe de Vienne cdevienne at gmail.com
Wed Dec 4 15:40:53 CET 2013


Hi,

I propose to add a new method to ResultSet, named one(), that would
return exactly one entity from the resultset.
If more than one row and one column, or no row at all is found, an
exception would be raised.

It would allow to write :

entity = execute('SOME REQUEST').one()

where right now we have to do :

rset = execute('SOME REQUEST')
if rset.rowcount != 1:
    # do something
entity = rset.get_entity(0, 0)

The idea is shamelessly borowed from SQLAlchemy Query.one() [1]

I have a patch that propose an implementation, I can push it for review
and discussion if you think it is worth it.

Christophe

PS: btw, I did not manage to run the cubicweb test suite, is there some
things to be known to do so ?

[1]
http://docs.sqlalchemy.org/en/rel_0_9/orm/query.html#sqlalchemy.orm.query.Query.one



More information about the Cubicweb mailing list