Oleg Broytmann wrote:
> On Sun, May 14, 2006 at 01:39:37AM +0100, Christopher Arndt wrote:
>
>>Is there a special reason why the SelectResult class does not implement
>>__len__ or __nonzero__ methods?
>
> http://svn.colorstudy.com/SQLObject/docs/FAQ.txt
Ok, read that and agree that this rules out implicit SQL execution for
the __len__ method.
But do these concerns apply to the __nonzero__ method as well? Is this
also expected to be a "fast" operation? How do other iterators handle this?
I don't really see the point of the "speed" argument anyway, since the
whole point of SQLObject is, that even mere attribute access can trigger
SQL execution, which can take an arbitrary amount of time.
Anyway, I figured out, how I can add this behaviour to SelectResult as a
MixIn:
class MySelectResult:
def __nonzero__(self):
return self.count() > 0
SQLObject.SelectResultsClass.__bases__ += (MySelectResult,)
class Person(SQLObject):
name = StringCol(alternateID=True, length=20)
org = StringCol(length=100)
if __name__ == '__main__':
Person.createTable()
u1 = Person(name="joe", org="doe.com")
u2 = Person(name="alice", org="foo.com")
u3 = Person(name="bob", org="foo.com")
pl = Person.select(Person.q.org == 'foo.com')
if pl:
for p in pl:
print p.name
else:
print "No records found."
-------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
sqlobject-discuss mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/sqlobject-discuss