On Thu, 2 Jun 2011 19:43:29 +1000, Brian May wrote:
> On 2 June 2011 17:05, Sebastian Spaeth <sebast...@sspaeth.de> wrote:
> > What would be the best way to solve this (besides fixing the C api to
> > allow to reset the iterator ;-) ?)

> * It is not easy to fix the C api to reset the iterator (what about
> repeating the search?)

I am not sure about the difficulty of that, I am not a C-kind of
guy. Repeating the search would be easy but potentially gives you
different results since the db could have changed since then.
> * The only accurate way to get the number of messages is to iterate through
> every search result and count them?

There is count_messages() which wraps notmuch_query_count_messages which
invokes some very quick xapian function. But the xapian docs explicitely
state that it's xapians best guess and not guaranteed to be the real
number of messages (although it always was in my attempts). And you
don't want len() to return an approximation of the iterator length....

> If so, then len(...) I think might be very slow if there are a large number
> of elements.
> Maybe it might be easier/better to implement object.__nonzero__(self)
>  instead of the object.__len__(self) method?
> http://docs.python.org/reference/datamodel.html
> object.__nonzero__(self)
> Called to implement truth value testing and the built-in operation bool();
> should return False or True, or their integer equivalents 0 or 1. When this
> method is not defined, __len__() is called, if it is defined, and the object
> is considered true if its result is nonzero. If a class defines neither
> __len__() nor __nonzero__(), all its instances are considered true.

Interesting, did not know about this one. I guess that would solve the
example with the:

t = query.search_threads()

if t:
  for thread in t:
    print thread

Actually the "if t:" is no longer needed, I just tried a query returning
no Threads and 

for thread in threads:

works just fine with an empty Threads() object.

I made the change, and implemented __nonzero__ and removed the len()
method. It just doesn't make sense on 1-time iterators. (I documented
the change in the API docs). Sorry if this breaks existing code.

list(Threads()) works just fine too, it just took a while to create a
list of 13k Thread() objects on this laptop. (and list() will of course
not return until it is finished).


Attachment: pgp2HrsCIo0F1.pgp
Description: PGP signature

notmuch mailing list

Reply via email to